DIR
*opendir(filename)
char *filename;
struct direct
*readdir(dirp)
DIR *dirp;
long
telldir(dirp)
DIR *dirp;
seekdir(dirp, loc)
DIR *dirp;
long loc;
rewinddir(dirp)
DIR *dirp;
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 operations. The pointer NULL is returned if filename cannot be accessed or is not a directory.
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.
Telldir returns the current location associated with the named directory stream.
Seekdir sets the position of the next readdir operation on the directory stream. The new position reverts to the one associated with the directory stream when the telldir operation was performed. Values returned by telldir are good only for the lifetime of the DIR pointer from which they are derived. If the directory is closed and then reopened, the telldir value may be invalidated due to undetected directory compaction in 4.2BSD. It is safe to use a previous telldir value immediately after a call to opendir and before any calls to readdir.
Rewinddir resets the position of the named directory stream to the beginning of the directory.
Closedir causes the named directory stream to be closed, and the structure associated with the DIR pointer to be freed.
A direct structure is as follows:
struct direct { /* unsigned */ long d_ino; /* inode number of entry */ unsigned short d_reclen; /* length of this record */ unsigned short d_namlen; /* length of string in d_name */ char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */ };
The d_reclen field is meaningless in non-4.2BSD systems and should be ignored. The use of a long for d_ino is also a 4.2BSDism; ino_t (see types(5)) should be used elsewhere. The macro DIRSIZ(dp) gives the minimum memory size needed to hold the direct value pointed to by dp, with the minimum necessary allocation for d_name.
The preferred way to search the current directory for entry ``name'' is:
len = strlen(name); dirp = opendir("."); if (dirp == NULL) { fprintf(stderr, "%s: can't read directory .\n", argv[0]); return NOT_FOUND; } while ((dp = readdir(dirp)) != NULL) if (dp->d_namlen == len && strcmp(dp->d_name, name) == 0) { closedir(dirp); return FOUND; } closedir(dirp); return NOT_FOUND;
This manual page has been substantially rewritten to be informative in the absence of a 4.2BSD manual.
The returned value of readdir points to a static area that will be overwritten by subsequent calls.
There are some unfortunate name conflicts with the real V7 directory structure definitions.