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