xref: /freebsd/lib/libc/gen/scandir.3 (revision 09ec5e67a7a6605e5a58a2e3e82dab243a90609a)
158f0484fSRodney W. Grimes.\" Copyright (c) 1983, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
12fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
1358f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1458f0484fSRodney W. Grimes.\"    without specific prior written permission.
1558f0484fSRodney W. Grimes.\"
1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658f0484fSRodney W. Grimes.\" SUCH DAMAGE.
2758f0484fSRodney W. Grimes.\"
2858f0484fSRodney W. Grimes.\"     @(#)scandir.3	8.1 (Berkeley) 6/4/93
2958f0484fSRodney W. Grimes.\"
30*09ec5e67SMina Galić.Dd August 31, 2023
3158f0484fSRodney W. Grimes.Dt SCANDIR 3
32a307d598SRuslan Ermilov.Os
3358f0484fSRodney W. Grimes.Sh NAME
3458f0484fSRodney W. Grimes.Nm scandir ,
35a294e679SKonstantin Belousov.Nm scandirat ,
36ea448a0aSKonstantin Belousov.Nm scandir_b ,
3705c9a015SAymeric Wibo.Nm alphasort ,
3805c9a015SAymeric Wibo.Nm versionsort
3958f0484fSRodney W. Grimes.Nd scan a directory
4025bb73e0SAlexey Zelkin.Sh LIBRARY
4125bb73e0SAlexey Zelkin.Lb libc
4258f0484fSRodney W. Grimes.Sh SYNOPSIS
4332eef9aeSRuslan Ermilov.In dirent.h
4458f0484fSRodney W. Grimes.Ft int
45aa5e19a9SKonstantin Belousov.Fo scandir
46aa5e19a9SKonstantin Belousov.Fa "const char *dirname"
47aa5e19a9SKonstantin Belousov.Fa "struct dirent ***namelist"
48aa5e19a9SKonstantin Belousov.Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp"
49aa5e19a9SKonstantin Belousov.Fa "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
50aa5e19a9SKonstantin Belousov.Fc
51a294e679SKonstantin Belousov.Ft
52a294e679SKonstantin Belousov.Fo scandirat
53a294e679SKonstantin Belousov.Fa int dirfd
54a294e679SKonstantin Belousov.Fa "const char *dirname"
55a294e679SKonstantin Belousov.Fa "struct dirent ***namelist"
56a294e679SKonstantin Belousov.Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp"
57a294e679SKonstantin Belousov.Fa "int \*(lp*compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
58a294e679SKonstantin Belousov.Fc
5958f0484fSRodney W. Grimes.Ft int
60aa5e19a9SKonstantin Belousov.Fo scandir_b
61aa5e19a9SKonstantin Belousov.Fa "const char *dirname"
62aa5e19a9SKonstantin Belousov.Fa "struct dirent ***namelist"
63aa5e19a9SKonstantin Belousov.Fa "int \*(lp*select\^(rp\*(lpconst struct dirent *\*(rp"
64aa5e19a9SKonstantin Belousov.Fa "int \*(lp^compar\*(rp\*(lpconst struct dirent **, const struct dirent **\*(rp"
65aa5e19a9SKonstantin Belousov.Fc
6646cdc140SDavid Chisnall.Ft int
674176dd52SKonstantin Belousov.Fn alphasort "const struct dirent **d1" "const struct dirent **d2"
6805c9a015SAymeric Wibo.Ft int
6905c9a015SAymeric Wibo.Fn versionsort "const struct dirent **d1" "const struct dirent **d2"
7058f0484fSRodney W. Grimes.Sh DESCRIPTION
7158f0484fSRodney W. GrimesThe
7258f0484fSRodney W. Grimes.Fn scandir
7358f0484fSRodney W. Grimesfunction
7458f0484fSRodney W. Grimesreads the directory
7558f0484fSRodney W. Grimes.Fa dirname
7658f0484fSRodney W. Grimesand builds an array of pointers to directory
7758f0484fSRodney W. Grimesentries using
7858f0484fSRodney W. Grimes.Xr malloc 3 .
7958f0484fSRodney W. GrimesIt returns the number of entries in the array.
8058f0484fSRodney W. GrimesA pointer to the array of directory entries is stored in the location
8158f0484fSRodney W. Grimesreferenced by
8258f0484fSRodney W. Grimes.Fa namelist .
8358f0484fSRodney W. Grimes.Pp
8458f0484fSRodney W. GrimesThe
8558f0484fSRodney W. Grimes.Fa select
862efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is called by
8758f0484fSRodney W. Grimes.Fn scandir
8858f0484fSRodney W. Grimesto select which entries are to be included in the array.
8958f0484fSRodney W. GrimesThe select routine is passed a
9058f0484fSRodney W. Grimespointer to a directory entry and should return a non-zero
9158f0484fSRodney W. Grimesvalue if the directory entry is to be included in the array.
9258f0484fSRodney W. GrimesIf
9358f0484fSRodney W. Grimes.Fa select
9458f0484fSRodney W. Grimesis null, then all the directory entries will be included.
9558f0484fSRodney W. Grimes.Pp
9658f0484fSRodney W. GrimesThe
9758f0484fSRodney W. Grimes.Fa compar
982efeeba5SRuslan Ermilovargument is a pointer to a user supplied subroutine which is passed to
9958f0484fSRodney W. Grimes.Xr qsort 3
10058f0484fSRodney W. Grimesto sort the completed array.
10158f0484fSRodney W. GrimesIf this pointer is null, the array is not sorted.
10258f0484fSRodney W. Grimes.Pp
10358f0484fSRodney W. GrimesThe
10458f0484fSRodney W. Grimes.Fn alphasort
10558f0484fSRodney W. Grimesfunction
10658f0484fSRodney W. Grimesis a routine which can be used for the
10758f0484fSRodney W. Grimes.Fa compar
108e5732e18SAndrey A. Chernovargument to sort the array alphabetically using
109e5732e18SAndrey A. Chernov.Xr strcoll 3 .
11058f0484fSRodney W. Grimes.Pp
11105c9a015SAymeric WiboThe
11205c9a015SAymeric Wibo.Fn versionsort
11305c9a015SAymeric Wibofunction is a routine which can be used for the
11405c9a015SAymeric Wibo.Fa compar
11505c9a015SAymeric Wiboargument to sort the array naturally using
11605c9a015SAymeric Wibo.Xr strverscmp 3 .
11705c9a015SAymeric Wibo.Pp
11858f0484fSRodney W. GrimesThe memory allocated for the array can be deallocated with
11958f0484fSRodney W. Grimes.Xr free 3 ,
12058f0484fSRodney W. Grimesby freeing each pointer in the array and then the array itself.
12146cdc140SDavid Chisnall.Pp
12246cdc140SDavid ChisnallThe
123a294e679SKonstantin Belousov.Fn scandirat
124a294e679SKonstantin Belousovfunction is similar to
125a294e679SKonstantin Belousov.Fn scandir ,
126a294e679SKonstantin Belousovbut takes an additional
127a294e679SKonstantin Belousov.Fa dirfd
128a294e679SKonstantin Belousovargument.
129a294e679SKonstantin BelousovIf the supplied
130a294e679SKonstantin Belousov.Fa dirname
131a294e679SKonstantin Belousovis absolute, the function's behavior is identical to that of
132a294e679SKonstantin Belousov.Fn scandir ,
133a294e679SKonstantin Belousovthe
134a294e679SKonstantin Belousov.Fa dirfd
135a294e679SKonstantin Belousovargument is unused.
136a294e679SKonstantin BelousovIf
137a294e679SKonstantin Belousov.Fa dirname
138a294e679SKonstantin Belousovis relative,
139a294e679SKonstantin Belousov.Fa dirfd
140a294e679SKonstantin Belousovmust be a valid file descriptor referencing a directory, in
141a294e679SKonstantin Belousovwhich case the
142a294e679SKonstantin Belousov.Fa dirname
143a294e679SKonstantin Belousovlookup is performed relative to the directory referenced by
144a294e679SKonstantin Belousov.Fa dirfd .
145a294e679SKonstantin BelousovIf
146a294e679SKonstantin Belousov.Fa dirfd
147a294e679SKonstantin Belousovhas the special value
148a294e679SKonstantin Belousov.Va AT_FDCWD ,
149a294e679SKonstantin Belousovthen the current process directory is used as the base for
150a294e679SKonstantin Belousovrelative lookups.
151a294e679SKonstantin BelousovSee
152a294e679SKonstantin Belousov.Xr openat 2
153a294e679SKonstantin Belousovfor additional details.
154a294e679SKonstantin Belousov.Pp
155a294e679SKonstantin BelousovThe
15646cdc140SDavid Chisnall.Fn scandir_b
15746cdc140SDavid Chisnallfunction behaves in the same way as
15846cdc140SDavid Chisnall.Fn scandir ,
15946cdc140SDavid Chisnallbut takes blocks as arguments instead of function pointers and calls
16046cdc140SDavid Chisnall.Fn qsort_b
16146cdc140SDavid Chisnallrather than
16246cdc140SDavid Chisnall.Fn qsort .
16358f0484fSRodney W. Grimes.Sh DIAGNOSTICS
16458f0484fSRodney W. GrimesReturns \-1 if the directory cannot be opened for reading or if
16558f0484fSRodney W. Grimes.Xr malloc 3
16658f0484fSRodney W. Grimescannot allocate enough memory to hold all the data structures.
16758f0484fSRodney W. Grimes.Sh SEE ALSO
168a294e679SKonstantin Belousov.Xr openat 2 ,
16958f0484fSRodney W. Grimes.Xr directory 3 ,
17058f0484fSRodney W. Grimes.Xr malloc 3 ,
17158f0484fSRodney W. Grimes.Xr qsort 3 ,
1720aee91e1SChristian Brueffer.Xr strcoll 3 ,
17305c9a015SAymeric Wibo.Xr strverscmp 3 ,
1740aee91e1SChristian Brueffer.Xr dir 5
17505c9a015SAymeric Wibo.Sh STANDARDS
17605c9a015SAymeric WiboThe
17705c9a015SAymeric Wibo.Fn versionsort
17805c9a015SAymeric Wibofunction is a GNU extension and conforms to no standard.
17958f0484fSRodney W. Grimes.Sh HISTORY
18058f0484fSRodney W. GrimesThe
18158f0484fSRodney W. Grimes.Fn scandir
18258f0484fSRodney W. Grimesand
18358f0484fSRodney W. Grimes.Fn alphasort
18458f0484fSRodney W. Grimesfunctions appeared in
18558f0484fSRodney W. Grimes.Bx 4.2 .
186a294e679SKonstantin BelousovThe
187a294e679SKonstantin Belousov.Fn scandirat
18805c9a015SAymeric Wiboand
18905c9a015SAymeric Wibo.Fn
19005c9a015SAymeric Wiboversionsort
19105c9a015SAymeric Wibofunctions were added in
192*09ec5e67SMina Galić.Fx 13.2 .
193