xref: /freebsd/lib/libc/gen/scandir.c (revision dcdafd0e)
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.
1358f0484fSRodney W. Grimes  * 4. 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 
49f5636f88SKonstantin Belousov static int alphasort_thunk(void *thunk, const void *p1, const void *p2);
50f5636f88SKonstantin Belousov 
5158f0484fSRodney W. Grimes /*
5258f0484fSRodney W. Grimes  * The DIRSIZ macro is the minimum record length which will hold the directory
5358f0484fSRodney W. Grimes  * entry.  This requires the amount of space in struct dirent without the
5458f0484fSRodney W. Grimes  * d_name field, plus enough space for the name and a terminating nul byte
5558f0484fSRodney W. Grimes  * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
5658f0484fSRodney W. Grimes  */
5758f0484fSRodney W. Grimes #undef DIRSIZ
5858f0484fSRodney W. Grimes #define DIRSIZ(dp)							\
5958f0484fSRodney W. Grimes 	((sizeof(struct dirent) - sizeof(dp)->d_name) +			\
6058f0484fSRodney W. Grimes 	    (((dp)->d_namlen + 1 + 3) &~ 3))
6158f0484fSRodney W. Grimes 
6258f0484fSRodney W. Grimes int
634176dd52SKonstantin Belousov scandir(const char *dirname, struct dirent ***namelist,
644176dd52SKonstantin Belousov     int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
654176dd52SKonstantin Belousov 	const struct dirent **))
6658f0484fSRodney W. Grimes {
67b231cb39SDavid E. O'Brien 	struct dirent *d, *p, **names = NULL;
68b231cb39SDavid E. O'Brien 	size_t nitems = 0;
6958f0484fSRodney W. Grimes 	long arraysz;
7058f0484fSRodney W. Grimes 	DIR *dirp;
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes 	if ((dirp = opendir(dirname)) == NULL)
7358f0484fSRodney W. Grimes 		return(-1);
7458f0484fSRodney W. Grimes 
7518798c64SDavid Schultz 	arraysz = 32;	/* initial estimate of the array size */
7658f0484fSRodney W. Grimes 	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
7758f0484fSRodney W. Grimes 	if (names == NULL)
7829595ffdSJulian Elischer 		goto fail;
7958f0484fSRodney W. Grimes 
8058f0484fSRodney W. Grimes 	while ((d = readdir(dirp)) != NULL) {
8158f0484fSRodney W. Grimes 		if (select != NULL && !(*select)(d))
8258f0484fSRodney W. Grimes 			continue;	/* just selected names */
8358f0484fSRodney W. Grimes 		/*
8458f0484fSRodney W. Grimes 		 * Make a minimum size copy of the data
8558f0484fSRodney W. Grimes 		 */
8658f0484fSRodney W. Grimes 		p = (struct dirent *)malloc(DIRSIZ(d));
8758f0484fSRodney W. Grimes 		if (p == NULL)
8829595ffdSJulian Elischer 			goto fail;
89e169c667SPoul-Henning Kamp 		p->d_fileno = d->d_fileno;
90e169c667SPoul-Henning Kamp 		p->d_type = d->d_type;
9158f0484fSRodney W. Grimes 		p->d_reclen = d->d_reclen;
9258f0484fSRodney W. Grimes 		p->d_namlen = d->d_namlen;
9358f0484fSRodney W. Grimes 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
9458f0484fSRodney W. Grimes 		/*
9558f0484fSRodney W. Grimes 		 * Check to make sure the array has space left and
9658f0484fSRodney W. Grimes 		 * realloc the maximum size.
9758f0484fSRodney W. Grimes 		 */
9829595ffdSJulian Elischer 		if (nitems >= arraysz) {
9929595ffdSJulian Elischer 			struct dirent **names2;
10029595ffdSJulian Elischer 
10129595ffdSJulian Elischer 			names2 = (struct dirent **)realloc((char *)names,
10218798c64SDavid Schultz 				(arraysz * 2) * sizeof(struct dirent *));
10329595ffdSJulian Elischer 			if (names2 == NULL) {
10429595ffdSJulian Elischer 				free(p);
10529595ffdSJulian Elischer 				goto fail;
10658f0484fSRodney W. Grimes 			}
10729595ffdSJulian Elischer 			names = names2;
10818798c64SDavid Schultz 			arraysz *= 2;
10929595ffdSJulian Elischer 		}
11029595ffdSJulian Elischer 		names[nitems++] = p;
11158f0484fSRodney W. Grimes 	}
11258f0484fSRodney W. Grimes 	closedir(dirp);
11358f0484fSRodney W. Grimes 	if (nitems && dcomp != NULL)
114f5636f88SKonstantin Belousov 		qsort_r(names, nitems, sizeof(struct dirent *),
115f5636f88SKonstantin Belousov 		    &dcomp, alphasort_thunk);
11658f0484fSRodney W. Grimes 	*namelist = names;
11758f0484fSRodney W. Grimes 	return (nitems);
11829595ffdSJulian Elischer 
11929595ffdSJulian Elischer fail:
12029595ffdSJulian Elischer 	while (nitems > 0)
12129595ffdSJulian Elischer 		free(names[--nitems]);
12229595ffdSJulian Elischer 	free(names);
12329595ffdSJulian Elischer 	closedir(dirp);
1244176dd52SKonstantin Belousov 	return (-1);
12558f0484fSRodney W. Grimes }
12658f0484fSRodney W. Grimes 
12758f0484fSRodney W. Grimes /*
12858f0484fSRodney W. Grimes  * Alphabetic order comparison routine for those who want it.
129f5636f88SKonstantin Belousov  *
130dcdafd0eSAndrey A. Chernov  * POSIX 2008 requires the alphasort() to use strcoll().
13158f0484fSRodney W. Grimes  */
13258f0484fSRodney W. Grimes int
1334176dd52SKonstantin Belousov alphasort(const struct dirent **d1, const struct dirent **d2)
13458f0484fSRodney W. Grimes {
1354176dd52SKonstantin Belousov 
136dcdafd0eSAndrey A. Chernov 	return (strcoll((*d1)->d_name, (*d2)->d_name));
13758f0484fSRodney W. Grimes }
138f5636f88SKonstantin Belousov 
139f5636f88SKonstantin Belousov static int
140f5636f88SKonstantin Belousov alphasort_thunk(void *thunk, const void *p1, const void *p2)
141f5636f88SKonstantin Belousov {
142f5636f88SKonstantin Belousov 	int (*dc)(const struct dirent **, const struct dirent **);
143f5636f88SKonstantin Belousov 
144f5636f88SKonstantin Belousov 	dc = *(int (**)(const struct dirent **, const struct dirent **))thunk;
145f5636f88SKonstantin Belousov 	return (dc((const struct dirent **)p1, (const struct dirent **)p2));
146f5636f88SKonstantin Belousov }
147