xref: /dragonfly/lib/libc/gen/scandir.c (revision 37de577a)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/lib/libc/gen/scandir.c,v 1.5.6.1 2001/03/05 09:52:13 obrien Exp $
30  *
31  * @(#)scandir.c	8.3 (Berkeley) 1/2/94
32  */
33 
34 /*
35  * Scan the directory dirname calling select to make a list of selected
36  * directory entries then sort using qsort and compare routine dcomp.
37  * Returns the number of entries and a pointer to a list of pointers to
38  * struct dirent (through namelist). Returns -1 if there were any errors.
39  */
40 
41 #include "namespace.h"
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <dirent.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include "un-namespace.h"
49 
50 #include "gen_private.h"
51 
52 int
53 scandir(const char *dirname, struct dirent ***namelist,
54 	int (*select)(const struct dirent *),
55 	int (*dcomp)(const struct dirent **, const struct dirent **))
56 {
57 	struct dirent *d, *p, **names = NULL;
58 	size_t arraysz, nitems = 0;
59 	struct stat stb;
60 	DIR *dirp;
61 
62 	if ((dirp = opendir(dirname)) == NULL)
63 		return(-1);
64 	if (_fstat(dirp->dd_fd, &stb) < 0)
65 		goto fail;
66 
67 	/*
68 	 * Estimate the array size by taking the size of the directory file
69 	 * and dividing it by a multiple of the minimum size entry.
70 	 * Ensure that the size does fit into memory without overflooding.
71 	 */
72 	if (stb.st_size / 24 > SIZE_T_MAX)
73 		goto fail;
74 	arraysz = stb.st_size / 24;
75 	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
76 	if (names == NULL)
77 		goto fail;
78 
79 	while ((d = readdir(dirp)) != NULL) {
80 		if (select != NULL && !(*select)(d))
81 			continue;	/* just selected names */
82 		/*
83 		 * Make a minimum size copy of the data
84 		 */
85 		p = (struct dirent *)malloc(_DIRENT_DIRSIZ(d));
86 		if (p == NULL)
87 			goto fail;
88 		p->d_ino = d->d_ino;
89 		p->d_type = d->d_type;
90 		p->d_namlen = d->d_namlen;
91 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
92 		/*
93 		 * Check to make sure the array has space left and
94 		 * realloc the maximum size.
95 		 */
96 		if (nitems >= arraysz) {
97 			const int inc = 10;	/* increase by this much */
98 			struct dirent **names2;
99 
100 			names2 = (struct dirent **)realloc((char *)names,
101 				(arraysz + inc) * sizeof(struct dirent *));
102 			if (names2 == NULL) {
103 				free(p);
104 				goto fail;
105 			}
106 			names = names2;
107 			arraysz += inc;
108 		}
109 		names[nitems++] = p;
110 	}
111 	closedir(dirp);
112 	if (nitems && dcomp != NULL)
113 		qsort(names, nitems, sizeof(struct dirent *),
114 		    (int (*)(const void *, const void *))dcomp);
115 	*namelist = names;
116 	return(nitems);
117 
118 fail:
119 	while (nitems > 0)
120 		free(names[--nitems]);
121 	free(names);
122 	closedir(dirp);
123 	return -1;
124 }
125 
126 /*
127  * Alphabetic order comparison routine for those who want it.
128  */
129 int
130 alphasort(const struct dirent **d1, const struct dirent **d2)
131 {
132 	return(strcmp((*d1)->d_name, (*d2)->d_name));
133 }
134