Commit | Line | Data |
---|---|---|
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 | 21 | opendir, 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 | |
74 | opens the directory named by | |
75 | .I filename | |
76 | and associates a | |
77 | .I directory stream | |
78 | with it. | |
79 | .I Opendir | |
80 | returns a pointer to be used to identify the | |
81 | .I directory stream | |
82 | in subsequent operations. The pointer | |
83 | .SM | |
84 | .B NULL | |
85 | is returned if | |
86 | .I filename | |
87 | cannot be accessed, or if it cannot | |
88 | .IR malloc (3) | |
89 | enough memory to hold the whole thing. | |
90 | .PP | |
91 | .I Readdir | |
92 | returns a pointer to the next directory entry. It returns | |
93 | .B NULL | |
94 | upon reaching the end of the directory or detecting an invalid | |
95 | .I seekdir | |
96 | operation. | |
97 | .PP | |
98 | .I Telldir | |
99 | returns the current location associated with the named | |
100 | .I directory stream. | |
101 | .PP | |
102 | .I Seekdir | |
103 | sets the position of the next | |
104 | .I readdir | |
105 | operation on the | |
106 | .I directory stream. | |
107 | The new position reverts to the one associated with the | |
108 | .I directory stream | |
109 | when the | |
110 | .I telldir | |
111 | operation was performed. Values returned by | |
112 | .I telldir | |
113 | are good only for the lifetime of the DIR pointer from which they are derived. | |
114 | If the directory is closed and then reopened, the | |
115 | .I telldir | |
116 | value may be invalidated due to undetected directory compaction. | |
117 | It is safe to use a previous | |
118 | .I telldir | |
119 | value immediately after a call to | |
120 | .I opendir | |
121 | and before any calls to | |
122 | .I readdir. | |
123 | .PP | |
124 | .I Rewinddir | |
125 | resets the position of the named | |
126 | .I directory stream | |
127 | to the beginning of the directory. | |
128 | .PP | |
129 | .I Closedir | |
130 | closes the named | |
131 | .I directory stream | |
132 | and frees the structure associated with the DIR pointer. | |
133 | .PP | |
01cc7fcc KB |
134 | .I Dirfd |
135 | returns the integer file descriptor associated with the named | |
136 | .I directory stream, | |
137 | see open(2). | |
138 | .PP | |
577ec8b7 KM |
139 | Sample 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" | |
160 | open(2), | |
161 | close(2), | |
162 | read(2), | |
163 | lseek(2), | |
164 | dir(5) |