Scan a directory
#include <sys/types.h> #include <sys/dir.h> int scandir( char * dirname, struct direct * (* namelist), int (*select)(struct dirent *), int (*compar)(const void *,const void *) );
- The name of the directory that you want to scan.
- A pointer to a location where scandir() can store a pointer to the array of directory entries that it builds.
- A pointer to a user-supplied subroutine that scandir() calls
to select which entries to included in the array.
The select routine is passed a pointer to a directory entry and
should return a nonzero value if the directory entry is to be included in
If select is NULL, all the directory entries are included.
- A pointer to a user-supplied subroutine that's passed to
to sort the completed array.
If this pointer is NULL, the array isn't sorted.
You can use alphasort() as the compar parameter to sort the array alphabetically.
Use the -l c option to qcc to link against this library. This library is usually included automatically.
|This function is in libc.a, but not in libc.so (in order to save space).|
The scandir() function reads the directory dirname and builds an array of pointers to directory entries, using malloc() to allocate the space. The scandir() function returns the number of entries in the array, and stores a pointer to the array in the location referenced by namelist.
You can deallocate the memory allocated for the array by calling free(). Free each pointer in the array, and then free the array itself.
The number of entries in the array, or -1 if the directory can't be opened for reading, or malloc() can't allocate enough memory to hold all the data structures.