2 * Copyright (C) 2010-2014 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
19 #define _LARGEFILE_SOURCE 1
27 #include <sys/types.h>
32 /* Directory separator is \ when cross-compiling for MS Windows
34 #if defined(__MINGW32__)
40 #define FCOPY_BUF_SZ 4096
42 int is_dir(const char *path)
46 int ret = lstat(path, &st);
48 if (ret == 0 && S_ISDIR(st.st_mode))
54 int is_file(const char *path)
58 int ret = lstat(path, &st);
60 if (ret == 0 && S_ISREG(st.st_mode))
66 char *dir_normalize(const char *dpath)
71 if (!dpath || !strlen(dpath))
74 npath = strdup(dpath);
78 if (n > 1 && npath[n - 1] == '/')
84 static char **paths_add(char **paths, int n, char *path)
88 result = malloc((n+1) * sizeof(void *));
90 memcpy(result + 1, paths, n * sizeof(void *));
97 char **dir_list(const char *dpath, int (*filter) (const char *))
101 char **paths, *path, *name, **tmp;
104 dir = opendir(dpath);
110 paths = malloc(sizeof(void *));
113 while ((ent = readdir(dir)) != NULL) {
116 if (!strcmp(name, ".") || !strcmp(name, ".."))
119 path = path_append(dpath, name);
121 if (!filter || filter(path)) {
122 tmp = paths_add(paths, n, path);
137 void paths_free(char **paths)
151 char *file_get_content(const char *fpath)
157 size = file_get_size(fpath);
161 } else if (size == 0) {
166 FILE *fp = fopen(fpath, "rb");
168 page = malloc(size + 1);
169 if (!page || size != fread(page, 1, size, fp)) {
173 *(page + size) = '\0';
185 long file_get_size(const char *path)
193 fp = fopen(path, "rb");
195 if (fseek(fp, 0, SEEK_END) == -1)
208 #define FCOPY_BUF_SZ 4096
209 static int FILE_copy(FILE *src, FILE *dst)
212 char *buf = malloc(FCOPY_BUF_SZ);
216 return FILE_COPY_ERROR_ALLOC_BUFFER;
219 n = fread(buf, 1, FCOPY_BUF_SZ, src);
221 if (fwrite(buf, 1, n, dst) != n)
222 ret = FILE_COPY_ERROR_WRITE;
225 ret = FILE_COPY_ERROR_READ;
237 file_copy(const char *src, const char *dst)
242 log_fct("copy %s to %s", src, dst);
244 fsrc = fopen(src, "r");
247 fdst = fopen(dst, "w+");
250 ret = FILE_copy(fsrc, fdst);
253 ret = FILE_COPY_ERROR_OPEN_DST;
258 ret = FILE_COPY_ERROR_OPEN_SRC;
264 char *path_append(const char *dir, const char *path)
268 ndir = dir_normalize(dir);
270 if (!ndir && (!path || !strlen(path)))
276 } else if (!path || !strlen(path)) {
280 ret = malloc(strlen(ndir) + 1 + strlen(path) + 1);
291 void mkdirs(const char *dirs, mode_t mode)
296 log_fct("mkdirs %s", dirs);
299 dir = malloc(strlen(dirs) + 1);
303 if ((*c == DIRSEP || *c == '\0') && c != dirs) {
304 strncpy(dir, dirs, i);
319 file_copy_print_error(int code, const char *src, const char *dst)
324 case FILE_COPY_ERROR_OPEN_SRC:
325 printf("File copy error: failed to open %s.\n", src);
327 case FILE_COPY_ERROR_OPEN_DST:
328 printf("File copy error: failed to open %s.\n", dst);
330 case FILE_COPY_ERROR_READ:
331 printf("File copy error: failed to read %s.\n", src);
333 case FILE_COPY_ERROR_WRITE:
334 printf("File copy error: failed to write %s.\n", src);
336 case FILE_COPY_ERROR_ALLOC_BUFFER:
337 printf("File copy error: failed to allocate buffer.\n");
340 printf("File copy error: unknown error %d.\n", code);