removed some useless code
[psensor.git] / src / lib / pio.c
1 /*
2  * Copyright (C) 2010-2011 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <sys/stat.h>
22 #include <string.h>
23 #include <dirent.h>
24
25 #include "pio.h"
26
27 char **dir_list(const char *dpath, int (*filter) (const char *path))
28 {
29         struct dirent *ent;
30         DIR *dir;
31         char **paths;
32         int n;
33
34         dir = opendir(dpath);
35
36         if (!dir)
37                 return NULL;
38
39         n = 1;
40         paths = malloc(sizeof(void *));
41         *paths = NULL;
42
43         while ((ent = readdir(dir)) != NULL) {
44                 char *fpath;
45                 char *name = ent->d_name;
46
47                 if (!strcmp(name, ".") || !strcmp(name, ".."))
48                         continue;
49
50                 fpath = malloc(strlen(dpath) + 1 + strlen(name) + 1);
51
52                 strcpy(fpath, dpath);
53                 strcat(fpath, "/");
54                 strcat(fpath, name);
55
56                 if (!filter || filter(fpath)) {
57                         char **npaths;
58
59                         n++;
60                         npaths = malloc(n * sizeof(void *));
61                         memcpy(npaths + 1, paths, (n - 1) * sizeof(void *));
62                         free(paths);
63                         paths = npaths;
64                         *npaths = fpath;
65
66                 } else {
67                         free(fpath);
68                 }
69         }
70
71         closedir(dir);
72
73         return paths;
74 }
75
76 void paths_free(char **paths)
77 {
78         char **paths_cur;
79
80         paths_cur = paths;
81         while (*paths_cur) {
82                 free(*paths_cur);
83
84                 paths_cur++;
85         }
86
87         free(paths);
88 }
89