Node:Scanning Directory Content, Next:Simple Directory Lister Mark II, Previous:Random Access Directory, Up:Accessing Directories
A higher-level interface to the directory handling functions is the
scandir
function. With its help one can select a subset of the
entries in a directory, possibly sort them and get a list of names as
the result.
int scandir (const char *dir, struct dirent ***namelist, int (*selector) (const struct dirent *), int (*cmp) (const void *, const void *)) | Function |
The Finally the entries in *namelist are sorted using the
user-supplied function cmp. The arguments passed to the cmp
function are of type The return value of the function is the number of entries placed in
*namelist. If it is |
As described above the fourth argument to the scandir
function
must be a pointer to a sorting function. For the convenience of the
programmer the GNU C library contains implementations of functions which
are very helpful for this purpose.
int alphasort (const void *a, const void *b) | Function |
The alphasort function behaves like the strcoll function
(see String/Array Comparison). The difference is that the arguments
are not string pointers but instead they are of type
struct dirent ** .
The return value of |
int versionsort (const void *a, const void *b) | Function |
The versionsort function is like alphasort except that it
uses the strverscmp function internally.
|
If the filesystem supports large files we cannot use the scandir
anymore since the dirent
structure might not able to contain all
the information. The LFS provides the new type struct dirent64
. To use this we need a new function.
int scandir64 (const char *dir, struct dirent64 ***namelist, int (*selector) (const struct dirent64 *), int (*cmp) (const void *, const void *)) | Function |
The scandir64 function works like the scandir function
except that the directory entries it returns are described by elements
of type struct dirent64 . The function pointed to by
selector is again used to select the desired entries, except that
selector now must point to a function which takes a
struct dirent64 * parameter.
Similarly the cmp function should expect its two arguments to be
of type |
As cmp is now a function of a different type, the functions
alphasort
and versionsort
cannot be supplied for that
argument. Instead we provide the two replacement functions below.
int alphasort64 (const void *a, const void *b) | Function |
The alphasort64 function behaves like the strcoll function
(see String/Array Comparison). The difference is that the arguments
are not string pointers but instead they are of type
struct dirent64 ** .
Return value of |
int versionsort64 (const void *a, const void *b) | Function |
The versionsort64 function is like alphasort64 , excepted that it
uses the strverscmp function internally.
|
It is important not to mix the use of scandir
and the 64-bit
comparison functions or vice versa. There are systems on which this
works but on others it will fail miserably.