date and time created 91/03/07 10:23:53 by bostic
[unix-history] / usr / src / lib / libc / gen / scandir.3
CommitLineData
460316d1 1.\" Copyright (c) 1983 Regents of the University of California.
25aac6e1 2.\" All rights reserved.
460316d1 3.\"
91cff1e1 4.\" %sccs.include.redist.man%
25aac6e1 5.\"
955256c9 6.\" @(#)scandir.3 6.7 (Berkeley) %G%
460316d1 7.\"
de30fdda 8.TH SCANDIR 3 ""
460316d1
KM
9.UC 5
10.SH NAME
7b21dbb4 11scandir, alphasort \- scan a directory
460316d1
KM
12.SH SYNOPSIS
13.nf
643c10cf
KB
14.ft B
15#include <sys/types.h>
16#include <dirent.h>
17
18scandir(dirname, namelist, select, compar)
19char *dirname;
955256c9 20struct dirent ***namelist;
643c10cf
KB
21int (*select)();
22int (*compar)();
23
24alphasort(d1, d2)
25void *d1, *d2;
26.ft R
460316d1
KM
27.fi
28.SH DESCRIPTION
29.I Scandir
30reads the directory
31.I dirname
32and builds an array of pointers to directory
33entries using
34.IR malloc (3).
4754ea9c
KB
35It returns the number of entries in the array.
36A pointer to the array of directory entries is stored in the location
37referenced by
460316d1
KM
38.IR namelist .
39.PP
40The
41.I select
42parameter is a pointer to a user supplied subroutine which is called by
43.I scandir
44to select which entries are to be included in the array.
45The select routine is passed a
46pointer to a directory entry and should return a non-zero
47value if the directory entry is to be included in the array.
48If
49.I select
50is null, then all the directory entries will be included.
51.PP
52The
53.I compar
54parameter is a pointer to a user supplied subroutine which is passed to
55.IR qsort (3)
643c10cf
KB
56to sort the completed array.
57If this pointer is null, the array is not sorted.
58.PP
460316d1
KM
59.I Alphasort
60is a routine which can be used for the
61.I compar
62parameter to sort the array alphabetically.
63.PP
64The memory allocated for the array can be deallocated with
643c10cf
KB
65.IR free (3),
66by freeing each pointer in the array and then the array itself.
460316d1 67.SH "SEE ALSO"
643c10cf 68directory(3), malloc(3), qsort(3), dir(5)
460316d1
KM
69.SH DIAGNOSTICS
70Returns \-1 if the directory cannot be opened for reading or if
71.IR malloc (3)
72cannot allocate enough memory to hold all the data structures.