From: Jean-Philippe Orsini Date: Mon, 9 Jan 2012 08:28:46 +0000 (+0000) Subject: style,cleanup X-Git-Tag: v0.8.0.5~493 X-Git-Url: http://git.wpitchoune.net/gitweb/?a=commitdiff_plain;h=cd8a0e371e873542fad55ef6276152c3cb62e0ee;p=psensor.git style,cleanup --- diff --git a/src/lib/pio.c b/src/lib/pio.c index 3cfb02d..6d2293f 100644 --- a/src/lib/pio.c +++ b/src/lib/pio.c @@ -24,11 +24,37 @@ #include "pio.h" -char **dir_list(const char *dpath, int (*filter) (const char *path)) +static char *path_append(const char *dir, const char *path) +{ + char *result; + + result = malloc(strlen(dir) + 1 + strlen(path) + 1); + + strcpy(result, dir); + strcat(result, "/"); + strcat(result, path); + + return result; +} + +static char **paths_add(char **paths, int n, char *path) +{ + char **result; + + result = malloc((n+1) * sizeof(void *)); + + memcpy(result + 1, paths, n * sizeof(void *)); + + *result = path; + + return result; +} + +char **dir_list(const char *dpath, int (*filter) (const char *)) { struct dirent *ent; DIR *dir; - char **paths; + char **paths, *path, *name, **tmp; int n; dir = opendir(dpath); @@ -41,30 +67,21 @@ char **dir_list(const char *dpath, int (*filter) (const char *path)) *paths = NULL; while ((ent = readdir(dir)) != NULL) { - char *fpath; - char *name = ent->d_name; + name = ent->d_name; if (!strcmp(name, ".") || !strcmp(name, "..")) continue; - fpath = malloc(strlen(dpath) + 1 + strlen(name) + 1); + path = path_append(dpath, name); - strcpy(fpath, dpath); - strcat(fpath, "/"); - strcat(fpath, name); - - if (!filter || filter(fpath)) { - char **npaths; - - n++; - npaths = malloc(n * sizeof(void *)); - memcpy(npaths + 1, paths, (n - 1) * sizeof(void *)); + if (!filter || filter(path)) { + tmp = paths_add(paths, n, path); free(paths); - paths = npaths; - *npaths = fpath; + paths = tmp; + n++; } else { - free(fpath); + free(path); } }