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