2 Copyright (C) 2010-2014 wpitchoune@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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
25 #include <sys/types.h>
30 /* Directory separator is \ when cross-compiling for MS Windows
32 #if defined(__MINGW32__)
38 #define FCOPY_BUF_SZ 4096
40 int is_dir(const char *path)
44 int ret = lstat(path, &st);
46 if (ret == 0 && S_ISDIR(st.st_mode))
52 int is_file(const char *path)
56 int ret = lstat(path, &st);
58 if (ret == 0 && S_ISREG(st.st_mode))
64 char *dir_normalize(const char *dpath)
69 if (!dpath || !strlen(dpath))
72 npath = strdup(dpath);
76 if (n > 1 && npath[n - 1] == '/')
82 char **dir_list(const char *dpath, int (*filter) (const char *path))
95 paths = malloc(sizeof(void *));
98 while ((ent = readdir(dir)) != NULL) {
100 char *name = ent->d_name;
102 if (!strcmp(name, ".") || !strcmp(name, ".."))
105 fpath = malloc(strlen(dpath) + 1 + strlen(name) + 1);
107 strcpy(fpath, dpath);
111 if (!filter || filter(fpath)) {
115 npaths = malloc(n * sizeof(void *));
116 memcpy(npaths + 1, paths, (n - 1) * sizeof(void *));
131 void paths_free(char **paths)
145 char *file_get_content(const char *fpath)
151 size = file_get_size(fpath);
155 } else if (size == 0) {
160 FILE *fp = fopen(fpath, "rb");
162 page = malloc(size + 1);
163 if (!page || size != fread(page, 1, size, fp)) {
168 *(page + size) = '\0';
179 long file_get_size(const char *path)
186 fp = fopen(path, "rb");
190 if (fseek(fp, 0, SEEK_END) == -1)
203 #define FCOPY_BUF_SZ 4096
204 static int FILE_copy(FILE *src, FILE *dst)
207 char *buf = malloc(FCOPY_BUF_SZ);
211 return FILE_COPY_ERROR_ALLOC_BUFFER;
214 n = fread(buf, 1, FCOPY_BUF_SZ, src);
216 if (fwrite(buf, 1, n, dst) != n)
217 ret = FILE_COPY_ERROR_WRITE;
220 ret = FILE_COPY_ERROR_READ;
232 file_copy(const char *src, const char *dst)
237 fsrc = fopen(src, "r");
240 fdst = fopen(dst, "w+");
243 ret = FILE_copy(fsrc, fdst);
246 ret = FILE_COPY_ERROR_OPEN_DST;
251 ret = FILE_COPY_ERROR_OPEN_SRC;
257 char *path_append(const char *dir, const char *path)
261 ndir = dir_normalize(dir);
263 if (!ndir && (!path || !strlen(path)))
269 } else if (!path || !strlen(path)) {
273 ret = malloc(strlen(ndir) + 1 + strlen(path) + 1);
284 void mkdirs(const char *dirs, mode_t mode)
286 char *c = (char *)dirs;
287 char *dir = malloc(strlen(dirs) + 1);
291 if ((*c == DIRSEP || *c == '\0') && c != dirs) {
292 strncpy(dir, dirs, i);
307 file_copy_print_error(int code, const char *src, const char *dst)
312 case FILE_COPY_ERROR_OPEN_SRC:
313 printf("File copy error: failed to open %s.\n", src);
315 case FILE_COPY_ERROR_OPEN_DST:
316 printf("File copy error: failed to open %s.\n", dst);
318 case FILE_COPY_ERROR_READ:
319 printf("File copy error: failed to read %s.\n", src);
321 case FILE_COPY_ERROR_WRITE:
322 printf("File copy error: failed to write %s.\n", src);
324 case FILE_COPY_ERROR_ALLOC_BUFFER:
325 printf("File copy error: failed to allocate buffer.\n");
328 printf("File copy error: unknown error %d.\n", code);