2 Copyright (C) 2010-2011 jeanfi@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
28 int is_dir(const char *path)
32 int ret = lstat(path, &st);
34 if (ret == 0 && S_ISDIR(st.st_mode))
40 int is_file(const char *path)
44 int ret = lstat(path, &st);
46 if (ret == 0 && S_ISREG(st.st_mode))
52 char *dir_normalize(const char *dpath)
57 if (!dpath || !strlen(dpath))
60 npath = strdup(dpath);
64 if (n > 1 && npath[n - 1] == '/')
70 char **dir_list(const char *dpath, int (*filter) (const char *path))
83 paths = malloc(sizeof(void *));
86 while ((ent = readdir(dir)) != NULL) {
88 char *name = ent->d_name;
90 if (!strcmp(name, ".") || !strcmp(name, ".."))
93 fpath = malloc(strlen(dpath) + 1 + strlen(name) + 1);
99 if (!filter || filter(fpath)) {
103 npaths = malloc(n * sizeof(void *));
104 memcpy(npaths + 1, paths, (n - 1) * sizeof(void *));
119 void paths_free(char **paths)
133 char *file_get_content(const char *fpath)
139 size = file_get_size(fpath);
144 } else if (size == 0) {
149 FILE *fp = fopen(fpath, "rb");
151 page = malloc(size + 1);
152 if (!page || size != fread(page, 1, size, fp)) {
157 *(page + size) = '\0';
168 long file_get_size(const char *path)
175 fp = fopen(path, "rb");
179 if (fseek(fp, 0, SEEK_END) == -1)
192 #define FCOPY_BUF_SZ 4096
193 static int FILE_copy(FILE *src, FILE *dst)
196 char *buf = malloc(FCOPY_BUF_SZ);
200 return FILE_COPY_ERROR_ALLOC_BUFFER;
203 n = fread(buf, 1, FCOPY_BUF_SZ, src);
205 if (fwrite(buf, 1, n, dst) != n)
206 ret = FILE_COPY_ERROR_WRITE;
209 ret = FILE_COPY_ERROR_READ;
221 file_copy(const char *src, const char *dst)
226 fsrc = fopen(src, "r");
229 fdst = fopen(dst, "w+");
232 ret = FILE_copy(fsrc, fdst);
235 ret = FILE_COPY_ERROR_OPEN_DST;
240 ret = FILE_COPY_ERROR_OPEN_SRC;
246 char *path_append(const char *dir, const char *path)
250 ndir = dir_normalize(dir);
252 if (!ndir && (!path || !strlen(path)))
258 } else if (!path || !strlen(path)) {
262 ret = malloc(strlen(ndir) + 1 + strlen(path) + 1);
274 file_copy_print_error(int code, const char *src, const char *dst)
279 case FILE_COPY_ERROR_OPEN_SRC:
280 printf("File copy error: failed to open %s.\n", src);
282 case FILE_COPY_ERROR_OPEN_DST:
283 printf("File copy error: failed to open %s.\n", dst);
285 case FILE_COPY_ERROR_READ:
286 printf("File copy error: failed to read %s.\n", src);
288 case FILE_COPY_ERROR_WRITE:
289 printf("File copy error: failed to write %s.\n", src);
291 case FILE_COPY_ERROR_ALLOC_BUFFER:
292 printf("File copy error: failed to allocate buffer.\n");
295 printf("File copy error: unknown error %d.\n", code);