]> git.saurik.com Git - apple/libc.git/blob - gen/directory.3
Libc-594.1.4.tar.gz
[apple/libc.git] / gen / directory.3
1 .\" Copyright (c) 1983, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\" @(#)directory.3 8.1 (Berkeley) 6/4/93
33 .\" $FreeBSD: src/lib/libc/gen/directory.3,v 1.12 2001/10/01 16:08:50 ru Exp $
34 .\"
35 .Dd June 4, 1993
36 .Dt DIRECTORY 3
37 .Os
38 .Sh NAME
39 .Nm closedir ,
40 .Nm dirfd ,
41 .Nm opendir ,
42 .Nm readdir ,
43 .Nm readdir_r ,
44 .Nm rewinddir ,
45 .Nm seekdir ,
46 .Nm telldir
47 .Nd directory operations
48 .Sh LIBRARY
49 .Lb libc
50 .Sh SYNOPSIS
51 .In dirent.h
52 .Ft int
53 .Fn closedir "DIR *dirp"
54 .Ft int
55 .Fn dirfd "DIR *dirp"
56 .Ft DIR *
57 .Fn opendir "const char *dirname"
58 .Ft struct dirent *
59 .Fn readdir "DIR *dirp"
60 .Ft int
61 .Fn readdir_r "DIR *restrict dirp" "struct dirent *restrict entry" \
62 "struct dirent **restrict result"
63 .Ft void
64 .Fn rewinddir "DIR *dirp"
65 .Ft void
66 .Fn seekdir "DIR *dirp" "long loc"
67 .Ft long
68 .Fn telldir "DIR *dirp"
69 .Sh DESCRIPTION
70 The
71 .Fn opendir
72 function
73 opens the directory named by
74 .Fa dirname ,
75 associates a
76 .Em directory stream
77 with it,
78 and returns a pointer to be used to identify the
79 .Em directory stream
80 in subsequent operations. The pointer
81 .Dv NULL
82 is returned if
83 .Fa dirname
84 cannot be accessed or if it cannot
85 .Xr malloc 3
86 enough memory to hold the whole thing.
87 .Pp
88 The
89 .Fn readdir
90 function
91 returns a pointer to the next directory entry. It returns
92 .Dv NULL
93 upon reaching the end of the directory or detecting an invalid
94 .Fn seekdir
95 operation.
96 .Pp
97 .Fn readdir_r
98 provides the same functionality as
99 .Fn readdir ,
100 but the caller must provide a directory
101 .Fa entry
102 buffer to store the results in. If the read succeeds,
103 .Fa result
104 is pointed at the
105 .Fa entry ;
106 upon reaching the end of the directory,
107 .Fa result
108 is set to
109 .Dv NULL .
110 .Fn readdir_r
111 returns 0 on success or an error number to indicate failure.
112 .Pp
113 The
114 .Fn telldir
115 function
116 returns the current location associated with the named
117 .Em directory stream .
118 Values returned by
119 .Fn telldir
120 are good only for the lifetime of the
121 .Dv DIR
122 pointer (e.g.,
123 .Fa dirp )
124 from which they are derived. If the directory is closed and then
125 reopened, prior values returned by
126 .Fn telldir
127 will no longer be valid.
128 .Pp
129 The
130 .Fn seekdir
131 function
132 sets the position of the next
133 .Fn readdir
134 operation on the
135 .Em directory stream .
136 The new position reverts to the one associated with the
137 .Em directory stream
138 when the
139 .Fn telldir
140 operation was performed.
141 .Pp
142 The
143 .Fn rewinddir
144 function
145 resets the position of the named
146 .Em directory stream
147 to the beginning of the directory.
148 .Pp
149 The
150 .Fn closedir
151 function
152 closes the named
153 .Em directory stream
154 and frees the structure associated with the
155 .Fa dirp
156 pointer,
157 returning 0 on success.
158 On failure, \-1 is returned and the global variable
159 .Va errno
160 is set to indicate the error.
161 .Pp
162 The
163 .Fn dirfd
164 function
165 returns the integer file descriptor associated with the named
166 .Em directory stream ,
167 see
168 .Xr open 2 .
169 .Pp
170 Sample code which searches a directory for entry ``name'' is:
171 .Bd -literal -offset indent
172 len = strlen(name);
173 dirp = opendir(".");
174 while ((dp = readdir(dirp)) != NULL)
175 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
176 (void)closedir(dirp);
177 return FOUND;
178 }
179 (void)closedir(dirp);
180 return NOT_FOUND;
181 .Ed
182 .Sh LEGACY SYNOPSIS
183 .Fd #include <sys/types.h>
184 .Fd #include <dirent.h>
185 .Pp
186 .In sys/types.h
187 is necessary for these functions.
188 .Sh SEE ALSO
189 .Xr close 2 ,
190 .Xr lseek 2 ,
191 .Xr open 2 ,
192 .Xr read 2 ,
193 .Xr compat 5 ,
194 .Xr dir 5
195 .Sh HISTORY
196 The
197 .Fn closedir ,
198 .Fn dirfd ,
199 .Fn opendir ,
200 .Fn readdir ,
201 .Fn rewinddir ,
202 .Fn seekdir ,
203 and
204 .Fn telldir
205 functions appeared in
206 .Bx 4.2 .