~jfi/ubuntu/quantal/psensor/new-upstream

« back to all changes in this revision

Viewing changes to src/plib/plib_io.c

  • Committer: Bazaar Package Importer
  • Author(s): Jean-Philippe Orsini
  • Date: 2011-02-20 15:15:30 UTC
  • Revision ID: james.westby@ubuntu.com-20110220151530-5xzhb3o39yaegrn0
Tags: upstream-0.6.1.5
ImportĀ upstreamĀ versionĀ 0.6.1.5

Show diffs side-by-side

added added

removed removed

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