]> git.saurik.com Git - apple/libc.git/blame - gen/directory.3
Libc-825.24.tar.gz
[apple/libc.git] / gen / directory.3
CommitLineData
5b2abdfb
A
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
224c7076
A
39.Nm closedir ,
40.Nm dirfd ,
5b2abdfb
A
41.Nm opendir ,
42.Nm readdir ,
43.Nm readdir_r ,
5b2abdfb 44.Nm rewinddir ,
224c7076
A
45.Nm seekdir ,
46.Nm telldir
5b2abdfb
A
47.Nd directory operations
48.Sh LIBRARY
49.Lb libc
50.Sh SYNOPSIS
5b2abdfb 51.In dirent.h
224c7076
A
52.Ft int
53.Fn closedir "DIR *dirp"
54.Ft int
55.Fn dirfd "DIR *dirp"
5b2abdfb 56.Ft DIR *
224c7076 57.Fn opendir "const char *dirname"
5b2abdfb
A
58.Ft struct dirent *
59.Fn readdir "DIR *dirp"
60.Ft int
224c7076
A
61.Fn readdir_r "DIR *restrict dirp" "struct dirent *restrict entry" \
62 "struct dirent **restrict result"
5b2abdfb
A
63.Ft void
64.Fn rewinddir "DIR *dirp"
224c7076
A
65.Ft void
66.Fn seekdir "DIR *dirp" "long loc"
67.Ft long
68.Fn telldir "DIR *dirp"
5b2abdfb
A
69.Sh DESCRIPTION
70The
71.Fn opendir
72function
73opens the directory named by
224c7076 74.Fa dirname ,
5b2abdfb
A
75associates a
76.Em directory stream
224c7076
A
77with it,
78and returns a pointer to be used to identify the
5b2abdfb
A
79.Em directory stream
80in subsequent operations. The pointer
81.Dv NULL
82is returned if
224c7076
A
83.Fa dirname
84cannot be accessed or if it cannot
5b2abdfb
A
85.Xr malloc 3
86enough memory to hold the whole thing.
87.Pp
88The
89.Fn readdir
90function
91returns a pointer to the next directory entry. It returns
92.Dv NULL
93upon reaching the end of the directory or detecting an invalid
94.Fn seekdir
95operation.
96.Pp
97.Fn readdir_r
98provides the same functionality as
99.Fn readdir ,
100but the caller must provide a directory
101.Fa entry
102buffer to store the results in. If the read succeeds,
103.Fa result
104is pointed at the
105.Fa entry ;
224c7076 106upon reaching the end of the directory,
5b2abdfb
A
107.Fa result
108is set to
109.Dv NULL .
110.Fn readdir_r
111returns 0 on success or an error number to indicate failure.
112.Pp
113The
114.Fn telldir
115function
116returns the current location associated with the named
117.Em directory stream .
118Values returned by
119.Fn telldir
120are good only for the lifetime of the
121.Dv DIR
224c7076
A
122pointer (e.g.,
123.Fa dirp )
5b2abdfb
A
124from which they are derived. If the directory is closed and then
125reopened, prior values returned by
126.Fn telldir
127will no longer be valid.
128.Pp
129The
130.Fn seekdir
131function
132sets the position of the next
133.Fn readdir
134operation on the
135.Em directory stream .
136The new position reverts to the one associated with the
137.Em directory stream
138when the
139.Fn telldir
140operation was performed.
141.Pp
142The
143.Fn rewinddir
144function
145resets the position of the named
146.Em directory stream
147to the beginning of the directory.
148.Pp
149The
150.Fn closedir
151function
152closes the named
153.Em directory stream
154and frees the structure associated with the
155.Fa dirp
156pointer,
157returning 0 on success.
158On failure, \-1 is returned and the global variable
159.Va errno
160is set to indicate the error.
161.Pp
162The
163.Fn dirfd
164function
165returns the integer file descriptor associated with the named
ad3c9f2a
A
166.Em directory stream
167on success, see
5b2abdfb 168.Xr open 2 .
ad3c9f2a
A
169On failure, \-1 is returned and the global variable
170.Va errno
171is set to indicate the error.
5b2abdfb
A
172.Pp
173Sample code which searches a directory for entry ``name'' is:
174.Bd -literal -offset indent
175len = strlen(name);
176dirp = opendir(".");
177while ((dp = readdir(dirp)) != NULL)
178 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
179 (void)closedir(dirp);
180 return FOUND;
181 }
182(void)closedir(dirp);
183return NOT_FOUND;
184.Ed
224c7076
A
185.Sh LEGACY SYNOPSIS
186.Fd #include <sys/types.h>
187.Fd #include <dirent.h>
188.Pp
189.In sys/types.h
190is necessary for these functions.
5b2abdfb
A
191.Sh SEE ALSO
192.Xr close 2 ,
193.Xr lseek 2 ,
194.Xr open 2 ,
195.Xr read 2 ,
224c7076 196.Xr compat 5 ,
5b2abdfb
A
197.Xr dir 5
198.Sh HISTORY
199The
224c7076
A
200.Fn closedir ,
201.Fn dirfd ,
5b2abdfb
A
202.Fn opendir ,
203.Fn readdir ,
5b2abdfb 204.Fn rewinddir ,
224c7076 205.Fn seekdir ,
5b2abdfb 206and
224c7076 207.Fn telldir
5b2abdfb
A
208functions appeared in
209.Bx 4.2 .