xref: /csrg-svn/lib/libc/gen/scandir.3 (revision 61111)
1*61111Sbostic.\" Copyright (c) 1983, 1991, 1993
2*61111Sbostic.\"	The Regents of the University of California.  All rights reserved.
320586Smckusick.\"
443571Strent.\" %sccs.include.redist.man%
520586Smckusick.\"
6*61111Sbostic.\"     @(#)scandir.3	8.1 (Berkeley) 06/04/93
735309Sbostic.\"
848352Scael.Dd
948352Scael.Dt SCANDIR 3
1048352Scael.Os BSD 4.2
1148352Scael.Sh NAME
1248352Scael.Nm scandir ,
1348352Scael.Nm alphasort
1448352Scael.Nd scan a directory
1548352Scael.Sh SYNOPSIS
1648352Scael.Fd #include <sys/types.h>
1748352Scael.Fd #include <dirent.h>
1848352Scael.Ft int
1948352Scael.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \\*(lp*select\\*(rp\\*(lpstruct dirent *\\*(rp" "int \\*(lp*compar\\*(rp\\*(lpconst void *, const void *\\*(rp"
2048352Scael.Ft int
2148352Scael.Fn alphasort "const void *d1" "const void *d2"
2248352Scael.Sh DESCRIPTION
2348352ScaelThe
2448352Scael.Fn scandir
2548352Scaelfunction
2620586Smckusickreads the directory
2748352Scael.Fa dirname
2820586Smckusickand builds an array of pointers to directory
2920586Smckusickentries using
3048352Scael.Xr malloc 3 .
3143536SbosticIt returns the number of entries in the array.
3243536SbosticA pointer to the array of directory entries is stored in the location
3343536Sbosticreferenced by
3448352Scael.Fa namelist .
3548352Scael.Pp
3620586SmckusickThe
3748352Scael.Fa select
3820586Smckusickparameter is a pointer to a user supplied subroutine which is called by
3948352Scael.Fn scandir
4020586Smckusickto select which entries are to be included in the array.
4120586SmckusickThe select routine is passed a
4220586Smckusickpointer to a directory entry and should return a non-zero
4320586Smckusickvalue if the directory entry is to be included in the array.
4420586SmckusickIf
4548352Scael.Fa select
4620586Smckusickis null, then all the directory entries will be included.
4748352Scael.Pp
4820586SmckusickThe
4948352Scael.Fa compar
5020586Smckusickparameter is a pointer to a user supplied subroutine which is passed to
5148352Scael.Xr qsort 3
5243610Sbosticto sort the completed array.
5343610SbosticIf this pointer is null, the array is not sorted.
5448352Scael.Pp
5548352ScaelThe
5648352Scael.Fn alphasort
5748352Scaelfunction
5820586Smckusickis a routine which can be used for the
5948352Scael.Fa compar
6020586Smckusickparameter to sort the array alphabetically.
6148352Scael.Pp
6220586SmckusickThe memory allocated for the array can be deallocated with
6348352Scael.Xr free 3 ,
6443610Sbosticby freeing each pointer in the array and then the array itself.
6548352Scael.Sh DIAGNOSTICS
6620586SmckusickReturns \-1 if the directory cannot be opened for reading or if
6748352Scael.Xr malloc 3
6820586Smckusickcannot allocate enough memory to hold all the data structures.
6948352Scael.Sh SEE ALSO
7048352Scael.Xr directory 3 ,
7148352Scael.Xr malloc 3 ,
7248352Scael.Xr qsort 3 ,
7348352Scael.Xr dir 5
7448352Scael.Sh HISTORY
7548352ScaelThe
7648352Scael.Fn scandir
7748352Scaeland
7848352Scael.Fn alphasort
7948352Scaelfunctions appeared in
8048352Scael.Bx 4.2 .
81