Commit | Line | Data |
---|---|---|
577ec8b7 | 1 | .\" Copyright (c) 1983 Regents of the University of California. |
25aac6e1 | 2 | .\" All rights reserved. |
577ec8b7 | 3 | .\" |
91cff1e1 | 4 | .\" %sccs.include.redist.man% |
25aac6e1 | 5 | .\" |
91cff1e1 | 6 | .\" @(#)directory.3 6.6 (Berkeley) %G% |
577ec8b7 | 7 | .\" |
600e5e8e | 8 | .TH DIRECTORY 3 "" |
577ec8b7 KM |
9 | .UC 5 |
10 | .SH NAME | |
01cc7fcc | 11 | opendir, readdir, telldir, seekdir, rewinddir, closedir, dirfd \- directory operations |
577ec8b7 | 12 | .SH SYNOPSIS |
b5870130 KB |
13 | .nf |
14 | .ft B | |
15 | #include <sys/types.h> | |
16 | #include <dirent.h> | |
17 | ||
18 | DIR * | |
19 | opendir(const char *filename); | |
20 | ||
21 | struct direct | |
22 | *readdir(DIR * dirp); | |
23 | ||
24 | long | |
25 | telldir(const DIR *dirp); | |
26 | ||
27 | void | |
28 | seekdir(DIR *dirp, long loc); | |
29 | ||
30 | void | |
31 | rewinddir(DIR *dirp); | |
32 | ||
33 | int | |
34 | closedir(DIR *dirp); | |
35 | ||
36 | int | |
37 | dirfd(DIR *dirp) | |
38 | .ft R | |
39 | .fi | |
577ec8b7 KM |
40 | .SH DESCRIPTION |
41 | .I Opendir | |
42 | opens the directory named by | |
43 | .I filename | |
44 | and associates a | |
45 | .I directory stream | |
46 | with it. | |
47 | .I Opendir | |
48 | returns a pointer to be used to identify the | |
49 | .I directory stream | |
50 | in subsequent operations. The pointer | |
51 | .SM | |
52 | .B NULL | |
53 | is returned if | |
54 | .I filename | |
55 | cannot be accessed, or if it cannot | |
56 | .IR malloc (3) | |
57 | enough memory to hold the whole thing. | |
58 | .PP | |
59 | .I Readdir | |
60 | returns a pointer to the next directory entry. It returns | |
61 | .B NULL | |
62 | upon reaching the end of the directory or detecting an invalid | |
63 | .I seekdir | |
64 | operation. | |
65 | .PP | |
66 | .I Telldir | |
67 | returns the current location associated with the named | |
68 | .I directory stream. | |
69 | .PP | |
70 | .I Seekdir | |
71 | sets the position of the next | |
72 | .I readdir | |
73 | operation on the | |
74 | .I directory stream. | |
75 | The new position reverts to the one associated with the | |
76 | .I directory stream | |
77 | when the | |
78 | .I telldir | |
79 | operation was performed. Values returned by | |
80 | .I telldir | |
81 | are good only for the lifetime of the DIR pointer from which they are derived. | |
82 | If the directory is closed and then reopened, the | |
83 | .I telldir | |
84 | value may be invalidated due to undetected directory compaction. | |
85 | It is safe to use a previous | |
86 | .I telldir | |
87 | value immediately after a call to | |
88 | .I opendir | |
89 | and before any calls to | |
90 | .I readdir. | |
91 | .PP | |
92 | .I Rewinddir | |
93 | resets the position of the named | |
94 | .I directory stream | |
95 | to the beginning of the directory. | |
96 | .PP | |
97 | .I Closedir | |
98 | closes the named | |
99 | .I directory stream | |
b5870130 KB |
100 | and frees the structure associated with the DIR pointer, |
101 | returning 0 on success. | |
102 | On failure, -1 is returned and errno is set to indicate the error. | |
577ec8b7 | 103 | .PP |
01cc7fcc KB |
104 | .I Dirfd |
105 | returns the integer file descriptor associated with the named | |
106 | .I directory stream, | |
107 | see open(2). | |
108 | .PP | |
577ec8b7 KM |
109 | Sample code which searchs a directory for entry ``name'' is: |
110 | .PP | |
b5870130 KB |
111 | .nf |
112 | .RS | |
113 | len = strlen(name); | |
114 | dirp = opendir("."); | |
115 | for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) | |
116 | if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { | |
117 | (void)closedir(dirp); | |
118 | return FOUND; | |
119 | } | |
120 | (void)closedir(dirp); | |
121 | return NOT_FOUND; | |
122 | .RE | |
123 | .fi | |
577ec8b7 | 124 | .SH "SEE ALSO" |
b5870130 | 125 | open(2), close(2), read(2), lseek(2), dir(5) |