SCANDIR(3,L) AIX Technical Reference SCANDIR(3,L) ------------------------------------------------------------------------------- scandir PURPOSE Scans a directory. LIBRARY Standard C Library (libc.a) SYNTAX #include <sys/types.h> #include <dirent.h> int scandir (dirname, namelist, select, compar) char *dirname; struct dirent * (*namelist [ ]); int (*select) ( ); int (*compar) ( ); DESCRIPTION The scandir subroutine reads the directory pointed to by dirname, then uses the malloc subroutine to create an array of pointers to directory entries. The scandir subroutine returns the number of entries in the array and, through the namelist parameter, a pointer to the array. The select parameter points to a user-supplied subroutine that is called by scandir to select which entries to include in the array. The selection routine is passed a pointer to a directory entry and should return a nonzero value for a directory entry that is included in the array. If select is NULL, all directory entries are included. The compar parameter also points to a user-supplied subroutine. This routine is passed to qsort to sort the completed array. If compar is NULL, the array is not sorted. The alphasort subroutine provides comparison functions for sorting alphabetically and can be specified by the compar parameter. (See "alphasort.") The memory allocated to the array can be deallocated by freeing each pointer in the array and the array itself, with the free subroutine. (For more information on free, see "malloc, free, realloc, calloc, valloc, alloca, mallopt, mallinfo.") RETURN VALUE The scandir subroutine returns the value -1 if the directory cannot be opened for reading, or if the malloc subroutine cannot allocate enough memory to hold Processed November 7, 1990 SCANDIR(3,L) 1
SCANDIR(3,L) AIX Technical Reference SCANDIR(3,L) all the data structures. If successful, the scandir subroutine returns the number of entries found. RELATED INFORMATION In this book: "alphasort," "directory: opendir, readdir, telldir, seekdir, rewinddir, closedir," "malloc, free, realloc, calloc, valloc, alloca, mallopt, mallinfo," "qsort," and "dir." Processed November 7, 1990 SCANDIR(3,L) 2