Make unvis() have more reasonable argument types.
[unix-history] / usr / src / lib / libc / gen / directory.3
CommitLineData
577ec8b7 1.\" Copyright (c) 1983 Regents of the University of California.
25aac6e1 2.\" All rights reserved.
577ec8b7 3.\"
91cff1e1 4.\" %sccs.include.redist.man%
25aac6e1 5.\"
91cff1e1 6.\" @(#)directory.3 6.6 (Berkeley) %G%
577ec8b7 7.\"
600e5e8e 8.TH DIRECTORY 3 ""
577ec8b7
KM
9.UC 5
10.SH NAME
01cc7fcc 11opendir, readdir, telldir, seekdir, rewinddir, closedir, dirfd \- directory operations
577ec8b7 12.SH SYNOPSIS
b5870130
KB
13.nf
14.ft B
15#include <sys/types.h>
16#include <dirent.h>
17
18DIR *
19opendir(const char *filename);
20
21struct direct
22*readdir(DIR * dirp);
23
24long
25telldir(const DIR *dirp);
26
27void
28seekdir(DIR *dirp, long loc);
29
30void
31rewinddir(DIR *dirp);
32
33int
34closedir(DIR *dirp);
35
36int
37dirfd(DIR *dirp)
38.ft R
39.fi
577ec8b7
KM
40.SH DESCRIPTION
41.I Opendir
42opens the directory named by
43.I filename
44and associates a
45.I directory stream
46with it.
47.I Opendir
48returns a pointer to be used to identify the
49.I directory stream
50in subsequent operations. The pointer
51.SM
52.B NULL
53is returned if
54.I filename
55cannot be accessed, or if it cannot
56.IR malloc (3)
57enough memory to hold the whole thing.
58.PP
59.I Readdir
60returns a pointer to the next directory entry. It returns
61.B NULL
62upon reaching the end of the directory or detecting an invalid
63.I seekdir
64operation.
65.PP
66.I Telldir
67returns the current location associated with the named
68.I directory stream.
69.PP
70.I Seekdir
71sets the position of the next
72.I readdir
73operation on the
74.I directory stream.
75The new position reverts to the one associated with the
76.I directory stream
77when the
78.I telldir
79operation was performed. Values returned by
80.I telldir
81are good only for the lifetime of the DIR pointer from which they are derived.
82If the directory is closed and then reopened, the
83.I telldir
84value may be invalidated due to undetected directory compaction.
85It is safe to use a previous
86.I telldir
87value immediately after a call to
88.I opendir
89and before any calls to
90.I readdir.
91.PP
92.I Rewinddir
93resets the position of the named
94.I directory stream
95to the beginning of the directory.
96.PP
97.I Closedir
98closes the named
99.I directory stream
b5870130
KB
100and frees the structure associated with the DIR pointer,
101returning 0 on success.
102On failure, -1 is returned and errno is set to indicate the error.
577ec8b7 103.PP
01cc7fcc
KB
104.I Dirfd
105returns the integer file descriptor associated with the named
106.I directory stream,
107see open(2).
108.PP
577ec8b7
KM
109Sample code which searchs a directory for entry ``name'' is:
110.PP
b5870130
KB
111.nf
112.RS
113len = strlen(name);
114dirp = opendir(".");
115for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
116 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
117 (void)closedir(dirp);
118 return FOUND;
119 }
120(void)closedir(dirp);
121return NOT_FOUND;
122.RE
123.fi
577ec8b7 124.SH "SEE ALSO"
b5870130 125open(2), close(2), read(2), lseek(2), dir(5)