2 * Copyright (C) 2010-2011 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
27 int is_dir(const char *path)
31 int ret = lstat(path, &st);
33 if (ret == 0 && S_ISDIR(st.st_mode))
39 int is_file(const char *path)
43 int ret = lstat(path, &st);
45 if (ret == 0 && S_ISREG(st.st_mode))
51 char *dir_normalize(const char *dpath)
56 if (!dpath || !strlen(dpath))
59 npath = strdup(dpath);
63 if (n > 1 && npath[n - 1] == '/')
69 char **dir_list(const char *dpath, int (*filter) (const char *path))
82 paths = malloc(sizeof(void *));
85 while ((ent = readdir(dir)) != NULL) {
87 char *name = ent->d_name;
89 if (!strcmp(name, ".") || !strcmp(name, ".."))
92 fpath = malloc(strlen(dpath) + 1 + strlen(name) + 1);
98 if (!filter || filter(fpath)) {
102 npaths = malloc(n * sizeof(void *));
103 memcpy(npaths + 1, paths, (n - 1) * sizeof(void *));
118 void paths_free(char **paths)
132 char *file_get_content(const char *fpath)
138 size = file_get_size(fpath);
143 } else if (size == 0) {
148 FILE *fp = fopen(fpath, "rb");
150 page = malloc(size + 1);
151 if (!page || size != fread(page, 1, size, fp)) {
156 *(page + size) = '\0';
167 long file_get_size(const char *path)
174 fp = fopen(path, "rb");
178 if (fseek(fp, 0, SEEK_END) == -1)
191 #define FCOPY_BUF_SZ 4096
192 static int FILE_copy(FILE *src, FILE *dst)
195 char *buf = malloc(FCOPY_BUF_SZ);
199 return FILE_COPY_ERROR_ALLOC_BUFFER;
202 n = fread(buf, 1, FCOPY_BUF_SZ, src);
204 if (fwrite(buf, 1, n, dst) != n)
205 ret = FILE_COPY_ERROR_WRITE;
208 ret = FILE_COPY_ERROR_READ;
220 file_copy(const char *src, const char *dst)
225 fsrc = fopen(src, "r");
228 fdst = fopen(dst, "w+");
231 ret = FILE_copy(fsrc, fdst);
234 ret = FILE_COPY_ERROR_OPEN_DST;
239 ret = FILE_COPY_ERROR_OPEN_SRC;
245 char *path_append(const char *dir, const char *path)
249 ndir = dir_normalize(dir);
251 if (!ndir && (!path || !strlen(path)))
257 } else if (!path || !strlen(path)) {
261 ret = malloc(strlen(ndir) + 1 + strlen(path) + 1);
273 file_copy_print_error(int code, const char *src, const char *dst)
278 case FILE_COPY_ERROR_OPEN_SRC:
279 printf("File copy error: failed to open %s.\n", src);
281 case FILE_COPY_ERROR_OPEN_DST:
282 printf("File copy error: failed to open %s.\n", dst);
284 case FILE_COPY_ERROR_READ:
285 printf("File copy error: failed to read %s.\n", src);
287 case FILE_COPY_ERROR_WRITE:
288 printf("File copy error: failed to write %s.\n", src);
290 case FILE_COPY_ERROR_ALLOC_BUFFER:
291 printf("File copy error: failed to allocate buffer.\n");
294 printf("File copy error: unknown error %d.\n", code);