Scandi R

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

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

Arguments:
dirname The name of the directory that you want to scan. namelist A pointer to a location where scandir() can store a pointer to the array of directory entries (of typestruct direct *) that it builds. select A pointer to a user-supplied subroutine that scandir() calls to select which entries to included in the array. The select routine is passed a pointer to a directory entry (a struct dirent) 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. compar 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. You can use alphasort() as the compar parameter to sort the array alphabetically.

Library:
libc Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:
The scandir() function reads the directory dirname and builds an array of pointers to directory entries, using malloc() to allocate the space. The scandir() function returns the number of entries in the array, and stores a pointer to the array in the location referenced by namelist.

alphasort()
Compare two directory entries

Synopsis:
#include <sys/types.h> #include <sys/dir.h> struct direct { unsigned long d_fileno; unsigned short d_reclen; unsigned short d_namlen; char d_name[1]; }; int alphasort( struct direct **d1, struct direct **d2);

Arguments:
d1, d2 Pointers to the struct direct directory entries that you want to compare.

Library:
libc Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:
The alphasort() function alphabetically compares two directory entries. You can use it as the comparargument to scandir().

Returns:
<0 The d1 entry precedes the d2 entry alphabetically. 0 The entries are equivalent. >0 The d1 entry follows the d2 entry alphabetically.

You might also like