2
Copyright (C) 2010-2011 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
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);
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);