/
filelist.c
318 lines (258 loc) · 7.27 KB
/
filelist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef _WIN32
/* No glob on Win32, but we can get it done for us by linking with
setargv.obj. On my MinGW install this (or some equivalent) seems
to be the default anyway. */
#include <glob.h>
#endif
#ifdef ZIPSUPPORT
#include <zip.h>
#endif
#include "lightcurves.h"
#include "util.h"
static int is_zip_file (char *filename, char *errstr);
struct input_file *read_file_list (int argc, char **argv,
int *nf_r, char *errstr) {
struct input_file *list = (struct input_file *) NULL;
FILE *fp = (FILE *) NULL;
char line[16384], *av, *p, *s;
int a, f, nf = 0;
#ifndef _WIN32
glob_t gbuf;
int rv, op;
/* Init */
gbuf.gl_pathv = (char **) NULL;
#endif
int is_zip;
#ifdef ZIPSUPPORT
zip_t *z = (zip_t *) NULL;
zip_error_t ze;
struct zip_stat sb;
int zerrno;
int ent, nent;
const char *name;
int namelen;
zip_uint8_t opsys;
zip_uint32_t extattr;
#endif
/* Loop over arguments */
for(a = 0; a < argc; a++) {
av = argv[a];
if(*av == '@') { /* @list form */
av++; /* skip past the @ */
is_zip = is_zip_file(av, errstr);
if(is_zip < 0)
goto error;
if(is_zip) {
#ifdef ZIPSUPPORT
z = zip_open(av, 0, &zerrno);
if(!z) {
zip_error_init_with_code(&ze, zerrno);
report_err(errstr, "zip_open: %s: %s",
av, zip_error_strerror(&ze));
zip_error_fini(&ze);
goto error;
}
nent = zip_get_num_entries(z, 0);
if(nent < 0) {
report_err(errstr, "zip_get_num_entries: %s",
zip_strerror(z));
goto error;
}
for(ent = 0; ent < nent; ent++) {
if(zip_stat_index(z, ent, 0, &sb)) {
report_err(errstr, "zip_stat_index(%d): %s\n",
ent, zip_strerror(z));
goto error;
}
if(zip_file_get_external_attributes(z, ent, 0, &opsys, &extattr)) {
report_err(errstr, "zip_get_external_attributes(%d): %s\n",
ent, zip_strerror(z));
goto error;
}
name = sb.name;
namelen = strlen(name);
if((opsys != ZIP_OPSYS_AMIGA && extattr & 0x10) ||
(namelen > 0 && name[namelen-1] == '/'))
/* Is directory */
continue;
/* Otherwise, add to list */
list = (struct input_file *) realloc(list, (nf+1) * sizeof(struct input_file));
if(!list) {
report_syserr(errstr, "realloc");
goto error;
}
s = strdup(name);
if(!s) {
report_syserr(errstr, "strdup");
goto error;
}
list[nf].filename = s;
list[nf].ient = ent;
list[nf].iarg = a;
list[nf].arg = av;
nf++;
}
if(zip_close(z)) {
report_err(errstr, "zip_close: %s",
zip_strerror(z));
goto error;
}
z = (zip_t *) NULL;
#else
report_err(errstr, "not compiled with zip support");
goto error;
#endif
}
else {
fp = fopen(av, "r");
if(!fp) {
report_syserr(errstr, "open: %s", av);
goto error;
}
while(fgets(line, sizeof(line), fp)) {
p = sstrip(line);
if(*p) {
list = (struct input_file *) realloc(list, (nf+1) * sizeof(struct input_file));
if(!list) {
report_syserr(errstr, "realloc");
goto error;
}
s = strdup(p);
if(!s) {
report_syserr(errstr, "strdup");
goto error;
}
list[nf].filename = s;
list[nf].ient = -1;
list[nf].iarg = a;
list[nf].arg = av;
nf++;
}
}
if(ferror(fp)) {
report_syserr(errstr, "read: %s", av);
goto error;
}
fclose(fp);
fp = (FILE *) NULL;
}
}
else { /* glob or single filename */
#ifndef _WIN32
rv = glob(av, 0, NULL, &gbuf);
if(rv == 0) { /* succeeded */
/* Allocate block */
list = (struct input_file *) realloc(list,
(nf+gbuf.gl_pathc) * sizeof(struct input_file));
if(!list) {
report_syserr(errstr, "realloc");
goto error;
}
/* Zero out the new pointers */
memset(list+nf, 0, gbuf.gl_pathc * sizeof(struct input_file));
/* Record so we know to free them */
op = nf;
nf += gbuf.gl_pathc;
for(f = 0; f < gbuf.gl_pathc; f++) {
s = strdup(gbuf.gl_pathv[f]);
if(!s) {
report_syserr(errstr, "strdup");
goto error;
}
list[op].filename = s;
list[op].ient = -1;
list[op].iarg = a;
list[op].arg = av;
op++;
}
globfree(&gbuf);
gbuf.gl_pathv = (char **) NULL;
}
else if(rv == GLOB_NOMATCH) { /* no match */
#endif /* _WIN32 */
/* Assume it's a single entry. */
list = (struct input_file *) realloc(list, (nf+1) * sizeof(struct input_file));
if(!list) {
report_syserr(errstr, "realloc");
goto error;
}
s = strdup(av);
if(!s) {
report_syserr(errstr, "strdup");
goto error;
}
list[nf].filename = s;
list[nf].ient = -1;
list[nf].iarg = a;
list[nf].arg = av;
nf++;
#ifndef _WIN32
}
else {
report_err(errstr, "glob error: %s", av);
goto error;
}
#endif
}
}
*nf_r = nf;
return(list);
error:
if(fp)
fclose(fp);
#ifndef _WIN32
if(gbuf.gl_pathv)
globfree(&gbuf);
#endif
#ifdef ZIPSUPPORT
if(z) {
zip_close(z);
z = (zip_t *) NULL;
}
#endif
if(list) {
for(f = 0; f < nf; f++)
if(list[f].filename)
free((void *) list[f].filename);
free((void *) list);
}
return((struct input_file *) NULL);
}
#define NMAGIC 4
static int is_zip_file (char *filename, char *errstr) {
FILE *fp;
char buf[NMAGIC];
int nread, rv;
fp = fopen(filename, "rb");
if(!fp) {
report_syserr(errstr, "open: %s", filename);
goto error;
}
/* Somewhat simplified method for detection of a zip file. This tests
for the signatures:
PK\x03\x04 local file header (normal file)
PK\x05\x06 end of central directory (empty file)
PK\x07\x08 multi-volume archive
which should cover most normal zip files, but strictly speaking we
should detect zip format by seeking to end of file and backing up
looking for the end of central directory record. */
nread = fread(buf, 1, NMAGIC, fp);
if(nread == NMAGIC &&
(buf[0] == 'P' && buf[1] == 'K' &&
((buf[2] == '\x03' && buf[3] == '\x04') ||
(buf[2] == '\x05' && buf[3] == '\x06') ||
(buf[2] == '\x07' && buf[3] == '\x08'))))
rv = 1;
else if(ferror(fp))
goto error;
else
rv = 0;
fclose(fp);
return(rv);
error:
return(-1);
}