[Previous] [Contents] [Next]

scandir()

Scan a directory

Synopsis:

#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 *) );

Library:

libc

Description:

The scandir() function reads the directory dirname and builds an array of pointers to directory entries using malloc(). It returns the number of entries in the array. A pointer to the array of directory entries is stored in the location referenced by namelist.

The select parameter is a pointer to a user-supplied subroutine that's called by scandir() to select which entries are to be 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 the array. If select is NULL, all the directory entries are included.

The compar parameter is a pointer to a user-supplied subroutine that's passed to qsort() to sort the completed array. If this pointer is NULL, the array isn't sorted.

The alphasort() function can be used for the compar parameter to sort the array alphabetically.

The memory allocated for the array can be deallocated with free(), by freeing each pointer in the array and then the array itself.

Returns:

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.

Classification:

Legacy Unix

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

alphasort(), closedir(), malloc(), opendir(), qsort(), readdir(), rewinddir()


[Previous] [Contents] [Next]