Imported Upstream version 1.1.2
[psensor-pkg-ubuntu.git] / src / lib / pio.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #define _LARGEFILE_SOURCE 1
20 #include "config.h"
21
22 #include <dirent.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 #include <plog.h>
30 #include <pio.h>
31
32 /* Directory separator is \ when cross-compiling for MS Windows
33    systems */
34 #if defined(__MINGW32__)
35 #define DIRSEP ('\\')
36 #else
37 #define DIRSEP '/'
38 #endif
39
40 #define FCOPY_BUF_SZ 4096
41
42 int is_dir(const char *path)
43 {
44         struct stat st;
45
46         int ret = lstat(path, &st);
47
48         if (ret == 0 && S_ISDIR(st.st_mode))
49                 return 1;
50
51         return 0;
52 }
53
54 int is_file(const char *path)
55 {
56         struct stat st;
57
58         int ret = lstat(path, &st);
59
60         if (ret == 0 && S_ISREG(st.st_mode))
61                 return 1;
62
63         return 0;
64 }
65
66 char *dir_normalize(const char *dpath)
67 {
68         char *npath;
69         int n;
70
71         if (!dpath || !strlen(dpath))
72                 return NULL;
73
74         npath = strdup(dpath);
75
76         n = strlen(npath);
77
78         if (n > 1 && npath[n - 1] == '/')
79                 npath[n - 1] = '\0';
80
81         return npath;
82 }
83
84 static char **paths_add(char **paths, int n, char *path)
85 {
86         char **result;
87
88         result = malloc((n+1) * sizeof(void *));
89
90         memcpy(result + 1, paths, n * sizeof(void *));
91
92         *result = path;
93
94         return result;
95 }
96
97 char **dir_list(const char *dpath, int (*filter) (const char *))
98 {
99         struct dirent *ent;
100         DIR *dir;
101         char **paths, *path, *name, **tmp;
102         int n;
103
104         dir = opendir(dpath);
105
106         if (!dir)
107                 return NULL;
108
109         n = 1;
110         paths = malloc(sizeof(void *));
111         *paths = NULL;
112
113         while ((ent = readdir(dir)) != NULL) {
114                 name = ent->d_name;
115
116                 if (!strcmp(name, ".") || !strcmp(name, ".."))
117                         continue;
118
119                 path = path_append(dpath, name);
120
121                 if (!filter || filter(path)) {
122                         tmp = paths_add(paths, n, path);
123                         free(paths);
124                         paths = tmp;
125
126                         n++;
127                 } else {
128                         free(path);
129                 }
130         }
131
132         closedir(dir);
133
134         return paths;
135 }
136
137 void paths_free(char **paths)
138 {
139         char **paths_cur;
140
141         paths_cur = paths;
142         while (*paths_cur) {
143                 free(*paths_cur);
144
145                 paths_cur++;
146         }
147
148         free(paths);
149 }
150
151 char *file_get_content(const char *fpath)
152 {
153         long size;
154
155         char *page;
156
157         size = file_get_size(fpath);
158         if (size == -1) {
159                 page = NULL;
160
161         } else if (size == 0) {
162                 page = malloc(1);
163                 *page = '\0';
164
165         } else {
166                 FILE *fp = fopen(fpath, "rb");
167
168                 if (fp) {
169                         page = malloc(size + 1);
170                         if (!page || size != fread(page, 1, size, fp)) {
171                                 free(page);
172                                 page = NULL;
173                         } else {
174                                 *(page + size) = '\0';
175                         }
176
177                         fclose(fp);
178                 } else {
179                         page = NULL;
180                 }
181         }
182
183         return page;
184 }
185
186 long file_get_size(const char *path)
187 {
188         FILE *fp;
189         long size;
190
191         if (!is_file(path))
192                 return -1;
193
194         fp = fopen(path, "rb");
195         if (fp) {
196                 if (fseek(fp, 0, SEEK_END) == -1)
197                         size = -1;
198                 else
199                         size = ftell(fp);
200
201                 fclose(fp);
202         } else {
203                 size = -1;
204         }
205
206         return size;
207 }
208
209 #define FCOPY_BUF_SZ 4096
210 static int FILE_copy(FILE *src, FILE *dst)
211 {
212         int ret = 0;
213         char *buf = malloc(FCOPY_BUF_SZ);
214         int n;
215
216         if (!buf)
217                 return FILE_COPY_ERROR_ALLOC_BUFFER;
218
219         while (!ret) {
220                 n = fread(buf, 1, FCOPY_BUF_SZ, src);
221                 if (n) {
222                         if (fwrite(buf, 1, n, dst) != n)
223                                 ret = FILE_COPY_ERROR_WRITE;
224                 } else {
225                         if (!feof(src))
226                                 ret = FILE_COPY_ERROR_READ;
227                         else
228                                 break;
229                 }
230         }
231
232         free(buf);
233
234         return ret;
235 }
236
237 int
238 file_copy(const char *src, const char *dst)
239 {
240         FILE *fsrc, *fdst;
241         int ret = 0;
242
243         log_fct("copy %s to %s", src, dst);
244
245         fsrc = fopen(src, "r");
246
247         if (fsrc) {
248                 fdst = fopen(dst, "w+");
249
250                 if (fdst) {
251                         ret = FILE_copy(fsrc, fdst);
252                         fclose(fdst);
253                 } else {
254                         ret = FILE_COPY_ERROR_OPEN_DST;
255                 }
256
257                 fclose(fsrc);
258         } else {
259                 ret = FILE_COPY_ERROR_OPEN_SRC;
260         }
261
262         return ret;
263 }
264
265 char *path_append(const char *dir, const char *path)
266 {
267         char *ret, *ndir;
268
269         ndir = dir_normalize(dir);
270
271         if (!ndir && (!path || !strlen(path)))
272                 ret = NULL;
273
274         else if (!ndir) {
275                 ret = strdup(path);
276
277         } else if (!path || !strlen(path)) {
278                 return ndir;
279
280         } else {
281                 ret = malloc(strlen(ndir) + 1 + strlen(path) + 1);
282                 strcpy(ret, ndir);
283                 strcat(ret, "/");
284                 strcat(ret, path);
285         }
286
287         free(ndir);
288
289         return ret;
290 }
291
292 void mkdirs(const char *dirs, mode_t mode)
293 {
294         char *c, *dir;
295         int i;
296
297         log_fct("mkdirs %s", dirs);
298
299         c = (char *)dirs;
300         dir = malloc(strlen(dirs) + 1);
301
302         i = 0;
303         while (*c) {
304                 if ((*c == DIRSEP || *c == '\0') && c != dirs) {
305                         strncpy(dir, dirs, i);
306                         dir[i] = '\0';
307                         mkdir(dir, mode);
308                 }
309
310                 c++;
311                 i++;
312         }
313
314         mkdir(dirs, mode);
315
316         free(dir);
317 }
318
319 void
320 file_copy_print_error(int code, const char *src, const char *dst)
321 {
322         switch (code) {
323         case 0:
324                 break;
325         case FILE_COPY_ERROR_OPEN_SRC:
326                 printf("File copy error: failed to open %s.\n", src);
327                 break;
328         case FILE_COPY_ERROR_OPEN_DST:
329                 printf("File copy error: failed to open %s.\n", dst);
330                 break;
331         case FILE_COPY_ERROR_READ:
332                 printf("File copy error: failed to read %s.\n", src);
333                 break;
334         case FILE_COPY_ERROR_WRITE:
335                 printf("File copy error: failed to write %s.\n", src);
336                 break;
337         case FILE_COPY_ERROR_ALLOC_BUFFER:
338                 printf("File copy error: failed to allocate buffer.\n");
339                 break;
340         default:
341                 printf("File copy error: unknown error %d.\n", code);
342         }
343 }