svcadm(1M)을 검색하려면 섹션에서 1M 을 선택하고, 맨 페이지 이름에 svcadm을 입력하고 검색을 누른다.
readdir(3c)
Standard C Library Functions readdir(3C)
NAME
readdir, readdir_r - read directory
SYNOPSIS
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
struct dirent **restrict result);
POSIX.1c Draft 6
cc [ flag... ] file... -D__USE_DRAFT6_PROTOTYPES__ [ library... ]
struct dirent *readdir_r(DIR *dirp, struct dirent *entry);
DESCRIPTION
The type DIR, which is defined in the header <dirent.h>, represents a
directory stream, which is an ordered sequence of all the directory
entries in a particular directory. Directory entries represent files.
Files can be removed from a directory or added to a directory asyn‐
chronously to the operation of readdir() and readdir_r().
readdir()
The readdir() function returns a pointer to a structure representing
the directory entry at the current position in the directory stream
specified by the argument dirp, and positions the directory stream at
the next entry. It returns a null pointer upon reaching the end of the
directory stream. The structure dirent defined by the <dirent.h> header
describes a directory entry.
The readdir() function will not return directory entries containing
empty names. If entries for . (dot) or .. (dot-dot) exist, one entry
will be returned for dot and one entry will be returned for dot-dot;
otherwise they will not be returned.
The pointer returned by readdir() points to data that can be overwrit‐
ten by another call to readdir() on the same directory stream. These
data are not overwritten by another call to readdir() on a different
directory stream.
If a file is removed from or added to the directory after the most
recent call to opendir(3C) or rewinddir(3C), whether a subsequent call
to readdir() returns an entry for that file is unspecified.
The readdir() function can buffer several directory entries per actual
read operation. It marks for update the st_atime field of the directory
each time the directory is actually read.
After a call to fork(2), either the parent or child (but not both) can
continue processing the directory stream using readdir(), rewinddir()
or seekdir(3C). If both the parent and child processes use these func‐
tions, the result is undefined.
If the entry names a symbolic link, the value of the d_ino member is
unspecified.
readdir_r()
Unless the end of the directory stream has been reached or an error
occurred, the readdir_r() function initializes the dirent structure
referenced by entry to represent the directory entry at the current
position in the directory stream referred to by dirp, and positions the
directory stream at the next entry.
The caller must allocate storage pointed to by entry to be large enough
for a dirent structure with an array of char d_name member containing
at least NAME_MAX (that is, pathconf(directory, _PC_NAME_MAX)) plus one
elements. (_PC_NAME_MAX is defined in <unistd.h>.)
The readdir_r() function will not return directory entries containing
empty names. It is unspecified whether entries are returned for . (dot)
or .. (dot-dot).
If a file is removed from or added to the directory after the most
recent call to opendir() or rewinddir(), whether a subsequent call to
readdir_r() returns an entry for that file is unspecified.
The readdir_r() function can buffer several directory entries per
actual read operation. It marks for update the st_atime field of the
directory each time the directory is actually read.
The readdir_r() function performs all of the actions described above
and sets the pointer pointed to by result. If a directory entry is
returned, the pointer will be set to the same value as the entry argu‐
ment; otherwise, it will be set to NULL.
RETURN VALUES
Upon successful completion, readdir() returns a pointer to an object of
type struct dirent. When an error is encountered, a null pointer is
returned and errno is set to indicate the error. When the end of the
directory is encountered, a null pointer is returned and errno is not
changed.
Upon successful completion, readdir_r() sets result to a pointer to an
object of type struct dirent and returns 0. Otherwise, an error number
is returned to indicate the failure; errno is not set. When the end of
the directory is encountered, a null pointer is stored in result and 0
is returned.
ERRORS
The readdir() and readdir_r() functions will fail if:
EOVERFLOW One of the values in the structure to be returned cannot
be represented correctly.
The readdir() and readdir_r() functions may fail if:
EBADF The dirp argument does not refer to an open directory stream.
ENOENT The current position of the directory stream is invalid.
USAGE
The readdir() and readdir_r() functions should be used in conjunction
with opendir(), closedir(), and rewinddir() to examine the contents of
the directory. Since readdir() returns a null pointer both at the end
of the directory and on error, an application wanting to check for
error situations should set errno to 0 before calling this function. If
errno is set to non-zero on return, an error occurred.
It is safe to use readdir() in a threaded application, so long as only
one thread reads from the directory stream at any given time. The read‐
dir() function is generally preferred over the readdir_r() function.
The readdir_r() function returns the error number if an error occurred.
It returns 0 on success (including reaching the end of the directory
stream).
The readdir() and readdir_r() functions have transitional interfaces
for 64-bit file offsets. See lf64(7).
EXAMPLES
Example 1 Search the current directory for the entry name.
The following sample program will search the current directory for each
of the arguments supplied on the command line:
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <strings.h>
static void lookup(const char *arg)
{
DIR *dirp;
struct dirent *dp;
if ((dirp = opendir(".")) == NULL) {
perror("couldn't open '.'");
return;
}
do {
errno = 0;
if ((dp = readdir(dirp)) != NULL) {
if (strcmp(dp->d_name, arg) != 0)
continue;
(void) printf("found %s\n", arg);
(void) closedir(dirp);
return;
}
} while (dp != NULL);
if (errno != 0)
perror("error reading directory");
else
(void) printf("failed to find %s\n", arg);
(void) closedir(dirp);
return;
}
int main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++)
lookup(argv[i]);
return (0);
}
ATTRIBUTES
See attributes(7) for descriptions of the following attributes:
tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) ATTRIBUTE TYPEAT‐
TRIBUTE VALUE _ Interface StabilityCommitted _ MT-LevelSee below. _
StandardSee standards(7).
The readdir() function is Safe when used by a single thread on a given
DIR pointer (that is, opendir(), readdir(), ..., closedir()), and is
the recommended usage. The readdir_r() function is Safe to be used by
multiple threads on a given DIR pointer.
SEE ALSO
fork(2), lstat(2), symlink(2), closedir(3C), dirfd(3C), opendir(3C),
rewinddir(3C), scandir(3C), seekdir(3C), telldir(3C), attributes(7),
lf64(7), standards(7)
NOTES
Prior to Oracle Solaris 11.4, the default compilation environment pro‐
vided a definition of the readdir_r() function as specified in POSIX.1c
Draft 6. The final POSIX.1c standard changed the interface for read‐
dir_r(). To allow applications that were written to use the obsolete
Draft-6 interfaces to continue to be compiled and run, the
__USE_DRAFT6_PROTOTYPES__ macro must be defined:
cc -D__USE_DRAFT6_PROTOTYPES__ ...
Support for the Draft-6 interfaces is provided for source compatibility
only and might not be supported in future releases. Old applications
should be converted to use the standard definitions.
Oracle Solaris 11.4 17 Aug 2018 readdir(3C)