1*48352Scael.\" Copyright (c) 1983, 1991 Regents of the University of California. 235309Sbostic.\" All rights reserved. 320490Smckusick.\" 443571Strent.\" %sccs.include.redist.man% 520490Smckusick.\" 6*48352Scael.\" @(#)directory.3 6.7 (Berkeley) 04/19/91 735309Sbostic.\" 8*48352Scael.Dd 9*48352Scael.Dt DIRECTORY 3 10*48352Scael.Os BSD 4.2 11*48352Scael.Sh NAME 12*48352Scael.Nm opendir , 13*48352Scael.Nm readdir , 14*48352Scael.Nm telldir , 15*48352Scael.Nm seekdir , 16*48352Scael.Nm rewinddir , 17*48352Scael.Nm closedir , 18*48352Scael.Nm dirfd 19*48352Scael.Nd directory operations 20*48352Scael.Sh SYNOPSIS 21*48352Scael.Fd #include <sys/types.h> 22*48352Scael.Fd #include <dirent.h> 23*48352Scael.Ft DIR * 24*48352Scael.Fn opendir "const char *filename" 25*48352Scael.Ft struct direct 26*48352Scael.Fn readdir "DIR *dirp" 27*48352Scael.Ft long 28*48352Scael.Fn telldir "const DIR *dirp" 29*48352Scael.Ft void 30*48352Scael.Fn seekdir "DIR *dirp" "long loc" 31*48352Scael.Ft void 32*48352Scael.Fn rewinddir "DIR *dirp" 33*48352Scael.Ft int 34*48352Scael.Fn closedir "DIR *dirp" 35*48352Scael.Ft int 36*48352Scael.Fn dirfd "DIR *dirp" 37*48352Scael.Sh DESCRIPTION 38*48352ScaelThe 39*48352Scael.Fn opendir 40*48352Scaelfunction 4120490Smckusickopens the directory named by 42*48352Scael.Fa filename , 43*48352Scaelassociates a 44*48352Scael.Em directory stream 45*48352Scaelwith it 46*48352Scaeland 4720490Smckusickreturns a pointer to be used to identify the 48*48352Scael.Em directory stream 4920490Smckusickin subsequent operations. The pointer 50*48352Scael.Dv NULL 5120490Smckusickis returned if 52*48352Scael.Fa filename 5320490Smckusickcannot be accessed, or if it cannot 54*48352Scael.Xr malloc 3 5520490Smckusickenough memory to hold the whole thing. 56*48352Scael.Pp 57*48352ScaelThe 58*48352Scael.Fn readdir 59*48352Scaelfunction 6020490Smckusickreturns a pointer to the next directory entry. It returns 61*48352Scael.Dv NULL 6220490Smckusickupon reaching the end of the directory or detecting an invalid 63*48352Scael.Fn seekdir 6420490Smckusickoperation. 65*48352Scael.Pp 66*48352ScaelThe 67*48352Scael.Fn telldir 68*48352Scaelfunction 6920490Smckusickreturns the current location associated with the named 70*48352Scael.Em directory stream . 71*48352Scael.Pp 72*48352ScaelThe 73*48352Scael.Fn seekdir 74*48352Scaelfunction 7520490Smckusicksets the position of the next 76*48352Scael.Fn readdir 7720490Smckusickoperation on the 78*48352Scael.Em directory stream . 7920490SmckusickThe new position reverts to the one associated with the 80*48352Scael.Em directory stream 8120490Smckusickwhen the 82*48352Scael.Fn telldir 8320490Smckusickoperation was performed. Values returned by 84*48352Scael.Fn telldir 85*48352Scaelare good only for the lifetime of the 86*48352Scael.Dv DIR 87*48352Scaelpointer, 88*48352Scael.Fa dirp , 89*48352Scaelfrom which they are derived. 9020490SmckusickIf the directory is closed and then reopened, the 91*48352Scael.Fn telldir 9220490Smckusickvalue may be invalidated due to undetected directory compaction. 9320490SmckusickIt is safe to use a previous 94*48352Scael.Fn telldir 9520490Smckusickvalue immediately after a call to 96*48352Scael.Fn opendir 9720490Smckusickand before any calls to 98*48352Scael.Fn readdir . 99*48352Scael.Pp 100*48352ScaelThe 101*48352Scael.Fn rewinddir 102*48352Scaelfunction 10320490Smckusickresets the position of the named 104*48352Scael.Em directory stream 10520490Smckusickto the beginning of the directory. 106*48352Scael.Pp 107*48352ScaelThe 108*48352Scael.Fn closedir 109*48352Scaelfunction 11020490Smckusickcloses the named 111*48352Scael.Em directory stream 112*48352Scaeland frees the structure associated with the 113*48352Scael.Fa dirp 114*48352Scaelpointer, 11542384Sbosticreturning 0 on success. 116*48352ScaelOn failure, \-1 is returned and the global variable 117*48352Scael.Va errno 118*48352Scaelis set to indicate the error. 119*48352Scael.Pp 120*48352ScaelThe 121*48352Scael.Fn dirfd 122*48352Scaelfunction 12330339Sbosticreturns the integer file descriptor associated with the named 124*48352Scael.Em directory stream , 125*48352Scaelsee 126*48352Scael.Xr open 2 . 127*48352Scael.Pp 12820490SmckusickSample code which searchs a directory for entry ``name'' is: 129*48352Scael.Bd -literal -offset indent 13042384Sbosticlen = strlen(name); 13142384Sbosticdirp = opendir("."); 13242384Sbosticfor (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) 13342384Sbostic if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { 13442384Sbostic (void)closedir(dirp); 13542384Sbostic return FOUND; 13642384Sbostic } 13742384Sbostic(void)closedir(dirp); 13842384Sbosticreturn NOT_FOUND; 139*48352Scael.Ed 140*48352Scael.Sh SEE ALSO 141*48352Scael.Xr open 2 , 142*48352Scael.Xr close 2 , 143*48352Scael.Xr read 2 , 144*48352Scael.Xr lseek 2 , 145*48352Scael.Xr dir 5 146*48352Scael.Sh HISTORY 147*48352ScaelThe 148*48352Scael.Fn opendir , 149*48352Scael.Fn readdir , 150*48352Scael.Fn telldir , 151*48352Scael.Fn seekdir , 152*48352Scael.Fn rewinddir , 153*48352Scael.Fn closedir , 154*48352Scaeland 155*48352Scael.Fn dirfd 156*48352Scaelfunctions appeared in 157*48352Scael.Bx 4.2 . 158