add Berkeley specific copyright notices
[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.\"
25aac6e1
KB
4.\" Redistribution and use in source and binary forms are permitted
5.\" provided that the above copyright notice and this paragraph are
6.\" duplicated in all such forms and that any documentation,
7.\" advertising materials, and other materials related to such
8.\" distribution and use acknowledge that the software was developed
9.\" by the University of California, Berkeley. The name of the
10.\" University may not be used to endorse or promote products derived
11.\" from this software without specific prior written permission.
12.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14.\" WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15.\"
16.\" @(#)directory.3 6.4 (Berkeley) %G%
577ec8b7 17.\"
600e5e8e 18.TH DIRECTORY 3 ""
577ec8b7
KM
19.UC 5
20.SH NAME
01cc7fcc 21opendir, readdir, telldir, seekdir, rewinddir, closedir, dirfd \- directory operations
577ec8b7 22.SH SYNOPSIS
600e5e8e 23.B #include <sys/types.h>
70086ecd 24.br
577ec8b7
KM
25.B #include <sys/dir.h>
26.PP
27.SM
28.B DIR
29.B *opendir(filename)
30.br
31.B char *filename;
32.PP
33.B struct direct
34.B *readdir(dirp)
35.br
36.SM
37.B DIR
38.B *dirp;
39.PP
40.B long
41.B telldir(dirp)
42.br
43.SM
44.B DIR
45.B *dirp;
46.PP
47.B seekdir(dirp, loc)
48.br
49.SM
50.B DIR
51.B *dirp;
52.br
53.B long loc;
54.PP
55.B rewinddir(dirp)
56.br
57.SM
58.B DIR
59.B *dirp;
60.PP
61.B closedir(dirp)
62.br
63.SM
64.B DIR
65.B *dirp;
01cc7fcc
KB
66.PP
67.B dirfd(dirp)
68.br
69.SM
70.B DIR
71.B *dirp;
577ec8b7
KM
72.SH DESCRIPTION
73.I Opendir
74opens the directory named by
75.I filename
76and associates a
77.I directory stream
78with it.
79.I Opendir
80returns a pointer to be used to identify the
81.I directory stream
82in subsequent operations. The pointer
83.SM
84.B NULL
85is returned if
86.I filename
87cannot be accessed, or if it cannot
88.IR malloc (3)
89enough memory to hold the whole thing.
90.PP
91.I Readdir
92returns a pointer to the next directory entry. It returns
93.B NULL
94upon reaching the end of the directory or detecting an invalid
95.I seekdir
96operation.
97.PP
98.I Telldir
99returns the current location associated with the named
100.I directory stream.
101.PP
102.I Seekdir
103sets the position of the next
104.I readdir
105operation on the
106.I directory stream.
107The new position reverts to the one associated with the
108.I directory stream
109when the
110.I telldir
111operation was performed. Values returned by
112.I telldir
113are good only for the lifetime of the DIR pointer from which they are derived.
114If the directory is closed and then reopened, the
115.I telldir
116value may be invalidated due to undetected directory compaction.
117It is safe to use a previous
118.I telldir
119value immediately after a call to
120.I opendir
121and before any calls to
122.I readdir.
123.PP
124.I Rewinddir
125resets the position of the named
126.I directory stream
127to the beginning of the directory.
128.PP
129.I Closedir
130closes the named
131.I directory stream
132and frees the structure associated with the DIR pointer.
133.PP
01cc7fcc
KB
134.I Dirfd
135returns the integer file descriptor associated with the named
136.I directory stream,
137see open(2).
138.PP
577ec8b7
KM
139Sample code which searchs a directory for entry ``name'' is:
140.PP
141.br
142 len = strlen(name);
143.br
144 dirp = opendir(".");
145.br
146 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
147.br
148 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
149.br
150 closedir(dirp);
151.br
152 return FOUND;
153.br
154 }
155.br
156 closedir(dirp);
157.br
158 return NOT_FOUND;
159.SH "SEE ALSO"
160open(2),
161close(2),
162read(2),
163lseek(2),
164dir(5)