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