sl@0
|
1 |
/** @file ../include/dirent.h
|
sl@0
|
2 |
@internalComponent
|
sl@0
|
3 |
*/
|
sl@0
|
4 |
|
sl@0
|
5 |
/** @fn getdirentries(int x, char *ptr, int y, long *lptr)
|
sl@0
|
6 |
getdirentries(int fd, char *buf, int nbytes, long *basep)
|
sl@0
|
7 |
@param x
|
sl@0
|
8 |
@param ptr
|
sl@0
|
9 |
@param y
|
sl@0
|
10 |
@param lptr
|
sl@0
|
11 |
@return If successful, the number of bytes actually transferred is returned.
|
sl@0
|
12 |
Otherwise, -1 is returned and the global variable errno is set to indicate the error.
|
sl@0
|
13 |
|
sl@0
|
14 |
The getdirentries system call reads directory entries from the directory
|
sl@0
|
15 |
referenced by the file descriptor x into the buffer pointed to by ptr, in a file system independent format. Up to y of data will be transferred. The y argument must be greater than or equal to the block size associated
|
sl@0
|
16 |
with the file, see stat. Some file systems may not support these system
|
sl@0
|
17 |
calls with buffers smaller than this size.
|
sl@0
|
18 |
|
sl@0
|
19 |
The data in the buffer is a series of dirent
|
sl@0
|
20 |
@code
|
sl@0
|
21 |
structures each containing the following entries: u_int32_t d_fileno;
|
sl@0
|
22 |
u_int16_t d_reclen;
|
sl@0
|
23 |
u_int8_t d_type;
|
sl@0
|
24 |
u_int8_t d_namlen;
|
sl@0
|
25 |
char d_name[MAXNAMELEN + 1];/* see below */
|
sl@0
|
26 |
@endcode
|
sl@0
|
27 |
|
sl@0
|
28 |
The d_fileno entry is a number which is unique for each distinct file in
|
sl@0
|
29 |
the file system. Files that are linked by hard links (see link ) have the same d_fileno. The d_reclen entry is the length, in bytes, of the directory record. The d_type entry is the type of the file pointed to by the directory record.
|
sl@0
|
30 |
The file type values are defined in \<sys/dirent.h\>. Presently, however, file type is not supported
|
sl@0
|
31 |
and d_type is set to 0. The d_name entry contains a null terminated file name. The d_namlen entry specifies the length of the file name excluding the null
|
sl@0
|
32 |
byte. Thus the actual size of d_name may vary from 1 to MAXNAMELEN + 1.
|
sl@0
|
33 |
|
sl@0
|
34 |
Entries may be separated by extra space.
|
sl@0
|
35 |
The d_reclen entry may be used as an offset from the start of a dirent structure to the next structure, if any.
|
sl@0
|
36 |
|
sl@0
|
37 |
The actual number of bytes transferred is returned. The current position pointer
|
sl@0
|
38 |
associated with x is set to point to the next block of entries. The pointer may not
|
sl@0
|
39 |
advance by the number of bytes returned by getdirentries . A value of zero is returned when the end of the directory
|
sl@0
|
40 |
has been reached.
|
sl@0
|
41 |
|
sl@0
|
42 |
The getdirentries system call writes the position of the block read into
|
sl@0
|
43 |
the location pointed to by lptr. Alternatively, the current position pointer may be set and retrieved
|
sl@0
|
44 |
by lseek. The current position pointer should only
|
sl@0
|
45 |
be set to a value returned by lseek, a value returned in the location pointed
|
sl@0
|
46 |
to by basep ( (getdirentries); only) or zero.
|
sl@0
|
47 |
|
sl@0
|
48 |
Examples:
|
sl@0
|
49 |
@code
|
sl@0
|
50 |
/* reading directory stream using getdirenttries */
|
sl@0
|
51 |
/* considering directory c: emp already exists */
|
sl@0
|
52 |
#include <stdio.h>
|
sl@0
|
53 |
int main()
|
sl@0
|
54 |
{
|
sl@0
|
55 |
int retval;
|
sl@0
|
56 |
long basep=(off_t) 0;
|
sl@0
|
57 |
char buf[1024];
|
sl@0
|
58 |
struct dirent * d;
|
sl@0
|
59 |
char * dname="C:\ emp\
|
sl@0
|
60 |
char * fname="C:\ emp\input.txt";
|
sl@0
|
61 |
int fd,fd1;
|
sl@0
|
62 |
fd1=open(fname,O_WRONLY|O_CREAT);
|
sl@0
|
63 |
if(fd==-1)
|
sl@0
|
64 |
{
|
sl@0
|
65 |
printf("file creation failed");
|
sl@0
|
66 |
return -1;
|
sl@0
|
67 |
}
|
sl@0
|
68 |
fd=open(dname,O_RDONLY);
|
sl@0
|
69 |
if(fd==-1)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
printf("directory opening failed");
|
sl@0
|
72 |
return -1;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
retval = getdirentries (fd, buf,(unsigned int)sizeof (buf),&basep);
|
sl@0
|
75 |
if(retval == -1)
|
sl@0
|
76 |
{
|
sl@0
|
77 |
printf("getdirentries call failed");
|
sl@0
|
78 |
return -1;
|
sl@0
|
79 |
}
|
sl@0
|
80 |
|
sl@0
|
81 |
d=(struct dirent *)buf;
|
sl@0
|
82 |
|
sl@0
|
83 |
printf("name of the file in the newly created directory is \"%s\",d-d_name);
|
sl@0
|
84 |
|
sl@0
|
85 |
close(fd1);
|
sl@0
|
86 |
close(fd);
|
sl@0
|
87 |
return 0;
|
sl@0
|
88 |
}
|
sl@0
|
89 |
|
sl@0
|
90 |
@endcode
|
sl@0
|
91 |
Output
|
sl@0
|
92 |
@code
|
sl@0
|
93 |
name of the file in the newly created directory is "input.txt"
|
sl@0
|
94 |
|
sl@0
|
95 |
@endcode
|
sl@0
|
96 |
@see lseek()
|
sl@0
|
97 |
@see open()
|
sl@0
|
98 |
|
sl@0
|
99 |
|
sl@0
|
100 |
|
sl@0
|
101 |
|
sl@0
|
102 |
@publishedAll
|
sl@0
|
103 |
@externallyDefinedApi
|
sl@0
|
104 |
*/
|
sl@0
|
105 |
|
sl@0
|
106 |
/** @fn opendir(const char *_path)
|
sl@0
|
107 |
@param _path
|
sl@0
|
108 |
|
sl@0
|
109 |
Note: This description also covers the following functions -
|
sl@0
|
110 |
readdir() readdir_r() telldir() seekdir() rewinddir() closedir() dirfd()
|
sl@0
|
111 |
|
sl@0
|
112 |
@return closedir function returns 0 on success or -1 on failure.
|
sl@0
|
113 |
|
sl@0
|
114 |
The opendir function
|
sl@0
|
115 |
opens the directory named by _path ,
|
sl@0
|
116 |
associates a directory stream with it
|
sl@0
|
117 |
and
|
sl@0
|
118 |
returns a pointer to be used to identify the directory stream in subsequent operations.
|
sl@0
|
119 |
The pointer NULL is returned if filename cannot be accessed, or if it cannot malloc enough memory to hold the whole thing.
|
sl@0
|
120 |
|
sl@0
|
121 |
The readdir function
|
sl@0
|
122 |
returns a pointer to the next directory entry.
|
sl@0
|
123 |
It returns NULL upon reaching the end of the directory or detecting an invalid seekdir operation.
|
sl@0
|
124 |
|
sl@0
|
125 |
The readdir_r function
|
sl@0
|
126 |
provides the same functionality as readdir ,
|
sl@0
|
127 |
but the caller must provide a directory entry buffer to store the results in.
|
sl@0
|
128 |
If the read succeeds, result is pointed at the entry ;
|
sl@0
|
129 |
upon reaching the end of the directory result is set to NULL .
|
sl@0
|
130 |
The readdir_r function
|
sl@0
|
131 |
returns 0 on success or an error number to indicate failure.
|
sl@0
|
132 |
|
sl@0
|
133 |
The telldir function
|
sl@0
|
134 |
returns the current location associated with the named directory stream .
|
sl@0
|
135 |
Values returned by telldir are good only for the lifetime of the DIR pointer, dirp ,
|
sl@0
|
136 |
from which they are derived.
|
sl@0
|
137 |
If the directory is closed and then
|
sl@0
|
138 |
reopened, prior values returned by telldir will no longer be valid.
|
sl@0
|
139 |
|
sl@0
|
140 |
The seekdir function
|
sl@0
|
141 |
sets the position of the next readdir operation on the directory stream .
|
sl@0
|
142 |
The new position reverts to the one associated with the directory stream when the telldir operation was performed.
|
sl@0
|
143 |
|
sl@0
|
144 |
The rewinddir function
|
sl@0
|
145 |
resets the position of the named directory stream to the beginning of the directory.
|
sl@0
|
146 |
|
sl@0
|
147 |
The closedir function
|
sl@0
|
148 |
closes the named directory stream and frees the structure associated with the dirp pointer,
|
sl@0
|
149 |
returning 0 on success.
|
sl@0
|
150 |
On failure, -1 is returned and the global variable errno is set to indicate the error.
|
sl@0
|
151 |
|
sl@0
|
152 |
The dirfd function
|
sl@0
|
153 |
returns the integer file descriptor associated with the named directory stream ,
|
sl@0
|
154 |
see open .
|
sl@0
|
155 |
|
sl@0
|
156 |
@code
|
sl@0
|
157 |
Sample code which searches a directory for entry ‘‘name’’ is: len = strlen(name);
|
sl@0
|
158 |
dirp = opendir(".");
|
sl@0
|
159 |
while ((dp = readdir(dirp)) != NULL)
|
sl@0
|
160 |
if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
|
sl@0
|
161 |
(void)closedir(dirp);
|
sl@0
|
162 |
return FOUND;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
(void)closedir(dirp);
|
sl@0
|
165 |
return NOT_FOUND;
|
sl@0
|
166 |
@endcode
|
sl@0
|
167 |
|
sl@0
|
168 |
Examples:
|
sl@0
|
169 |
@code
|
sl@0
|
170 |
/* Detailed description: This test code demonstrates usage of opendir system call, open directory name test.
|
sl@0
|
171 |
Preconditions: Expects Test directory to be present in the current working directory.
|
sl@0
|
172 |
*/
|
sl@0
|
173 |
#include <sys/types.h>
|
sl@0
|
174 |
#include <dirent.h>
|
sl@0
|
175 |
int main()
|
sl@0
|
176 |
{
|
sl@0
|
177 |
DIR *DirHandle;
|
sl@0
|
178 |
if(!(DirHandle = opendir("Test") ) )
|
sl@0
|
179 |
{
|
sl@0
|
180 |
printf("Failed to open directory Test\n");
|
sl@0
|
181 |
return -1;
|
sl@0
|
182 |
}
|
sl@0
|
183 |
printf("Directory Test opened \n");
|
sl@0
|
184 |
return 0;
|
sl@0
|
185 |
}
|
sl@0
|
186 |
@endcode
|
sl@0
|
187 |
@code
|
sl@0
|
188 |
Output
|
sl@0
|
189 |
Directory Test opened
|
sl@0
|
190 |
@endcode
|
sl@0
|
191 |
|
sl@0
|
192 |
Limitations:
|
sl@0
|
193 |
|
sl@0
|
194 |
The filename parameter of the opendir() function should not exceed 256 characters in length.
|
sl@0
|
195 |
|
sl@0
|
196 |
@see close()
|
sl@0
|
197 |
@see lseek()
|
sl@0
|
198 |
@see open()
|
sl@0
|
199 |
@see read()
|
sl@0
|
200 |
|
sl@0
|
201 |
|
sl@0
|
202 |
|
sl@0
|
203 |
|
sl@0
|
204 |
@publishedAll
|
sl@0
|
205 |
@externallyDefinedApi
|
sl@0
|
206 |
*/
|
sl@0
|
207 |
|
sl@0
|
208 |
/** @fn readdir(DIR *dp)
|
sl@0
|
209 |
@param dp
|
sl@0
|
210 |
Refer to opendir() for the documentation
|
sl@0
|
211 |
@see close()
|
sl@0
|
212 |
@see lseek()
|
sl@0
|
213 |
@see open()
|
sl@0
|
214 |
@see read()
|
sl@0
|
215 |
|
sl@0
|
216 |
|
sl@0
|
217 |
|
sl@0
|
218 |
|
sl@0
|
219 |
@publishedAll
|
sl@0
|
220 |
@externallyDefinedApi
|
sl@0
|
221 |
*/
|
sl@0
|
222 |
|
sl@0
|
223 |
|
sl@0
|
224 |
/** @fn rewinddir(DIR *dp)
|
sl@0
|
225 |
@param dp
|
sl@0
|
226 |
Refer to opendir() for the documentation
|
sl@0
|
227 |
@see close()
|
sl@0
|
228 |
@see lseek()
|
sl@0
|
229 |
@see open()
|
sl@0
|
230 |
@see read()
|
sl@0
|
231 |
|
sl@0
|
232 |
|
sl@0
|
233 |
|
sl@0
|
234 |
|
sl@0
|
235 |
@publishedAll
|
sl@0
|
236 |
@externallyDefinedApi
|
sl@0
|
237 |
*/
|
sl@0
|
238 |
|
sl@0
|
239 |
/** @fn alphasort(const void *d1, const void *d2)
|
sl@0
|
240 |
@param d1
|
sl@0
|
241 |
@param d2
|
sl@0
|
242 |
Refer to scandir() for the documentation
|
sl@0
|
243 |
@see directory()
|
sl@0
|
244 |
@see malloc()
|
sl@0
|
245 |
@see qsort()
|
sl@0
|
246 |
@see dir()
|
sl@0
|
247 |
|
sl@0
|
248 |
|
sl@0
|
249 |
|
sl@0
|
250 |
|
sl@0
|
251 |
@publishedAll
|
sl@0
|
252 |
@externallyDefinedApi
|
sl@0
|
253 |
*/
|
sl@0
|
254 |
|
sl@0
|
255 |
/** @fn scandir(const char *dirname, struct dirent ***namelist, int(*)(struct dirent *), int(*)(const void *, const void *))
|
sl@0
|
256 |
@param dirname
|
sl@0
|
257 |
@param namelist
|
sl@0
|
258 |
|
sl@0
|
259 |
Note: This description also covers the following functions -
|
sl@0
|
260 |
alphasort()
|
sl@0
|
261 |
|
sl@0
|
262 |
The scandir function
|
sl@0
|
263 |
reads the directory dirname and builds an array of pointers to directory
|
sl@0
|
264 |
entries using malloc It returns the number of entries in the array.
|
sl@0
|
265 |
A pointer to the array of directory entries is stored in the location
|
sl@0
|
266 |
referenced by namelist.
|
sl@0
|
267 |
|
sl@0
|
268 |
The select argument is a pointer to a user supplied subroutine which is called by scandir to select which entries are to be included in the array.
|
sl@0
|
269 |
The select routine is passed a
|
sl@0
|
270 |
pointer to a directory entry and should return a non-zero
|
sl@0
|
271 |
value if the directory entry is to be included in the array.
|
sl@0
|
272 |
If select is null, then all the directory entries will be included.
|
sl@0
|
273 |
|
sl@0
|
274 |
The compar argument is a pointer to a user supplied subroutine which is passed to qsort to sort the completed array.
|
sl@0
|
275 |
If this pointer is null, the array is not sorted.
|
sl@0
|
276 |
|
sl@0
|
277 |
The alphasort function
|
sl@0
|
278 |
is a routine which can be used for the compar argument to sort the array alphabetically.
|
sl@0
|
279 |
|
sl@0
|
280 |
The memory allocated for the array can be deallocated with free ,
|
sl@0
|
281 |
by freeing each pointer in the array and then the array itself.
|
sl@0
|
282 |
|
sl@0
|
283 |
Examples:
|
sl@0
|
284 |
@code
|
sl@0
|
285 |
//Illustrates how to use scandir API.
|
sl@0
|
286 |
#include <dirent.h>
|
sl@0
|
287 |
Void scandirTest()
|
sl@0
|
288 |
{
|
sl@0
|
289 |
struct dirent **namelist;
|
sl@0
|
290 |
int n;
|
sl@0
|
291 |
// Function call to get the dir entries into the namelist.
|
sl@0
|
292 |
n = scandir("\home\manjus\GETTEXT", &namelist;, 0, 0);
|
sl@0
|
293 |
|
sl@0
|
294 |
if(n > 0) // if scandir is successful it retuns the number of entries greater than 0
|
sl@0
|
295 |
{
|
sl@0
|
296 |
// print all the entries in the directory.
|
sl@0
|
297 |
while(n--)
|
sl@0
|
298 |
{
|
sl@0
|
299 |
printf("dir name @ pos %d is %s",n,namelist[n]->d_name);
|
sl@0
|
300 |
}
|
sl@0
|
301 |
}
|
sl@0
|
302 |
}
|
sl@0
|
303 |
|
sl@0
|
304 |
@endcode
|
sl@0
|
305 |
Diagnostics:
|
sl@0
|
306 |
Returns -1 if the directory cannot be opened for reading or if malloc cannot allocate enough memory to hold all the data structures.
|
sl@0
|
307 |
|
sl@0
|
308 |
Limitations:
|
sl@0
|
309 |
|
sl@0
|
310 |
The dirname parameter in scandir() should not exceed 256 characters in length.
|
sl@0
|
311 |
|
sl@0
|
312 |
@see directory()
|
sl@0
|
313 |
@see malloc()
|
sl@0
|
314 |
@see qsort()
|
sl@0
|
315 |
@see dir()
|
sl@0
|
316 |
|
sl@0
|
317 |
|
sl@0
|
318 |
|
sl@0
|
319 |
|
sl@0
|
320 |
@publishedAll
|
sl@0
|
321 |
@externallyDefinedApi
|
sl@0
|
322 |
*/
|
sl@0
|
323 |
|
sl@0
|
324 |
/** @fn seekdir(DIR *dp, long index)
|
sl@0
|
325 |
@param dp
|
sl@0
|
326 |
@param index
|
sl@0
|
327 |
Refer to opendir() for the documentation
|
sl@0
|
328 |
@see close()
|
sl@0
|
329 |
@see lseek()
|
sl@0
|
330 |
@see open()
|
sl@0
|
331 |
@see read()
|
sl@0
|
332 |
|
sl@0
|
333 |
|
sl@0
|
334 |
|
sl@0
|
335 |
|
sl@0
|
336 |
@publishedAll
|
sl@0
|
337 |
@externallyDefinedApi
|
sl@0
|
338 |
*/
|
sl@0
|
339 |
|
sl@0
|
340 |
/** @fn telldir(DIR *dp)
|
sl@0
|
341 |
@param dp
|
sl@0
|
342 |
Refer to opendir() for the documentation
|
sl@0
|
343 |
@see close()
|
sl@0
|
344 |
@see lseek()
|
sl@0
|
345 |
@see open()
|
sl@0
|
346 |
@see read()
|
sl@0
|
347 |
|
sl@0
|
348 |
|
sl@0
|
349 |
|
sl@0
|
350 |
|
sl@0
|
351 |
@publishedAll
|
sl@0
|
352 |
@externallyDefinedApi
|
sl@0
|
353 |
*/
|
sl@0
|
354 |
|
sl@0
|
355 |
|
sl@0
|
356 |
/** @fn closedir(DIR *dp)
|
sl@0
|
357 |
@param dp
|
sl@0
|
358 |
Refer to opendir() for the documentation
|
sl@0
|
359 |
@see close()
|
sl@0
|
360 |
@see lseek()
|
sl@0
|
361 |
@see open()
|
sl@0
|
362 |
@see read()
|
sl@0
|
363 |
|
sl@0
|
364 |
|
sl@0
|
365 |
|
sl@0
|
366 |
|
sl@0
|
367 |
@publishedAll
|
sl@0
|
368 |
@externallyDefinedApi
|
sl@0
|
369 |
*/
|
sl@0
|
370 |
|
sl@0
|
371 |
/** @typedef typedef struct _dirdesc DIR
|
sl@0
|
372 |
|
sl@0
|
373 |
defines DIR data type through typedef. A type representing a directory stream.
|
sl@0
|
374 |
|
sl@0
|
375 |
@publishedAll
|
sl@0
|
376 |
@externallyDefinedApi
|
sl@0
|
377 |
*/
|
sl@0
|
378 |
|
sl@0
|
379 |
|
sl@0
|
380 |
/** @def dirfd(dirp)
|
sl@0
|
381 |
|
sl@0
|
382 |
get directory stream file descriptor
|
sl@0
|
383 |
|
sl@0
|
384 |
@publishedAll
|
sl@0
|
385 |
@released
|
sl@0
|
386 |
*/
|
sl@0
|
387 |
|
sl@0
|
388 |
/** @def DTF_HIDEW
|
sl@0
|
389 |
|
sl@0
|
390 |
flags for opendir2. hide whiteout entries.
|
sl@0
|
391 |
|
sl@0
|
392 |
@publishedAll
|
sl@0
|
393 |
@released
|
sl@0
|
394 |
*/
|
sl@0
|
395 |
|
sl@0
|
396 |
|
sl@0
|
397 |
/** @def DTF_NODUP
|
sl@0
|
398 |
|
sl@0
|
399 |
flags for opendir2. don't return duplicate names.
|
sl@0
|
400 |
|
sl@0
|
401 |
@publishedAll
|
sl@0
|
402 |
@released
|
sl@0
|
403 |
*/
|
sl@0
|
404 |
|
sl@0
|
405 |
|
sl@0
|
406 |
/** @def DTF_REWIND
|
sl@0
|
407 |
|
sl@0
|
408 |
flags for opendir2. rewind after reading union stack.
|
sl@0
|
409 |
|
sl@0
|
410 |
@publishedAll
|
sl@0
|
411 |
@released
|
sl@0
|
412 |
*/
|
sl@0
|
413 |
|
sl@0
|
414 |
|
sl@0
|
415 |
/** @def __DTF_READALL
|
sl@0
|
416 |
|
sl@0
|
417 |
flags for opendir2. everything has been read.
|
sl@0
|
418 |
|
sl@0
|
419 |
@publishedAll
|
sl@0
|
420 |
@released
|
sl@0
|
421 |
*/
|
sl@0
|
422 |
|
sl@0
|
423 |
|