1 .\" Copyright 2014 Garrett D'Amore <garrett@damore.org>
2 .\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
4 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
7 .\" This notice shall appear on any product containing this material.
8 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
9 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
10 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
32 .Fa "DIR *restrict dirp"
33 .Fa "struct dirent **restrict result"
39 which is defined in the header
43 .Em directory stream ,
44 which is an ordered sequence of all the directory
45 entries in a particular directory.
46 Directory entries represent files.
47 Files can be removed from a directory or added to a directory asynchronously
55 function returns a pointer to a structure representing the directory entry at
56 the current position in the directory stream specified by the argument
58 and positions the directory stream at the next entry.
59 It returns a null pointer upon reaching the end of the directory stream.
64 header describes a directory entry.
68 function will not return directory entries containing empty names.
69 If entries for . (dot) or .. (dot-dot) exist, one entry will be returned
70 for dot and one entry will be returned for dot-dot; otherwise they will not be
73 The pointer returned by
75 points to data that can be overwritten by another call to
77 on the same directory stream.
78 These data are not overwritten by another call to
80 on a different directory stream.
82 If a file is removed from or added to the directory after the most recent call
87 whether a subsequent call to
89 returns an entry for that file is unspecified.
93 function can buffer several directory entries per actual read operation.
94 It marks for update the
96 field of the directory each time the directory is actually read.
100 either the parent or child (but not both) can
101 continue processing the directory stream using
106 If both the parent and child processes
107 use these functions, the result is undefined.
109 If the entry names a symbolic link, the value of the
111 member is unspecified.
113 Unless the end of the directory stream has been reached or an error occurred,
116 function initializes the
118 structure referenced by
120 to represent the directory entry at the current
121 position in the directory stream referred to by
123 and positions the directory stream at the next entry.
125 The caller must allocate storage pointed to by
127 to be large enough for a
129 structure with an array of
131 member containing at least
132 .Bro Dv NAME_MAX Brc Ns Li +1
137 function will not return directory entries containing empty names.
138 It is unspecified whether entries are returned for . (dot) or ..
141 If a file is removed from or added to the directory after the most recent call
146 whether a subsequent call to
148 returns an entry for that file is unspecified.
152 function can buffer several directory entries per actual read operation.
153 It marks for update the
155 field of the directory each time the directory is actually read.
159 also sets the pointer pointed to by
161 If a directory entry is returned,
162 the pointer will be set to the same value as the
164 argument; otherwise, it will be set to
169 Upon successful completion,
171 function returns a pointer to an object of type
172 .Vt "struct dirent" .
173 When an error is encountered, a null pointer is returned and
175 is set to indicate the error.
176 When the end of the directory is encountered, a null pointer is returned
182 function returns 0 if the end of the
183 directory is encountered or a directory entry is stored in the structure
186 Otherwise, an error number is returned to indicate the failure.
190 .Ss Example 1 Search the current directory for an entry.
191 The following sample program will search the current directory for each of the
192 arguments supplied on the command line:
193 .Bd -literal -offset Ds
194 #include <sys/types.h>
200 static void lookup(const char *arg)
205 if ((dirp = opendir(".")) == NULL) {
206 perror("couldn't open '.'");
212 if ((dp = readdir(dirp)) != NULL) {
213 if (strcmp(dp->d_name, arg) != 0)
216 (void) printf("found %s\en", arg);
217 (void) closedir(dirp);
220 } while (dp != NULL);
223 perror("error reading directory");
225 (void) printf("failed to find %s\en", arg);
226 (void) closedir(dirp);
230 int main(int argc, char *argv[])
233 for (i = 1; i < argc; i++)
245 functions will fail if:
248 One of the values in the structure to be returned cannot be represented
256 functions may fail if:
261 argument does not refer to an open directory stream.
263 The current position of the directory stream is invalid.
272 functions should be used in
275 .Xr closedir 3C , and
277 to examine the contents of the directory.
280 returns a null pointer both at the end of the directory and on error,
281 an application wanting to check for error situations should set
283 to 0 before calling this function.
286 is set to non-zero on return, an error occurred.
290 in a threaded application, so long as only
291 one thread reads from the directory stream at any given time.
293 These functions have transitional interfaces for 64-bit file offsets.