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