xref: /freebsd/lib/libc/gen/scandir.c (revision 992bcb37)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1983, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
32b231cb39SDavid E. O'Brien #include <sys/cdefs.h>
33c1920558SJohn Baldwin __SCCSID("@(#)scandir.c	8.3 (Berkeley) 1/2/94");
34b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$");
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes /*
3758f0484fSRodney W. Grimes  * Scan the directory dirname calling select to make a list of selected
3858f0484fSRodney W. Grimes  * directory entries then sort using qsort and compare routine dcomp.
3958f0484fSRodney W. Grimes  * Returns the number of entries and a pointer to a list of pointers to
4058f0484fSRodney W. Grimes  * struct dirent (through namelist). Returns -1 if there were any errors.
4158f0484fSRodney W. Grimes  */
4258f0484fSRodney W. Grimes 
43d201fe46SDaniel Eischen #include "namespace.h"
4458f0484fSRodney W. Grimes #include <dirent.h>
4558f0484fSRodney W. Grimes #include <stdlib.h>
4658f0484fSRodney W. Grimes #include <string.h>
47d201fe46SDaniel Eischen #include "un-namespace.h"
4858f0484fSRodney W. Grimes 
4946cdc140SDavid Chisnall #ifdef	I_AM_SCANDIR_B
5046cdc140SDavid Chisnall #include "block_abi.h"
5146cdc140SDavid Chisnall #define	SELECT(x)	CALL_BLOCK(select, x)
5246cdc140SDavid Chisnall #ifndef __BLOCKS__
53cc321ccdSKonstantin Belousov void qsort_b(void *, size_t, size_t, void *);
5446cdc140SDavid Chisnall #endif
5546cdc140SDavid Chisnall #else
5646cdc140SDavid Chisnall #define	SELECT(x)	select(x)
5746cdc140SDavid Chisnall #endif
5846cdc140SDavid Chisnall 
59992bcb37SMateusz Guzik #ifndef I_AM_SCANDIR_B
60f5636f88SKonstantin Belousov static int alphasort_thunk(void *thunk, const void *p1, const void *p2);
61992bcb37SMateusz Guzik #endif
62f5636f88SKonstantin Belousov 
6358f0484fSRodney W. Grimes int
6446cdc140SDavid Chisnall #ifdef I_AM_SCANDIR_B
6546cdc140SDavid Chisnall scandir_b(const char *dirname, struct dirent ***namelist,
6646cdc140SDavid Chisnall     DECLARE_BLOCK(int, select, const struct dirent *),
6746cdc140SDavid Chisnall     DECLARE_BLOCK(int, dcomp, const struct dirent **, const struct dirent **))
6846cdc140SDavid Chisnall #else
694176dd52SKonstantin Belousov scandir(const char *dirname, struct dirent ***namelist,
704176dd52SKonstantin Belousov     int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
714176dd52SKonstantin Belousov 	const struct dirent **))
7246cdc140SDavid Chisnall #endif
7358f0484fSRodney W. Grimes {
74b231cb39SDavid E. O'Brien 	struct dirent *d, *p, **names = NULL;
755f2bd3bdSPedro F. Giffuni 	size_t arraysz, numitems;
7658f0484fSRodney W. Grimes 	DIR *dirp;
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes 	if ((dirp = opendir(dirname)) == NULL)
7958f0484fSRodney W. Grimes 		return(-1);
8058f0484fSRodney W. Grimes 
810a8ff54eSConrad Meyer 	numitems = 0;
8218798c64SDavid Schultz 	arraysz = 32;	/* initial estimate of the array size */
8358f0484fSRodney W. Grimes 	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
8458f0484fSRodney W. Grimes 	if (names == NULL)
8529595ffdSJulian Elischer 		goto fail;
8658f0484fSRodney W. Grimes 
8758f0484fSRodney W. Grimes 	while ((d = readdir(dirp)) != NULL) {
8846cdc140SDavid Chisnall 		if (select != NULL && !SELECT(d))
8958f0484fSRodney W. Grimes 			continue;	/* just selected names */
9058f0484fSRodney W. Grimes 		/*
9158f0484fSRodney W. Grimes 		 * Make a minimum size copy of the data
9258f0484fSRodney W. Grimes 		 */
9369921123SKonstantin Belousov 		p = (struct dirent *)malloc(_GENERIC_DIRSIZ(d));
9458f0484fSRodney W. Grimes 		if (p == NULL)
9529595ffdSJulian Elischer 			goto fail;
96e169c667SPoul-Henning Kamp 		p->d_fileno = d->d_fileno;
97e169c667SPoul-Henning Kamp 		p->d_type = d->d_type;
9858f0484fSRodney W. Grimes 		p->d_reclen = d->d_reclen;
9958f0484fSRodney W. Grimes 		p->d_namlen = d->d_namlen;
10058f0484fSRodney W. Grimes 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
10158f0484fSRodney W. Grimes 		/*
10258f0484fSRodney W. Grimes 		 * Check to make sure the array has space left and
10358f0484fSRodney W. Grimes 		 * realloc the maximum size.
10458f0484fSRodney W. Grimes 		 */
10556362c6fSPedro F. Giffuni 		if (numitems >= arraysz) {
10629595ffdSJulian Elischer 			struct dirent **names2;
10729595ffdSJulian Elischer 
1089f36610fSPedro F. Giffuni 			names2 = reallocarray(names, arraysz,
1099f36610fSPedro F. Giffuni 			    2 * sizeof(struct dirent *));
11029595ffdSJulian Elischer 			if (names2 == NULL) {
11129595ffdSJulian Elischer 				free(p);
11229595ffdSJulian Elischer 				goto fail;
11358f0484fSRodney W. Grimes 			}
11429595ffdSJulian Elischer 			names = names2;
11518798c64SDavid Schultz 			arraysz *= 2;
11629595ffdSJulian Elischer 		}
11756362c6fSPedro F. Giffuni 		names[numitems++] = p;
11858f0484fSRodney W. Grimes 	}
11958f0484fSRodney W. Grimes 	closedir(dirp);
12056362c6fSPedro F. Giffuni 	if (numitems && dcomp != NULL)
12146cdc140SDavid Chisnall #ifdef I_AM_SCANDIR_B
12256362c6fSPedro F. Giffuni 		qsort_b(names, numitems, sizeof(struct dirent *), (void*)dcomp);
12346cdc140SDavid Chisnall #else
12456362c6fSPedro F. Giffuni 		qsort_r(names, numitems, sizeof(struct dirent *),
125f5636f88SKonstantin Belousov 		    &dcomp, alphasort_thunk);
12646cdc140SDavid Chisnall #endif
12758f0484fSRodney W. Grimes 	*namelist = names;
12856362c6fSPedro F. Giffuni 	return (numitems);
12929595ffdSJulian Elischer 
13029595ffdSJulian Elischer fail:
13156362c6fSPedro F. Giffuni 	while (numitems > 0)
13256362c6fSPedro F. Giffuni 		free(names[--numitems]);
13329595ffdSJulian Elischer 	free(names);
13429595ffdSJulian Elischer 	closedir(dirp);
1354176dd52SKonstantin Belousov 	return (-1);
13658f0484fSRodney W. Grimes }
13758f0484fSRodney W. Grimes 
138cc321ccdSKonstantin Belousov #ifndef I_AM_SCANDIR_B
13958f0484fSRodney W. Grimes /*
14058f0484fSRodney W. Grimes  * Alphabetic order comparison routine for those who want it.
1415fc5e42aSAndrey A. Chernov  * POSIX 2008 requires that alphasort() uses strcoll().
14258f0484fSRodney W. Grimes  */
14358f0484fSRodney W. Grimes int
1444176dd52SKonstantin Belousov alphasort(const struct dirent **d1, const struct dirent **d2)
14558f0484fSRodney W. Grimes {
1464176dd52SKonstantin Belousov 
147dcdafd0eSAndrey A. Chernov 	return (strcoll((*d1)->d_name, (*d2)->d_name));
14858f0484fSRodney W. Grimes }
149f5636f88SKonstantin Belousov 
150f5636f88SKonstantin Belousov static int
151f5636f88SKonstantin Belousov alphasort_thunk(void *thunk, const void *p1, const void *p2)
152f5636f88SKonstantin Belousov {
153f5636f88SKonstantin Belousov 	int (*dc)(const struct dirent **, const struct dirent **);
154f5636f88SKonstantin Belousov 
155f5636f88SKonstantin Belousov 	dc = *(int (**)(const struct dirent **, const struct dirent **))thunk;
156f5636f88SKonstantin Belousov 	return (dc((const struct dirent **)p1, (const struct dirent **)p2));
157f5636f88SKonstantin Belousov }
158cc321ccdSKonstantin Belousov #endif
159