xref: /minix3/lib/libc/gen/scandir.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*f14fb602SLionel Sambuc /*	$NetBSD: scandir.c,v 1.27 2012/03/13 21:13:36 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1983, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
342fe8fb19SBen Gras #if 0
352fe8fb19SBen Gras static char sccsid[] = "@(#)scandir.c	8.3 (Berkeley) 1/2/94";
362fe8fb19SBen Gras #else
37*f14fb602SLionel Sambuc __RCSID("$NetBSD: scandir.c,v 1.27 2012/03/13 21:13:36 christos Exp $");
382fe8fb19SBen Gras #endif
392fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
402fe8fb19SBen Gras 
412fe8fb19SBen Gras /*
422fe8fb19SBen Gras  * Scan the directory dirname calling selectfn to make a list of selected
432fe8fb19SBen Gras  * directory entries then sort using qsort and compare routine dcomp.
442fe8fb19SBen Gras  * Returns the number of entries and a pointer to a list of pointers to
452fe8fb19SBen Gras  * struct dirent (through namelist). Returns -1 if there were any errors.
462fe8fb19SBen Gras  */
472fe8fb19SBen Gras 
482fe8fb19SBen Gras #include "namespace.h"
492fe8fb19SBen Gras #include <sys/types.h>
502fe8fb19SBen Gras #include <sys/stat.h>
512fe8fb19SBen Gras 
522fe8fb19SBen Gras #include <assert.h>
532fe8fb19SBen Gras #include <errno.h>
542fe8fb19SBen Gras #include <dirent.h>
552fe8fb19SBen Gras #include <stdlib.h>
562fe8fb19SBen Gras #include <string.h>
572fe8fb19SBen Gras 
582fe8fb19SBen Gras /*
592fe8fb19SBen Gras  * Compute an estimate of the number of entries in a directory based on
602fe8fb19SBen Gras  * the file size. Returns the estimated number of entries or 0 on failure.
612fe8fb19SBen Gras  */
622fe8fb19SBen Gras static size_t
dirsize(int fd,size_t olen)632fe8fb19SBen Gras dirsize(int fd, size_t olen)
642fe8fb19SBen Gras {
652fe8fb19SBen Gras 	struct stat stb;
662fe8fb19SBen Gras 	size_t nlen;
672fe8fb19SBen Gras 
682fe8fb19SBen Gras 	if (fstat(fd, &stb) == -1)
692fe8fb19SBen Gras 		return 0;
702fe8fb19SBen Gras 	/*
712fe8fb19SBen Gras 	 * Estimate the array size by taking the size of the directory file
722fe8fb19SBen Gras 	 * and dividing it by a multiple of the minimum size entry.
732fe8fb19SBen Gras 	 */
742fe8fb19SBen Gras 	nlen = (size_t)(stb.st_size / _DIRENT_MINSIZE((struct dirent *)0));
752fe8fb19SBen Gras 	/*
762fe8fb19SBen Gras 	 * If the size turns up 0, switch to an alternate strategy and use the
772fe8fb19SBen Gras 	 * file size as the number of entries like ZFS returns. If that turns
782fe8fb19SBen Gras 	 * out to be 0 too return a minimum of 10 entries, plus the old length.
792fe8fb19SBen Gras 	 */
802fe8fb19SBen Gras 	if (nlen == 0)
812fe8fb19SBen Gras 		nlen = (size_t)(stb.st_size ? stb.st_size : 10);
822fe8fb19SBen Gras 	return olen + nlen;
832fe8fb19SBen Gras }
842fe8fb19SBen Gras 
852fe8fb19SBen Gras int
scandir(const char * dirname,struct dirent *** namelist,int (* selectfn)(const struct dirent *),int (* dcomp)(const void *,const void *))862fe8fb19SBen Gras scandir(const char *dirname, struct dirent ***namelist,
872fe8fb19SBen Gras     int (*selectfn)(const struct dirent *),
882fe8fb19SBen Gras     int (*dcomp)(const void *, const void *))
892fe8fb19SBen Gras {
902fe8fb19SBen Gras 	struct dirent *d, *p, **names, **newnames;
912fe8fb19SBen Gras 	size_t nitems, arraysz;
922fe8fb19SBen Gras 	DIR *dirp;
932fe8fb19SBen Gras 
942fe8fb19SBen Gras 	_DIAGASSERT(dirname != NULL);
952fe8fb19SBen Gras 	_DIAGASSERT(namelist != NULL);
962fe8fb19SBen Gras 
972fe8fb19SBen Gras 	if ((dirp = opendir(dirname)) == NULL)
982fe8fb19SBen Gras 		return -1;
992fe8fb19SBen Gras 
1002fe8fb19SBen Gras 	if ((arraysz = dirsize(dirp->dd_fd, 0)) == 0)
1012fe8fb19SBen Gras 		goto bad;
1022fe8fb19SBen Gras 
1032fe8fb19SBen Gras 	names = malloc(arraysz * sizeof(*names));
1042fe8fb19SBen Gras 	if (names == NULL)
1052fe8fb19SBen Gras 		goto bad;
1062fe8fb19SBen Gras 
1072fe8fb19SBen Gras 	nitems = 0;
1082fe8fb19SBen Gras 	while ((d = readdir(dirp)) != NULL) {
1092fe8fb19SBen Gras 		if (selectfn != NULL && !(*selectfn)(d))
1102fe8fb19SBen Gras 			continue;	/* just selected names */
1112fe8fb19SBen Gras 
1122fe8fb19SBen Gras 		/*
1132fe8fb19SBen Gras 		 * Check to make sure the array has space left and
1142fe8fb19SBen Gras 		 * realloc the maximum size.
1152fe8fb19SBen Gras 		 */
1162fe8fb19SBen Gras 		if (nitems >= arraysz) {
1172fe8fb19SBen Gras 			if ((arraysz = dirsize(dirp->dd_fd, arraysz)) == 0)
1182fe8fb19SBen Gras 				goto bad2;
1192fe8fb19SBen Gras 			newnames = realloc(names, arraysz * sizeof(*names));
1202fe8fb19SBen Gras 			if (newnames == NULL)
1212fe8fb19SBen Gras 				goto bad2;
1222fe8fb19SBen Gras 			names = newnames;
1232fe8fb19SBen Gras 		}
1242fe8fb19SBen Gras 
1252fe8fb19SBen Gras 		/*
1262fe8fb19SBen Gras 		 * Make a minimum size copy of the data
1272fe8fb19SBen Gras 		 */
1282fe8fb19SBen Gras 		p = malloc((size_t)_DIRENT_SIZE(d));
1292fe8fb19SBen Gras 		if (p == NULL)
1302fe8fb19SBen Gras 			goto bad2;
1312fe8fb19SBen Gras 		p->d_fileno = d->d_fileno;
1322fe8fb19SBen Gras 		p->d_reclen = d->d_reclen;
1332fe8fb19SBen Gras 		p->d_type = d->d_type;
1342fe8fb19SBen Gras 		p->d_namlen = d->d_namlen;
1352fe8fb19SBen Gras 		(void)memmove(p->d_name, d->d_name, (size_t)(p->d_namlen + 1));
1362fe8fb19SBen Gras 		names[nitems++] = p;
1372fe8fb19SBen Gras 	}
1382fe8fb19SBen Gras 	(void)closedir(dirp);
1392fe8fb19SBen Gras 	if (nitems && dcomp != NULL)
1402fe8fb19SBen Gras 		qsort(names, nitems, sizeof(*names), dcomp);
1412fe8fb19SBen Gras 	*namelist = names;
142*f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, nitems));
143*f14fb602SLionel Sambuc 	return (int)nitems;
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras bad2:
1462fe8fb19SBen Gras 	while (nitems-- > 0)
1472fe8fb19SBen Gras 		free(names[nitems]);
1482fe8fb19SBen Gras 	free(names);
1492fe8fb19SBen Gras bad:
1502fe8fb19SBen Gras 	(void)closedir(dirp);
1512fe8fb19SBen Gras 	return -1;
1522fe8fb19SBen Gras }
153