2 * libcompat - system compatibility library
4 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
23 #include <sys/types.h>
32 cleanup(DIR *dir, struct dirent **dirlist, int used)
40 for (i = 0; i < used; i++)
49 scandir(const char *dir, struct dirent ***namelist,
50 int (*filter)(const struct dirent *),
51 int (*cmp)(const void *, const void *))
54 struct dirent *e, *m, **list;
64 while ((e = readdir(d)) != NULL) {
65 if (filter != NULL && !filter(e))
68 if (used >= avail - 1) {
69 struct dirent **newlist;
75 newlist = realloc(list, avail * sizeof(struct dirent *));
77 return cleanup(d, list, used);
81 m = malloc(sizeof(struct dirent) + strlen(e->d_name));
83 return cleanup(d, list, used);
85 strcpy(m->d_name, e->d_name);
93 if (list != NULL && cmp != NULL)
94 qsort(list, used, sizeof(struct dirent *), cmp);