scandir(3)scandir(3)NAME
scandir, alphasort - Scan or sorts directory contents
SYNOPSIS
#include <sys/types.h> #include <sys/dir.h>
int scandir(
char *dir_name,
struct dirent *(*name_list []),
int (*compare)(struct dirent **dir1, struct dirent **dir2) );
int alphasort(
struct dirent **dir1,
struct dirent **dir2 );
LIBRARY
Standard C Library (libc)
PARAMETERS
Points to the directory name. Points to the array of pointers to
directory entries. Points to a user-supplied function that is called
by the scandir() function to select which entries to include in the
array. Points to a user-supplied function that sorts the completed
array. Points to a dirent structure. Points to a dirent structure.
DESCRIPTION
The scandir() function reads the directory pointed to by the dir_name
parameter. It then uses the malloc() function to create an array of
pointers to directory entries. The scandir() function returns the num‐
ber of entries in the array and, through the name_list parameter, a
pointer to the array.
The select parameter points to a user-supplied function that the scan‐
dir() function calls to select which entries to include in the array.
The selection routine is passed a pointer to a directory entry and
returns a nonzero value for a directory entry that is included in the
array. If the select parameter is a null value, all directory entries
are included.
The compare parameter points to a user-supplied function that is passed
to the qsort() function to sort the completed array. If the compare
parameter is a null value, the array is not sorted.
The memory allocated to the array can be deallocated by freeing each
pointer in the array, and the array itself, with the free() function.
The alphasort() function alphabetically compares the two dirent struc‐
tures pointed to by the dir1 and dir2 parameters. This function can be
passed as the compare parameter to either the scandir() function or the
qsort() function. A user-supplied subroutine may also be used.
RETURN VALUES
The scandir() function returns -1 if the directory cannot be opened for
reading or if the malloc() function cannot allocate enough memory to
hold all the data structures. If successful, the scandir() function
returns the number of entries found.
The alphasort() function returns the following values: Less than 0
(zero): The dirent structure pointed to by the dir1 parameter is lexi‐
cally less than the dirent structure pointed to by the dir2 parameter.
0 (zero): The dirent structures pointed to by the dir1 parameter and
the dir2 parameter are equal. Greater than 0 (zero): The dirent struc‐
ture pointed to by the dir1 parameter is lexically greater than the
direntstructure pointed to by the dir2 parameter.
SEE ALSO
Functions: malloc(3), opendir(3), qsort(3)scandir(3)