1 /*
2  * libcompat - system compatibility library
3  *
4  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
5  * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include <sys/types.h>
24 
25 #include <string.h>
26 #include <dirent.h>
27 #include <stdlib.h>
28 
29 #include "compat.h"
30 
31 static int
cleanup(DIR * dir,struct dirent ** dirlist,int used)32 cleanup(DIR *dir, struct dirent **dirlist, int used)
33 {
34 	if (dir)
35 		closedir(dir);
36 
37 	if (dirlist) {
38 		int i;
39 
40 		for (i = 0; i < used; i++)
41 			free(dirlist[i]);
42 		free(dirlist);
43 	}
44 
45 	return -1;
46 }
47 
48 int
scandir(const char * dir,struct dirent *** namelist,int (* filter)(const struct dirent *),int (* cmp)(const void *,const void *))49 scandir(const char *dir, struct dirent ***namelist,
50         int (*filter)(const struct dirent *),
51         int (*cmp)(const void *, const void *))
52 {
53 	DIR *d;
54 	struct dirent *e, *m, **list;
55 	int used, avail;
56 
57 	d = opendir(dir);
58 	if (!d)
59 		return -1;
60 
61 	list = NULL;
62 	used = avail = 0;
63 
64 	while ((e = readdir(d)) != NULL) {
65 		if (filter != NULL && !filter(e))
66 			continue;
67 
68 		if (used >= avail - 1) {
69 			struct dirent **newlist;
70 
71 			if (avail)
72 				avail *= 2;
73 			else
74 				avail = 20;
75 			newlist = realloc(list, avail * sizeof(*newlist));
76 			if (!newlist)
77 				return cleanup(d, list, used);
78 			list = newlist;
79 		}
80 
81 		m = malloc(sizeof(*m) + strlen(e->d_name));
82 		if (!m)
83 			return cleanup(d, list, used);
84 		*m = *e;
85 		strcpy(m->d_name, e->d_name);
86 
87 		list[used] = m;
88 		used++;
89 	}
90 
91 	closedir(d);
92 
93 	if (list != NULL && cmp != NULL)
94 		qsort(list, used, sizeof(list[0]), cmp);
95 
96 	*namelist = list;
97 
98 	return used;
99 }
100