Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

alphasort(3) [osf1 man page]

scandir(3)						     Library Functions Manual							scandir(3)

NAME
scandir, alphasort - Scans or sorts directory contents LIBRARY
Standard C Library (libc.a) SYNOPSIS
#include <sys/types.h> #include <sys/dir.h> int scandir ( char *dir_name, struct dirent * (*name_list[ ]), int (*select) ( struct dirent *dir ), int (*compare) ( struct dirent **dir1, struct dirent **dir2 ) ); int alphasort ( struct dirent **dir1, struct dirent **dir2 ); 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 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 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 structure pointed to by the dir1 parameter is lexically greater than the dirent structure pointed to by the dir2 parameter. RELATED INFORMATION
Functions: malloc(3), opendir(3), qsort(3) delim off scandir(3)

Check Out this Related Man Page

SCANDIR(3)						   BSD Library Functions Manual 						SCANDIR(3)

NAME
scandir, scandir_b, alphasort -- scan a directory SYNOPSIS
#include <sys/types.h> #include <dirent.h> int scandir(const char *dirname, struct dirent ***namelist, int (*select)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)); int alphasort(const struct dirent **d1, const struct dirent **d2); int scandir_b(const char *dirname, struct dirent ***namelist, int (^select)(const struct dirent *), int (^compar)(const struct dirent **, const struct dirent **)); DESCRIPTION
The scandir() function reads the directory dirname and builds an array of pointers to directory entries using malloc(3). It returns the num- ber of entries in the array. A pointer to the array of directory entries is stored in the location referenced by namelist (even if the num- ber of entries is 0). The select argument is a pointer to a user supplied subroutine which is 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 non-zero value if the directory entry is to be included in the array. If select is null, then all the directory entries will be included. The compar argument is a pointer to a user supplied subroutine which is passed to qsort(3) to sort the completed array. If this pointer is null, the array is not sorted. The alphasort() function is a routine which can be used for the compar argument to sort the array alphabetically. The memory allocated for the array can be deallocated with free(3), by freeing each pointer in the array and then the array itself. The scandir_b() function works the same way as the scandir() function, except that select and compar are blocks instead of subroutines. DIAGNOSTICS
Returns -1 if the directory cannot be opened for reading or if malloc(3) cannot allocate enough memory to hold all the data structures. SEE ALSO
directory(3), malloc(3), qsort(3), dir(5) HISTORY
The scandir() and alphasort() functions appeared in 4.2BSD. The scandir_b() function appeared in Mac OS X 10.6. BSD
May 20, 2008 BSD
Man Page