Directories can be read by anyone who has access permission to read the directory. But, only the operating system kernel can write to a directory, to preserve file system sanity. Here are the system calls that can be used on directories.
#include <dirent.h> DIR *opendir(const char *pathname); struct dirent *readdir(DIR *dp); void rewinddir(DIR *dp); int clodedir(DIR *dp);
The DIR structure is an internal structure used by the aforementioned functions to maintain information about the directory being read. It’s purpose is similar to FILE structure maintained by the standard I/O library, that we saw earlier. The dirent structure is defined in the file <dirent.h> and is implementation dependent. It generally contains a null terminated filename and i-node number.
opendir system call initialises things so that the first readdir reads the first entry in the directory. The ordering of entries within the directory is implementation dependent. opendir and readdir return pointers (of types DIR and struct dirent respectively) if there were no errors, else NULL.
rewinddir resets the pointer to point to the beginning of the directory.
closedir closes the directory stream associated with the passed pointer. It returns 0 if successful, -1 in case of any error.