xref: /freebsd/lib/libc/gen/scandir.3 (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)scandir.3	8.1 (Berkeley) 6/4/93
29.\" $FreeBSD$
30.\"
31.Dd August 23, 2022
32.Dt SCANDIR 3
33.Os
34.Sh NAME
35.Nm scandir ,
36.Nm scandirat ,
37.Nm scandir_b ,
38.Nm alphasort ,
39.Nm versionsort
40.Nd scan a directory
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In dirent.h
45.Ft int
46.Fo scandir
47.Fa "const char *dirname"
48.Fa "struct dirent ***namelist"
49.Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp"
50.Fa "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
51.Fc
52.Ft
53.Fo scandirat
54.Fa int dirfd
55.Fa "const char *dirname"
56.Fa "struct dirent ***namelist"
57.Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp"
58.Fa "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
59.Fc
60.Ft int
61.Fo scandir_b
62.Fa "const char *dirname"
63.Fa "struct dirent ***namelist"
64.Fa "int \*(lp*select\^(rp\*(lpconst struct dirent *\*(rp"
65.Fa "int \*(lp^compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
66.Fc
67.Ft int
68.Fn alphasort "const struct dirent **d1" "const struct dirent **d2"
69.Ft int
70.Fn versionsort "const struct dirent **d1" "const struct dirent **d2"
71.Sh DESCRIPTION
72The
73.Fn scandir
74function
75reads the directory
76.Fa dirname
77and builds an array of pointers to directory
78entries using
79.Xr malloc 3 .
80It returns the number of entries in the array.
81A pointer to the array of directory entries is stored in the location
82referenced by
83.Fa namelist .
84.Pp
85The
86.Fa select
87argument is a pointer to a user supplied subroutine which is called by
88.Fn scandir
89to select which entries are to be included in the array.
90The select routine is passed a
91pointer to a directory entry and should return a non-zero
92value if the directory entry is to be included in the array.
93If
94.Fa select
95is null, then all the directory entries will be included.
96.Pp
97The
98.Fa compar
99argument is a pointer to a user supplied subroutine which is passed to
100.Xr qsort 3
101to sort the completed array.
102If this pointer is null, the array is not sorted.
103.Pp
104The
105.Fn alphasort
106function
107is a routine which can be used for the
108.Fa compar
109argument to sort the array alphabetically using
110.Xr strcoll 3 .
111.Pp
112The
113.Fn versionsort
114function is a routine which can be used for the
115.Fa compar
116argument to sort the array naturally using
117.Xr strverscmp 3 .
118.Pp
119The memory allocated for the array can be deallocated with
120.Xr free 3 ,
121by freeing each pointer in the array and then the array itself.
122.Pp
123The
124.Fn scandirat
125function is similar to
126.Fn scandir ,
127but takes an additional
128.Fa dirfd
129argument.
130If the supplied
131.Fa dirname
132is absolute, the function's behavior is identical to that of
133.Fn scandir ,
134the
135.Fa dirfd
136argument is unused.
137If
138.Fa dirname
139is relative,
140.Fa dirfd
141must be a valid file descriptor referencing a directory, in
142which case the
143.Fa dirname
144lookup is performed relative to the directory referenced by
145.Fa dirfd .
146If
147.Fa dirfd
148has the special value
149.Va AT_FDCWD ,
150then the current process directory is used as the base for
151relative lookups.
152See
153.Xr openat 2
154for additional details.
155.Pp
156The
157.Fn scandir_b
158function behaves in the same way as
159.Fn scandir ,
160but takes blocks as arguments instead of function pointers and calls
161.Fn qsort_b
162rather than
163.Fn qsort .
164.Sh DIAGNOSTICS
165Returns \-1 if the directory cannot be opened for reading or if
166.Xr malloc 3
167cannot allocate enough memory to hold all the data structures.
168.Sh SEE ALSO
169.Xr openat 2 ,
170.Xr directory 3 ,
171.Xr malloc 3 ,
172.Xr qsort 3 ,
173.Xr strcoll 3 ,
174.Xr strverscmp 3 ,
175.Xr dir 5
176.Sh STANDARDS
177The
178.Fn versionsort
179function is a GNU extension and conforms to no standard.
180.Sh HISTORY
181The
182.Fn scandir
183and
184.Fn alphasort
185functions appeared in
186.Bx 4.2 .
187The
188.Fn scandirat
189and
190.Fn
191versionsort
192functions were added in
193.Fx 14.0 .
194