1 .\" $NetBSD: directory.3,v 1.26.28.1 2009/01/04 17:02:19 christos Exp $
3 .\" Copyright (c) 1983, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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
30 .\" @(#)directory.3 8.1 (Berkeley) 6/4/93
45 .Nd directory operations
51 .Fn fdopendir "int fd"
53 .Fn opendir "const char *filename"
55 .Fn readdir "DIR *dirp"
57 .Fn readdir_r "DIR * restrict dirp" "struct dirent * restrict entry" "struct dirent ** restrict result"
59 .Fn telldir "DIR *dirp"
61 .Fn seekdir "DIR *dirp" "long loc"
63 .Fn rewinddir "DIR *dirp"
65 .Fn closedir "DIR *dirp"
71 function opens the directory named by
81 with the directory file descriptor
85 must not be used further by the caller in any way.
87 Both functions return a pointer to be used to identify the
89 in subsequent operations.
94 cannot be accessed, or if it cannot
96 enough memory to hold the whole thing.
101 returns a pointer to the next directory entry.
104 upon reaching the end of the directory or detecting an invalid
111 provides the same functionality as
113 but the caller must provide a directory
115 buffer to store the results in.
116 If the read succeeds,
120 upon reaching the end of the directory
127 returns 0 on success or an error number to indicate failure.
132 returns the current location associated with the named
133 .Em directory stream .
138 sets the position of the next
141 .Em directory stream .
142 The new position reverts to the one associated with the
146 operation was performed.
149 are good only for the lifetime of the
153 from which they are derived.
154 If the directory is closed and then reopened, the
156 value cannot be re-used.
161 resets the position of the named
163 to the beginning of the directory.
170 and frees the structure associated with the
173 returning 0 on success.
174 On failure, \-1 is returned and the global variable
176 is set to indicate the error.
181 returns the integer file descriptor associated with the named
182 .Em directory stream ,
186 Sample code which searches a directory for entry
189 .Bd -literal -offset indent
193 while ((dp = readdir(dirp)) != NULL)
194 if (dp-\*[Gt]d_namlen == len \*[Am]\*[Am]
195 !strcmp(dp-\*[Gt]d_name, name)) {
196 (void)closedir(dirp);
199 (void)closedir(dirp);
228 functions appeared in