Imported Upstream version 1.3.2
[ppastats-pkg-ubuntu.git] / src / pio.c
index 701823d..2f806b8 100644 (file)
--- a/src/pio.c
+++ b/src/pio.c
@@ -1,21 +1,23 @@
 /*
-    Copyright (C) 2010-2014 wpitchoune@gmail.com
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
+ * Copyright (C) 2010-2014 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+#define _LARGEFILE_SOURCE 1
+#include "config.h"
 
 #include <dirent.h>
 #include <fts.h>
@@ -80,11 +82,24 @@ char *dir_normalize(const char *dpath)
        return npath;
 }
 
-char **dir_list(const char *dpath, int (*filter) (const char *path))
+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);
@@ -97,30 +112,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);
                }
        }
 
@@ -163,11 +169,11 @@ char *file_get_content(const char *fpath)
                        page = malloc(size + 1);
                        if (!page || size != fread(page, 1, size, fp)) {
                                free(page);
-                               return NULL;
+                               page = NULL;
+                       } else {
+                               *(page + size) = '\0';
                        }
 
-                       *(page + size) = '\0';
-
                        fclose(fp);
                } else {
                        page = NULL;
@@ -180,25 +186,24 @@ char *file_get_content(const char *fpath)
 long file_get_size(const char *path)
 {
        FILE *fp;
+       long size;
 
        if (!is_file(path))
                return -1;
 
        fp = fopen(path, "rb");
        if (fp) {
-               long size;
-
                if (fseek(fp, 0, SEEK_END) == -1)
-                       return -1;
-
-               size = ftell(fp);
+                       size = -1;
+               else
+                       size = ftell(fp);
 
                fclose(fp);
-
-               return size;
+       } else {
+               size = -1;
        }
 
-       return -1;
+       return size;
 }
 
 #define FCOPY_BUF_SZ 4096