xref: /freebsd/lib/libc/gen/scandir.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1983, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1458f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1558f0484fSRodney W. Grimes  *    without specific prior written permission.
1658f0484fSRodney W. Grimes  *
1758f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1858f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1958f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2058f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2158f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2258f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2358f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2458f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2558f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2658f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2758f0484fSRodney W. Grimes  * SUCH DAMAGE.
2858f0484fSRodney W. Grimes  */
2958f0484fSRodney W. Grimes 
3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3158f0484fSRodney W. Grimes static char sccsid[] = "@(#)scandir.c	8.3 (Berkeley) 1/2/94";
3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
33b231cb39SDavid E. O'Brien #include <sys/cdefs.h>
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__
5346cdc140SDavid Chisnall void
5446cdc140SDavid Chisnall qsort_b(void *, size_t, size_t, void*);
5546cdc140SDavid Chisnall #endif
5646cdc140SDavid Chisnall #else
5746cdc140SDavid Chisnall #define	SELECT(x)	select(x)
5846cdc140SDavid Chisnall #endif
5946cdc140SDavid Chisnall 
60f5636f88SKonstantin Belousov static int alphasort_thunk(void *thunk, const void *p1, const void *p2);
61f5636f88SKonstantin Belousov 
6258f0484fSRodney W. Grimes /*
6358f0484fSRodney W. Grimes  * The DIRSIZ macro is the minimum record length which will hold the directory
6458f0484fSRodney W. Grimes  * entry.  This requires the amount of space in struct dirent without the
6558f0484fSRodney W. Grimes  * d_name field, plus enough space for the name and a terminating nul byte
6658f0484fSRodney W. Grimes  * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
6758f0484fSRodney W. Grimes  */
6858f0484fSRodney W. Grimes #undef DIRSIZ
6958f0484fSRodney W. Grimes #define DIRSIZ(dp)							\
7058f0484fSRodney W. Grimes 	((sizeof(struct dirent) - sizeof(dp)->d_name) +			\
7158f0484fSRodney W. Grimes 	    (((dp)->d_namlen + 1 + 3) &~ 3))
7258f0484fSRodney W. Grimes 
7358f0484fSRodney W. Grimes int
7446cdc140SDavid Chisnall #ifdef I_AM_SCANDIR_B
7546cdc140SDavid Chisnall scandir_b(const char *dirname, struct dirent ***namelist,
7646cdc140SDavid Chisnall     DECLARE_BLOCK(int, select, const struct dirent *),
7746cdc140SDavid Chisnall     DECLARE_BLOCK(int, dcomp, const struct dirent **, const struct dirent **))
7846cdc140SDavid Chisnall #else
794176dd52SKonstantin Belousov scandir(const char *dirname, struct dirent ***namelist,
804176dd52SKonstantin Belousov     int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
814176dd52SKonstantin Belousov 	const struct dirent **))
8246cdc140SDavid Chisnall #endif
8358f0484fSRodney W. Grimes {
84b231cb39SDavid E. O'Brien 	struct dirent *d, *p, **names = NULL;
85b231cb39SDavid E. O'Brien 	size_t nitems = 0;
8658f0484fSRodney W. Grimes 	long arraysz;
8758f0484fSRodney W. Grimes 	DIR *dirp;
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes 	if ((dirp = opendir(dirname)) == NULL)
9058f0484fSRodney W. Grimes 		return(-1);
9158f0484fSRodney W. Grimes 
9218798c64SDavid Schultz 	arraysz = 32;	/* initial estimate of the array size */
9358f0484fSRodney W. Grimes 	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
9458f0484fSRodney W. Grimes 	if (names == NULL)
9529595ffdSJulian Elischer 		goto fail;
9658f0484fSRodney W. Grimes 
9758f0484fSRodney W. Grimes 	while ((d = readdir(dirp)) != NULL) {
9846cdc140SDavid Chisnall 		if (select != NULL && !SELECT(d))
9958f0484fSRodney W. Grimes 			continue;	/* just selected names */
10058f0484fSRodney W. Grimes 		/*
10158f0484fSRodney W. Grimes 		 * Make a minimum size copy of the data
10258f0484fSRodney W. Grimes 		 */
10358f0484fSRodney W. Grimes 		p = (struct dirent *)malloc(DIRSIZ(d));
10458f0484fSRodney W. Grimes 		if (p == NULL)
10529595ffdSJulian Elischer 			goto fail;
106e169c667SPoul-Henning Kamp 		p->d_fileno = d->d_fileno;
107e169c667SPoul-Henning Kamp 		p->d_type = d->d_type;
10858f0484fSRodney W. Grimes 		p->d_reclen = d->d_reclen;
10958f0484fSRodney W. Grimes 		p->d_namlen = d->d_namlen;
11058f0484fSRodney W. Grimes 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
11158f0484fSRodney W. Grimes 		/*
11258f0484fSRodney W. Grimes 		 * Check to make sure the array has space left and
11358f0484fSRodney W. Grimes 		 * realloc the maximum size.
11458f0484fSRodney W. Grimes 		 */
11529595ffdSJulian Elischer 		if (nitems >= arraysz) {
11629595ffdSJulian Elischer 			struct dirent **names2;
11729595ffdSJulian Elischer 
11829595ffdSJulian Elischer 			names2 = (struct dirent **)realloc((char *)names,
11918798c64SDavid Schultz 				(arraysz * 2) * sizeof(struct dirent *));
12029595ffdSJulian Elischer 			if (names2 == NULL) {
12129595ffdSJulian Elischer 				free(p);
12229595ffdSJulian Elischer 				goto fail;
12358f0484fSRodney W. Grimes 			}
12429595ffdSJulian Elischer 			names = names2;
12518798c64SDavid Schultz 			arraysz *= 2;
12629595ffdSJulian Elischer 		}
12729595ffdSJulian Elischer 		names[nitems++] = p;
12858f0484fSRodney W. Grimes 	}
12958f0484fSRodney W. Grimes 	closedir(dirp);
13058f0484fSRodney W. Grimes 	if (nitems && dcomp != NULL)
13146cdc140SDavid Chisnall #ifdef I_AM_SCANDIR_B
13246cdc140SDavid Chisnall 		qsort_b(names, nitems, sizeof(struct dirent *), (void*)dcomp);
13346cdc140SDavid Chisnall #else
134f5636f88SKonstantin Belousov 		qsort_r(names, nitems, sizeof(struct dirent *),
135f5636f88SKonstantin Belousov 		    &dcomp, alphasort_thunk);
13646cdc140SDavid Chisnall #endif
13758f0484fSRodney W. Grimes 	*namelist = names;
13858f0484fSRodney W. Grimes 	return (nitems);
13929595ffdSJulian Elischer 
14029595ffdSJulian Elischer fail:
14129595ffdSJulian Elischer 	while (nitems > 0)
14229595ffdSJulian Elischer 		free(names[--nitems]);
14329595ffdSJulian Elischer 	free(names);
14429595ffdSJulian Elischer 	closedir(dirp);
1454176dd52SKonstantin Belousov 	return (-1);
14658f0484fSRodney W. Grimes }
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes /*
14958f0484fSRodney W. Grimes  * Alphabetic order comparison routine for those who want it.
1505fc5e42aSAndrey A. Chernov  * POSIX 2008 requires that alphasort() uses strcoll().
15158f0484fSRodney W. Grimes  */
15258f0484fSRodney W. Grimes int
1534176dd52SKonstantin Belousov alphasort(const struct dirent **d1, const struct dirent **d2)
15458f0484fSRodney W. Grimes {
1554176dd52SKonstantin Belousov 
156dcdafd0eSAndrey A. Chernov 	return (strcoll((*d1)->d_name, (*d2)->d_name));
15758f0484fSRodney W. Grimes }
158f5636f88SKonstantin Belousov 
159f5636f88SKonstantin Belousov static int
160f5636f88SKonstantin Belousov alphasort_thunk(void *thunk, const void *p1, const void *p2)
161f5636f88SKonstantin Belousov {
162f5636f88SKonstantin Belousov 	int (*dc)(const struct dirent **, const struct dirent **);
163f5636f88SKonstantin Belousov 
164f5636f88SKonstantin Belousov 	dc = *(int (**)(const struct dirent **, const struct dirent **))thunk;
165f5636f88SKonstantin Belousov 	return (dc((const struct dirent **)p1, (const struct dirent **)p2));
166f5636f88SKonstantin Belousov }
167