1.\" Copyright (c) 1983, 1991 Regents of the University of California. 2.\" 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.\" from: @(#)directory.3 6.7 (Berkeley) 4/19/91 33.\" $Id: directory.3,v 1.2 1993/07/30 08:35:39 mycroft Exp $ 34.\" 35.Dd April 19, 1991 36.Dt DIRECTORY 3 37.Os BSD 4.2 38.Sh NAME 39.Nm opendir , 40.Nm readdir , 41.Nm telldir , 42.Nm seekdir , 43.Nm rewinddir , 44.Nm closedir , 45.Nm dirfd 46.Nd directory operations 47.Sh SYNOPSIS 48.Fd #include <sys/types.h> 49.Fd #include <dirent.h> 50.Ft DIR * 51.Fn opendir "const char *filename" 52.Ft struct direct 53.Fn readdir "DIR *dirp" 54.Ft long 55.Fn telldir "const DIR *dirp" 56.Ft void 57.Fn seekdir "DIR *dirp" "long loc" 58.Ft void 59.Fn rewinddir "DIR *dirp" 60.Ft int 61.Fn closedir "DIR *dirp" 62.Ft int 63.Fn dirfd "DIR *dirp" 64.Sh DESCRIPTION 65The 66.Fn opendir 67function 68opens the directory named by 69.Fa filename , 70associates a 71.Em directory stream 72with it 73and 74returns a pointer to be used to identify the 75.Em directory stream 76in subsequent operations. The pointer 77.Dv NULL 78is returned if 79.Fa filename 80cannot be accessed, or if it cannot 81.Xr malloc 3 82enough memory to hold the whole thing. 83.Pp 84The 85.Fn readdir 86function 87returns a pointer to the next directory entry. It returns 88.Dv NULL 89upon reaching the end of the directory or detecting an invalid 90.Fn seekdir 91operation. 92.Pp 93The 94.Fn telldir 95function 96returns the current location associated with the named 97.Em directory stream . 98.Pp 99The 100.Fn seekdir 101function 102sets the position of the next 103.Fn readdir 104operation on the 105.Em directory stream . 106The new position reverts to the one associated with the 107.Em directory stream 108when the 109.Fn telldir 110operation was performed. Values returned by 111.Fn telldir 112are good only for the lifetime of the 113.Dv DIR 114pointer, 115.Fa dirp , 116from which they are derived. 117If the directory is closed and then reopened, the 118.Fn telldir 119value may be invalidated due to undetected directory compaction. 120It is safe to use a previous 121.Fn telldir 122value immediately after a call to 123.Fn opendir 124and before any calls to 125.Fn readdir . 126.Pp 127The 128.Fn rewinddir 129function 130resets the position of the named 131.Em directory stream 132to the beginning of the directory. 133.Pp 134The 135.Fn closedir 136function 137closes the named 138.Em directory stream 139and frees the structure associated with the 140.Fa dirp 141pointer, 142returning 0 on success. 143On failure, \-1 is returned and the global variable 144.Va errno 145is set to indicate the error. 146.Pp 147The 148.Fn dirfd 149function 150returns the integer file descriptor associated with the named 151.Em directory stream , 152see 153.Xr open 2 . 154.Pp 155Sample code which searchs a directory for entry ``name'' is: 156.Bd -literal -offset indent 157len = strlen(name); 158dirp = opendir("."); 159for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) 160 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { 161 (void)closedir(dirp); 162 return FOUND; 163 } 164(void)closedir(dirp); 165return NOT_FOUND; 166.Ed 167.Sh SEE ALSO 168.Xr open 2 , 169.Xr close 2 , 170.Xr read 2 , 171.Xr lseek 2 , 172.Xr dir 5 173.Sh HISTORY 174The 175.Fn opendir , 176.Fn readdir , 177.Fn telldir , 178.Fn seekdir , 179.Fn rewinddir , 180.Fn closedir , 181and 182.Fn dirfd 183functions appeared in 184.Bx 4.2 . 185