xref: /csrg-svn/lib/libc/gen/directory.3 (revision 61111)
1*61111Sbostic.\" Copyright (c) 1983, 1991, 1993
2*61111Sbostic.\"	The Regents of the University of California.  All rights reserved.
320490Smckusick.\"
443571Strent.\" %sccs.include.redist.man%
520490Smckusick.\"
6*61111Sbostic.\"     @(#)directory.3	8.1 (Berkeley) 06/04/93
735309Sbostic.\"
848352Scael.Dd
948352Scael.Dt DIRECTORY 3
1048352Scael.Os BSD 4.2
1148352Scael.Sh NAME
1248352Scael.Nm opendir ,
1348352Scael.Nm readdir ,
1448352Scael.Nm telldir ,
1548352Scael.Nm seekdir ,
1648352Scael.Nm rewinddir ,
1748352Scael.Nm closedir ,
1848352Scael.Nm dirfd
1948352Scael.Nd directory operations
2048352Scael.Sh SYNOPSIS
2148352Scael.Fd #include <sys/types.h>
2248352Scael.Fd #include <dirent.h>
2348352Scael.Ft DIR *
2448352Scael.Fn opendir "const char *filename"
2560025Sbostic.Ft struct dirent *
2648352Scael.Fn readdir "DIR *dirp"
2748352Scael.Ft long
2848352Scael.Fn telldir "const DIR *dirp"
2948352Scael.Ft void
3048352Scael.Fn seekdir "DIR *dirp" "long  loc"
3148352Scael.Ft void
3248352Scael.Fn rewinddir "DIR *dirp"
3348352Scael.Ft int
3448352Scael.Fn closedir "DIR *dirp"
3548352Scael.Ft int
3648352Scael.Fn dirfd "DIR *dirp"
3748352Scael.Sh DESCRIPTION
3848352ScaelThe
3948352Scael.Fn opendir
4048352Scaelfunction
4120490Smckusickopens the directory named by
4248352Scael.Fa filename ,
4348352Scaelassociates a
4448352Scael.Em directory stream
4548352Scaelwith it
4648352Scaeland
4720490Smckusickreturns a pointer to be used to identify the
4848352Scael.Em directory stream
4920490Smckusickin subsequent operations.  The pointer
5048352Scael.Dv NULL
5120490Smckusickis returned if
5248352Scael.Fa filename
5320490Smckusickcannot be accessed, or if it cannot
5448352Scael.Xr malloc 3
5520490Smckusickenough memory to hold the whole thing.
5648352Scael.Pp
5748352ScaelThe
5848352Scael.Fn readdir
5948352Scaelfunction
6020490Smckusickreturns a pointer to the next directory entry.  It returns
6148352Scael.Dv NULL
6220490Smckusickupon reaching the end of the directory or detecting an invalid
6348352Scael.Fn seekdir
6420490Smckusickoperation.
6548352Scael.Pp
6648352ScaelThe
6748352Scael.Fn telldir
6848352Scaelfunction
6920490Smckusickreturns the current location associated with the named
7048352Scael.Em directory stream .
7148352Scael.Pp
7248352ScaelThe
7348352Scael.Fn seekdir
7448352Scaelfunction
7520490Smckusicksets the position of the next
7648352Scael.Fn readdir
7720490Smckusickoperation on the
7848352Scael.Em directory stream .
7920490SmckusickThe new position reverts to the one associated with the
8048352Scael.Em directory stream
8120490Smckusickwhen the
8248352Scael.Fn telldir
8320490Smckusickoperation was performed.  Values returned by
8448352Scael.Fn telldir
8548352Scaelare good only for the lifetime of the
8648352Scael.Dv DIR
8748352Scaelpointer,
8848352Scael.Fa dirp ,
8948352Scaelfrom which they are derived.
9020490SmckusickIf the directory is closed and then reopened, the
9148352Scael.Fn telldir
9220490Smckusickvalue may be invalidated due to undetected directory compaction.
9320490SmckusickIt is safe to use a previous
9448352Scael.Fn telldir
9520490Smckusickvalue immediately after a call to
9648352Scael.Fn opendir
9720490Smckusickand before any calls to
9848352Scael.Fn readdir .
9948352Scael.Pp
10048352ScaelThe
10148352Scael.Fn rewinddir
10248352Scaelfunction
10320490Smckusickresets the position of the named
10448352Scael.Em directory stream
10520490Smckusickto the beginning of the directory.
10648352Scael.Pp
10748352ScaelThe
10848352Scael.Fn closedir
10948352Scaelfunction
11020490Smckusickcloses the named
11148352Scael.Em directory stream
11248352Scaeland frees the structure associated with the
11348352Scael.Fa dirp
11448352Scaelpointer,
11542384Sbosticreturning 0 on success.
11648352ScaelOn failure, \-1 is returned and the global variable
11748352Scael.Va errno
11848352Scaelis set to indicate the error.
11948352Scael.Pp
12048352ScaelThe
12148352Scael.Fn dirfd
12248352Scaelfunction
12330339Sbosticreturns the integer file descriptor associated with the named
12448352Scael.Em directory stream ,
12548352Scaelsee
12648352Scael.Xr open 2 .
12748352Scael.Pp
12820490SmckusickSample code which searchs a directory for entry ``name'' is:
12948352Scael.Bd -literal -offset indent
13042384Sbosticlen = strlen(name);
13142384Sbosticdirp = opendir(".");
13257575Sbosticwhile ((dp = readdir(dirp)) != NULL)
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;
13948352Scael.Ed
14048352Scael.Sh SEE ALSO
14148352Scael.Xr open 2 ,
14248352Scael.Xr close 2 ,
14348352Scael.Xr read 2 ,
14448352Scael.Xr lseek 2 ,
14548352Scael.Xr dir 5
14648352Scael.Sh HISTORY
14748352ScaelThe
14848352Scael.Fn opendir ,
14948352Scael.Fn readdir ,
15048352Scael.Fn telldir ,
15148352Scael.Fn seekdir ,
15248352Scael.Fn rewinddir ,
15348352Scael.Fn closedir ,
15448352Scaeland
15548352Scael.Fn dirfd
15648352Scaelfunctions appeared in
15748352Scael.Bx 4.2 .
158