directory(3P) directory(3P)NAME opendir, readdir, telldir, seekdir, rewinddir, closedir - directory operations SYNOPSIS #include <sys/types.h> #include <direct.h> DIR *opendir(filename) char *filename; struct direct *readdir(dirp) DIR *dirp; void rewinddir(dirp) DIR *dirp; int closedir(dirp) DIR *dirp; DESCRIPTION opendir opens the directory named by filename and associates a directory stream with it. opendir returns a pointer to be used to identify the directory stream in subsequent opera- tions. The pointer NULL is returned if filename cannot be accessed, or if it cannot malloc(3) enough memory to hold the whole thing. readdir returns a pointer to the next directory entry. It returns NULL upon reaching the end of the directory or detecting an invalid seekdir operation. The rewinddir macro resets the position of the named direc- tory stream to the beginning of the directory. It also causes the directory stream to refer to the current state of the directory. closedir closes the named directory stream and frees the structure associated with the DIR pointer. Sample code which searchs a directory for entry name is: len = strlen(name); dirp = opendir("."); for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { closedir(dirp); return FOUND; } closedir(dirp); return NOT_FOUND; April, 1990 1
directory(3P) directory(3P)The result of using a directory stream after an exec(2) is undefined. After a fork(2), either the parent of the child (but not both) may continue processing the directory stream using readdir and/or rewinddir. SEE ALSO ls(1), open(2), close(2), getdirentries(2), read(2), lseek(2), dir(4). 2 April, 1990