1DIRECTORY(3) Library Functions Manual DIRECTORY(3)
2
3
4
6 opendir, readdir, telldir, seekdir, rewinddir, closedir - directory
7 operations
8
10 #include <sys/types.h>
11 #include <sys/dir.h>
12
13 DIR *opendir(filename)
14 char *filename;
15
16 struct direct *readdir(dirp)
17 DIR *dirp;
18
19 long telldir(dirp)
20 DIR *dirp;
21
22 seekdir(dirp, loc)
23 DIR *dirp;
24 long loc;
25
26 rewinddir(dirp)
27 DIR *dirp;
28
29 closedir(dirp)
30 DIR *dirp;
31
33 Opendir opens the directory named by filename and associates a direc‐
34 tory stream with it. Opendir returns a pointer to be used to identify
35 the directory stream in subsequent operations. The pointer NULL is
36 returned if filename cannot be accessed, or if it cannot malloc(3)
37 enough memory to hold the whole thing.
38
39 Readdir returns a pointer to the next directory entry. It returns NULL
40 upon reaching the end of the directory or detecting an invalid seekdir
41 operation.
42
43 Telldir returns the current location associated with the named direc‐
44 tory stream.
45
46 Seekdir sets the position of the next readdir operation on the direc‐
47 tory stream. The new position reverts to the one associated with the
48 directory stream when the telldir operation was performed. Values
49 returned by telldir are good only for the lifetime of the DIR pointer
50 from which they are derived. If the directory is closed and then
51 reopened, the telldir value may be invalidated due to undetected direc‐
52 tory compaction. It is safe to use a previous telldir value immedi‐
53 ately after a call to opendir and before any calls to readdir.
54
55 Rewinddir resets the position of the named directory stream to the
56 beginning of the directory.
57
58 Closedir closes the named directory stream and frees the structure
59 associated with the DIR pointer.
60
61 Sample code which searchs a directory for entry ``name'' is:
62
63 len = strlen(name);
64 dirp = opendir(".");
65 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
66 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
67 closedir(dirp);
68 return FOUND;
69 }
70 closedir(dirp);
71 return NOT_FOUND;
72
74 open(2), close(2), read(2), lseek(2), dir(5)
75
76
77
784.2 Berkeley Distribution September 24, 1985 DIRECTORY(3)