2 * Copyright (C) 2010-2012 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 static char *path_append(const char *dir, const char *path)
31 result = malloc(strlen(dir) + 1 + strlen(path) + 1);
40 static char **paths_add(char **paths, int n, char *path)
44 result = malloc((n+1) * sizeof(void *));
46 memcpy(result + 1, paths, n * sizeof(void *));
53 char **dir_list(const char *dpath, int (*filter) (const char *))
57 char **paths, *path, *name, **tmp;
66 paths = malloc(sizeof(void *));
69 while ((ent = readdir(dir)) != NULL) {
72 if (!strcmp(name, ".") || !strcmp(name, ".."))
75 path = path_append(dpath, name);
77 if (!filter || filter(path)) {
78 tmp = paths_add(paths, n, path);
93 void paths_free(char **paths)