2 * Copyright (C) 2011-2012 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 /* Directory separator is \ when cross-compiling for MS Windows
29 #if defined(__MINGW32__)
35 #define FCOPY_BUF_SZ 4096
37 static int file_copy(FILE *src, FILE *dst)
40 char *buf = malloc(FCOPY_BUF_SZ);
44 return FILE_COPY_ERROR_ALLOC_BUFFER;
47 n = fread(buf, 1, FCOPY_BUF_SZ, src);
49 if (fwrite(buf, 1, n, dst) != n)
50 ret = FILE_COPY_ERROR_WRITE;
53 ret = FILE_COPY_ERROR_READ;
64 int fcopy(const char *src, const char *dst)
69 fsrc = fopen(src, "r");
72 fdst = fopen(dst, "w+");
75 ret = file_copy(fsrc, fdst);
78 ret = FILE_COPY_ERROR_OPEN_DST;
83 ret = FILE_COPY_ERROR_OPEN_SRC;
89 char *path_append(const char *odir, const char *name)
93 dir = malloc(strlen(odir)+1+strlen(name)+1);
96 if (odir[strlen(odir) - 1] != '/' && name[0] != '/')
103 void mkdirs(const char *dirs, mode_t mode)
105 char *c = (char *)dirs;
106 char *dir = malloc(strlen(dirs) + 1);
110 if ((*c == DIRSEP || *c == '\0') && c != dirs) {
111 strncpy(dir, dirs, i);
125 static int is_file(const char *path)
129 int ret = lstat(path, &st);
131 if (ret == 0 && S_ISREG(st.st_mode))
137 static long file_get_size(const char *path)
144 fp = fopen(path, "rb");
148 if (fseek(fp, 0, SEEK_END) == -1)
161 char *file_get_content(const char *fpath)
167 size = file_get_size(fpath);
171 } else if (size == 0) {
176 FILE *fp = fopen(fpath, "rb");
178 page = malloc(size + 1);
179 if (!page || size != fread(page, 1, size, fp)) {
184 *(page + size) = '\0';