Update contrib.
1 /** @file ../include/dirent.h
5 /** @fn getdirentries(int x, char *ptr, int y, long *lptr)
6 getdirentries(int fd, char *buf, int nbytes, long *basep)
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.
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.
19 The data in the buffer is a series of dirent
21 structures each containing the following entries: u_int32_t d_fileno;
25 char d_name[MAXNAMELEN + 1];/* see below */
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.
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.
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
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.
50 /* reading directory stream using getdirenttries */
51 /* considering directory c: emp already exists */
59 char * dname="C:\ emp\
60 char * fname="C:\ emp\input.txt";
62 fd1=open(fname,O_WRONLY|O_CREAT);
65 printf("file creation failed");
68 fd=open(dname,O_RDONLY);
71 printf("directory opening failed");
74 retval = getdirentries (fd, buf,(unsigned int)sizeof (buf),&basep);
77 printf("getdirentries call failed");
81 d=(struct dirent *)buf;
83 printf("name of the file in the newly created directory is \"%s\",d-d_name);
93 name of the file in the newly created directory is "input.txt"
103 @externallyDefinedApi
106 /** @fn opendir(const char *_path)
109 Note: This description also covers the following functions -
110 readdir() readdir_r() telldir() seekdir() rewinddir() closedir() dirfd()
112 @return closedir function returns 0 on success or -1 on failure.
115 opens the directory named by _path ,
116 associates a directory stream with it
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.
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.
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.
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.
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.
144 The rewinddir function
145 resets the position of the named directory stream to the beginning of the directory.
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.
153 returns the integer file descriptor associated with the named directory stream ,
157 Sample code which searches a directory for entry ‘‘name’’ is: len = strlen(name);
159 while ((dp = readdir(dirp)) != NULL)
160 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
161 (void)closedir(dirp);
164 (void)closedir(dirp);
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.
173 #include <sys/types.h>
178 if(!(DirHandle = opendir("Test") ) )
180 printf("Failed to open directory Test\n");
183 printf("Directory Test opened \n");
189 Directory Test opened
194 The filename parameter of the opendir() function should not exceed 256 characters in length.
205 @externallyDefinedApi
208 /** @fn readdir(DIR *dp)
210 Refer to opendir() for the documentation
220 @externallyDefinedApi
224 /** @fn rewinddir(DIR *dp)
226 Refer to opendir() for the documentation
236 @externallyDefinedApi
239 /** @fn alphasort(const void *d1, const void *d2)
242 Refer to scandir() for the documentation
252 @externallyDefinedApi
255 /** @fn scandir(const char *dirname, struct dirent ***namelist, int(*)(struct dirent *), int(*)(const void *, const void *))
259 Note: This description also covers the following functions -
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.
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.
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.
277 The alphasort function
278 is a routine which can be used for the compar argument to sort the array alphabetically.
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.
285 //Illustrates how to use scandir API.
289 struct dirent **namelist;
291 // Function call to get the dir entries into the namelist.
292 n = scandir("\home\manjus\GETTEXT", &namelist;, 0, 0);
294 if(n > 0) // if scandir is successful it retuns the number of entries greater than 0
296 // print all the entries in the directory.
299 printf("dir name @ pos %d is %s",n,namelist[n]->d_name);
306 Returns -1 if the directory cannot be opened for reading or if malloc cannot allocate enough memory to hold all the data structures.
310 The dirname parameter in scandir() should not exceed 256 characters in length.
321 @externallyDefinedApi
324 /** @fn seekdir(DIR *dp, long index)
327 Refer to opendir() for the documentation
337 @externallyDefinedApi
340 /** @fn telldir(DIR *dp)
342 Refer to opendir() for the documentation
352 @externallyDefinedApi
356 /** @fn closedir(DIR *dp)
358 Refer to opendir() for the documentation
368 @externallyDefinedApi
371 /** @typedef typedef struct _dirdesc DIR
373 defines DIR data type through typedef. A type representing a directory stream.
376 @externallyDefinedApi
382 get directory stream file descriptor
390 flags for opendir2. hide whiteout entries.
399 flags for opendir2. don't return duplicate names.
408 flags for opendir2. rewind after reading union stack.
415 /** @def __DTF_READALL
417 flags for opendir2. everything has been read.