Index Index for
Section 3
Index Alphabetical
listing for S
Bottom of page Bottom of
page

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

dir_name Points to the directory name. name_list Points to the array of pointers to directory entries. select Points to a user-supplied function that is called by the scandir() function to select which entries to include in the array. compare Points to a user-supplied function that sorts the completed array. dir1 Points to a dirent structure. dir2 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 number 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 scandir() 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 structures 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 lexically 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 structure 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)

Index Index for
Section 3
Index Alphabetical
listing for S
Top of page Top of
page