📄 directory.3
字号:
.\" SCCSID: @(#)directory.3 2.4 8/13/87.TH directory 3.SH Nameopendir, readdir, telldir, seekdir, rewinddir, closedir \- directory operations.SH Syntax.B #include <sys/types.h>.br.B #include <sys/dir.h>.PP.B DIR.B *opendir(\fIdirname\fP).br.B char *\fIdirname\fP;.PP.B struct direct.B *readdir(\fIdirp\fP).br.B DIR.B *\fIdirp\fP;.PP.B long.B telldir(\fIdirp\fP).br.B DIR.B *\fIdirp\fP;.PP.B void seekdir(\fIdirp, loc\fP).br.B DIR.B *\fIdirp\fP;.br.B long loc;.PP.B void rewinddir(\fIdirp\fP).br.B DIR.B *\fIdirp\fP;.PP.B int.B closedir(\fIdirp\fP).br.B DIR.B *\fIdirp\fP;.SH Description.NXR "directory keyword".NXR "opendir subroutine".NXR "readdir subroutine".NXR "telldir subroutine".NXR "seekdir subroutine".NXR "rewinddir subroutine".NXR "closedir subroutine".NXR "directory" "operations"The .PN opendirlibrary routine opens the directory named by.I filenameand associates a directory stream with it. A pointer is returned toidentify the directory stream in subsequent operations. The pointerNULL is returned if the specified.I filenamecan not be accessed, or if insufficient memory is available to open the directory file..PPThe .PN readdirroutine returns a pointer to the next directory entry. It returnsNULL upon reaching the end of the directory or on detecting an invalid.PN seekdiroperation.The .PN readdir routine uses the.PN getdirentriessystem call to read directories. Since the .PN readdirroutine returns NULL upon reaching the end of the directory or ondetecting an error, an application which wishes to detect the difference mustset errno to 0 prior to calling.PN readdir ..PPThe .PN telldirroutine returns the current location associated with the nameddirectory stream. Values returned by.PN telldirare good only for the lifetime of the DIR pointer from which they are derived.If the directory is closed and then reopened, the .PN telldirvalue may be invalidated due to undetected directory compaction..PPThe .PN seekdirroutine sets the position of the next.PN readdiroperation on the directory stream. Only values returned by.PN telldirshould be used with.PN seekdir ..PPThe .PN rewinddirroutine resets the position of the named directory stream to the beginningof the directory..PPThe .PN closedirroutine closes the named directory stream and returns a value of 0 ifsuccessful. Otherwise, a value of \-1 is returned and errno is set to indicatethe error. All resources associated with this directory stream are released..SH Examples.NXR(e) "opendir subroutine"The following sample code searches a directory for the entry .I name..EX 0len = strlen(name);dirp = opendir(".");for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { closedir(dirp); return FOUND; }closedir(dirp);return NOT_FOUND;.EE.SH EnvironmentIn the POSIX environment, the file descriptor returned inthe DIR structure after an.PN opendir()call will have the FD_CLOEXEC flag set. See <\fBfcntl.h\fP> for more detail..SH Return ValuesUpon successful completion,.PN opendir()returns a pointer to anobject of type DIR. Otherwise, a value of NULL is returnedand errno is set to indicate the error..PPThe.PN readdir()routine returns a pointer to an object of typestruct dirent upon successful completion. Otherwise, a valueof NULL is returned and errno is set to indicate the error.When the end of the directory is encountered, a value of NULL isreturned and errno is not changed..PPThe.PN telldir()routine returns the current location. No errors are defined for.PN telldir(),.PN seekdir(),and.PN rewinddir()..PPThe.PN closedir()routine returns zero upon successful completion.Otherwise, a value of \-1 is returned and errno is set to indicatethe error..SH DiagnosticsThe.PN closedir()routine will fail if:.TP 15[EBADF]The \fIdirp\fP argument does not refer to an open directory stream..TP[EINTR]The routine was interrupted by a signal..PPThe.PN opendir()routine will fail if:.TP 15[EACCES]Search permission is denied for any component of \fIdirname\fPor read permission is denied for \fIdirname\fP..TP[ENAMETOOLONG]The length of the \fIdirname\fP string exceeds {PATH_MAX}, or a pathnamecomponent is longer than {NAME_MAX}..TP[ENOENT]The \fIdirname\fP argument points to the name of a file whichdoes not exist, or to an empty string and the environmentdefined is POSIX or SYSTEM_FIVE..TP[ENOTDIR]A component of \fIdirname\fP is not a directory..TP[EMFILE]Too many file descriptors are currently open for the process..TP[ENFILE]Too many files are currently open in the system..PPThe.PN readdir()routine will fail if:.TP 15[EBADF]The \fIdirp\fP argument does not refer to an open directory stream..SH See Alsoclose(2), getdirentries(2), lseek(2), open(2), read(2), dir(5)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -