![]() |
![]() |
![]() |
![]() |
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 *) );
If select is NULL, all the directory entries are included.
You can use alphasort() as the compar parameter to sort the array alphabetically.
libc
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.
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
alphasort(), closedir(), free(), malloc(), opendir(), qsort(), readdir(), rewinddir(), seekdir(), telldir()
![]() |
![]() |
![]() |
![]() |