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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
/* Copyright (C) 2001-2006 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied, modified
or distributed except as expressly authorized under the terms of that
license. Refer to licensing information at http://www.artifex.com/
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id$ */
/* "Unix-like" file system platform routines for Ghostscript */
#include "stdio_.h" /* for FILENAME_MAX */
#include "memory_.h"
#include "string_.h"
#include "gx.h"
#include "gp.h"
#include "gpmisc.h"
#include "gsstruct.h"
#include "gsutil.h" /* for string_match */
#include "stat_.h"
#include "dirent_.h"
#include "unistd_.h"
#include <stdlib.h> /* for mkstemp/mktemp */
/* Provide a definition of the maximum path length in case the system
* headers don't define it. This should be gp_file_name_sizeof from
* gp.h once that value is properly sent in a system-dependent way.
* HP-UX 11i 11.11 incorrectly defines FILENAME_MAX as 14.
*/
#ifdef FILENAME_MAX
# if FILENAME_MAX < 80 /* arbitrary */
# undef FILENAME_MAX
# endif
#endif
#ifndef FILENAME_MAX
# define FILENAME_MAX 1024
#endif
/* Library routines not declared in a standard header */
extern char *mktemp(char *);
/* ------ File naming and accessing ------ */
/* Define the default scratch file name prefix. */
const char gp_scratch_file_name_prefix[] = "gs_";
/* Define the name of the null output file. */
const char gp_null_file_name[] = "/dev/null";
/* Define the name that designates the current directory. */
const char gp_current_directory_name[] = ".";
/* Create and open a scratch file with a given name prefix. */
/* Write the actual file name at fname. */
static FILE *
gp_open_scratch_file_generic(const gs_memory_t *mem,
const char *prefix,
char fname[gp_file_name_sizeof],
const char *mode,
bool b64)
{ /* The -8 is for XXXXXX plus a possible final / and -. */
int prefix_length = strlen(prefix);
int len = gp_file_name_sizeof - prefix_length - 8;
FILE *fp;
if (gp_file_name_is_absolute(prefix, prefix_length))
*fname = 0;
else if (gp_gettmpdir(fname, &len) != 0)
strcpy(fname, "/tmp/");
else {
if (strlen(fname) != 0 && fname[strlen(fname) - 1] != '/')
strcat(fname, "/");
}
if (strlen(fname) + prefix_length + 8 >= gp_file_name_sizeof)
return 0; /* file name too long */
strcat(fname, prefix);
/* Prevent trailing X's in path from being converted by mktemp. */
if (*fname != 0 && fname[strlen(fname) - 1] == 'X')
strcat(fname, "-");
strcat(fname, "XXXXXX");
#ifdef HAVE_MKSTEMP
{
int file;
char ofname[gp_file_name_sizeof];
/* save the old filename template in case mkstemp fails */
memcpy(ofname, fname, gp_file_name_sizeof);
#ifdef HAVE_MKSTEMP64
if (b64)
file = mkstemp64(fname);
else
file = mkstemp(fname);
#else
file = mkstemp(fname);
#endif
if (file < -1) {
emprintf1(mem, "**** Could not open temporary file %s\n", ofname);
return NULL;
}
#if defined(O_LARGEFILE) && defined(__hpux)
if (b64)
fcntl(file, F_SETFD, fcntl(file, F_GETFD) | O_LARGEFILE);
#else
/* Fixme : what to do with b64 and 32-bit mkstemp? Unimplemented. */
#endif
fp = fdopen(file, mode);
if (fp == NULL)
close(file);
}
#else
mktemp(fname);
fp = (b64 ? gp_fopentemp : gp_fopentemp_64)(fname, mode);
#endif
if (fp == NULL)
emprintf1(mem, "**** Could not open temporary file %s\n", fname);
return fp;
}
FILE *
gp_open_scratch_file(const gs_memory_t *mem,
const char *prefix,
char fname[gp_file_name_sizeof],
const char *mode)
{
return gp_open_scratch_file_generic(mem, prefix, fname, mode, false);
}
/* Open a file with the given name, as a stream of uninterpreted bytes. */
FILE *
gp_fopen(const char *fname, const char *mode)
{
return fopen(fname, mode);
}
/* Set a file into binary or text mode. */
int
gp_setmode_binary(FILE * pfile, bool mode)
{
return 0; /* Noop under Unix */
}
/* ------ File enumeration ------ */
/* Thanks to Fritz Elfert (Fritz_Elfert@wue.maus.de) for */
/* the original version of the following code, and Richard Mlynarik */
/* (mly@adoc.xerox.com) for an improved version. */
typedef struct dirstack_s dirstack;
struct dirstack_s {
dirstack *next;
DIR *entry;
};
gs_private_st_ptrs1(st_dirstack, dirstack, "dirstack",
dirstack_enum_ptrs, dirstack_reloc_ptrs, next);
struct file_enum_s {
DIR *dirp; /* pointer to current open directory */
char *pattern; /* original pattern */
char *work; /* current path */
int worklen; /* strlen (work) */
dirstack *dstack; /* directory stack */
int patlen;
int pathead; /* how much of pattern to consider
* when listing files in current directory */
bool first_time;
gs_memory_t *memory;
};
gs_private_st_ptrs3(st_file_enum, struct file_enum_s, "file_enum",
file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern, work, dstack);
/* Private procedures */
/* Do a wild-card match. */
#ifdef DEBUG
static bool
wmatch(const byte * str, uint len, const byte * pstr, uint plen,
const string_match_params * psmp)
{
bool match = string_match(str, len, pstr, plen, psmp);
if (gs_debug_c('e')) {
int i;
dlputs("[e]string_match(\"");
for (i=0; i<len; i++)
errprintf_nomem("%c", str[i]);
dputs("\", \"");
for (i=0; i<plen; i++)
errprintf_nomem("%c", pstr[i]);
dprintf1("\") = %s\n", (match ? "TRUE" : "false"));
}
return match;
}
#define string_match wmatch
#endif
/* Search a string backward for a character. */
/* (This substitutes for strrchr, which some systems don't provide.) */
static char *
rchr(char *str, char ch, int len)
{
register char *p = str + len;
while (p > str)
if (*--p == ch)
return p;
return 0;
}
/* Pop a directory from the enumeration stack. */
static bool
popdir(file_enum * pfen)
{
dirstack *d = pfen->dstack;
if (d == 0)
return false;
pfen->dirp = d->entry;
pfen->dstack = d->next;
gs_free_object(pfen->memory, d, "gp_enumerate_files(popdir)");
return true;
}
/* Initialize an enumeration. */
file_enum *
gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem)
{
file_enum *pfen;
char *p;
char *work;
/* Reject attempts to enumerate paths longer than the */
/* system-dependent limit. */
if (patlen > FILENAME_MAX)
return 0;
/* Reject attempts to enumerate with a pattern containing zeroes. */
{
const char *p1;
for (p1 = pat; p1 < pat + patlen; p1++)
if (*p1 == 0)
return 0;
}
/* >>> Should crunch strings of repeated "/"'s in pat to a single "/"
* >>> to match stupid unix filesystem "conventions" */
pfen = gs_alloc_struct(mem, file_enum, &st_file_enum,
"gp_enumerate_files");
if (pfen == 0)
return 0;
/* pattern and work could be allocated as strings, */
/* but it's simpler for GC and freeing to allocate them as bytes. */
pfen->pattern =
(char *)gs_alloc_bytes(mem, patlen + 1,
"gp_enumerate_files(pattern)");
if (pfen->pattern == 0)
return 0;
memcpy(pfen->pattern, pat, patlen);
pfen->pattern[patlen] = 0;
work = (char *)gs_alloc_bytes(mem, FILENAME_MAX + 1,
"gp_enumerate_files(work)");
if (work == 0)
return 0;
pfen->work = work;
p = work;
memcpy(p, pat, patlen);
p += patlen;
*p = 0;
/* Remove directory specifications beyond the first wild card. */
/* Some systems don't have strpbrk, so we code it open. */
p = pfen->work;
while (!(*p == '*' || *p == '?' || *p == 0))
p++;
while (!(*p == '/' || *p == 0))
p++;
if (*p == '/')
*p = 0;
/* Substring for first wildcard match */
pfen->pathead = p - work;
/* Select the next higher directory-level. */
p = rchr(work, '/', p - work);
if (!p) { /* No directory specification */
work[0] = 0;
pfen->worklen = 0;
} else {
if (p == work) { /* Root directory -- don't turn "/" into "" */
p++;
}
*p = 0;
pfen->worklen = p - work;
}
pfen->memory = mem;
pfen->dstack = 0;
pfen->first_time = true;
pfen->patlen = patlen;
return pfen;
}
/* Enumerate the next file. */
uint
gp_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen)
{
const dir_entry *de;
char *work = pfen->work;
int worklen = pfen->worklen;
char *pattern = pfen->pattern;
int pathead = pfen->pathead;
int len;
struct stat stbuf;
if (pfen->first_time) {
pfen->dirp = ((worklen == 0) ? opendir(".") : opendir(work));
if_debug1('e', "[e]file_enum:First-Open '%s'\n", work);
pfen->first_time = false;
if (pfen->dirp == 0) { /* first opendir failed */
gp_enumerate_files_close(pfen);
return ~(uint) 0;
}
}
top:de = readdir(pfen->dirp);
if (de == 0) { /* No more entries in this directory */
char *p;
if_debug0('e', "[e]file_enum:Closedir\n");
closedir(pfen->dirp);
/* Back working directory and matching pattern up one level */
p = rchr(work, '/', worklen);
if (p != 0) {
if (p == work)
p++;
*p = 0;
worklen = p - work;
} else
worklen = 0;
p = rchr(pattern, '/', pathead);
if (p != 0)
pathead = p - pattern;
else
pathead = 0;
if (popdir(pfen)) { /* Back up the directory tree. */
if_debug1('e', "[e]file_enum:Dir popped '%s'\n", work);
goto top;
} else {
if_debug0('e', "[e]file_enum:Dirstack empty\n");
gp_enumerate_files_close(pfen);
return ~(uint) 0;
}
}
/* Skip . and .. */
len = strlen(de->d_name);
if (len <= 2 && (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")))
goto top;
if (len + worklen + 1 > FILENAME_MAX)
/* Should be an error, I suppose */
goto top;
if (worklen == 0) { /* "Current" directory (evil un*x kludge) */
memcpy(work, de->d_name, len + 1);
} else if (worklen == 1 && work[0] == '/') { /* Root directory */
memcpy(work + 1, de->d_name, len + 1);
len = len + 1;
} else {
work[worklen] = '/';
memcpy(work + worklen + 1, de->d_name, len + 1);
len = worklen + 1 + len;
}
/* Test for a match at this directory level */
if (!string_match((byte *) work, len, (byte *) pattern, pathead, NULL))
goto top;
/* Perhaps descend into subdirectories */
if (pathead < pfen->patlen) {
DIR *dp;
if (((stat(work, &stbuf) >= 0)
? !stat_is_dir(stbuf)
/* Couldn't stat it.
* Well, perhaps it's a directory and
* we'll be able to list it anyway.
* If it isn't or we can't, no harm done. */
: 0))
goto top;
if (pfen->patlen == pathead + 1) { /* Listing "foo/?/" -- return this entry */
/* if it's a directory. */
if (!stat_is_dir(stbuf)) { /* Do directoryp test the hard way */
dp = opendir(work);
if (!dp)
goto top;
closedir(dp);
}
work[len++] = '/';
goto winner;
}
/* >>> Should optimise the case in which the next level */
/* >>> of directory has no wildcards. */
dp = opendir(work);
#ifdef DEBUG
{
char save_end = pattern[pathead];
pattern[pathead] = 0;
if_debug2('e', "[e]file_enum:fname='%s', p='%s'\n",
work, pattern);
pattern[pathead] = save_end;
}
#endif /* DEBUG */
if (!dp)
/* Can't list this one */
goto top;
else { /* Advance to the next directory-delimiter */
/* in pattern */
char *p;
dirstack *d;
for (p = pattern + pathead + 1;; p++) {
if (*p == 0) { /* No more subdirectories to match */
pathead = pfen->patlen;
break;
} else if (*p == '/') {
pathead = p - pattern;
break;
}
}
/* Push a directory onto the enumeration stack. */
d = gs_alloc_struct(pfen->memory, dirstack,
&st_dirstack,
"gp_enumerate_files(pushdir)");
if (d != 0) {
d->next = pfen->dstack;
d->entry = pfen->dirp;
pfen->dstack = d;
} else
DO_NOTHING; /* >>> e_VMerror!!! */
if_debug1('e', "[e]file_enum:Dir pushed '%s'\n",
work);
worklen = len;
pfen->dirp = dp;
goto top;
}
}
winner:
/* We have a winner! */
pfen->worklen = worklen;
pfen->pathead = pathead;
memcpy(ptr, work, len);
return len;
}
/* Clean up the file enumeration. */
void
gp_enumerate_files_close(file_enum * pfen)
{
gs_memory_t *mem = pfen->memory;
if_debug0('e', "[e]file_enum:Cleanup\n");
while (popdir(pfen)) /* clear directory stack */
DO_NOTHING;
gs_free_object(mem, (byte *) pfen->work,
"gp_enumerate_close(work)");
gs_free_object(mem, (byte *) pfen->pattern,
"gp_enumerate_files_close(pattern)");
gs_free_object(mem, pfen, "gp_enumerate_files_close");
}
/* Test-cases:
(../?*r*?/?*.ps) {==} 100 string filenameforall
(../?*r*?/?*.ps*) {==} 100 string filenameforall
(../?*r*?/) {==} 100 string filenameforall
(/t*?/?*.ps) {==} 100 string filenameforall
*/
/* --------- 64 bit file access ----------- */
FILE *gp_fopen_64(const char *filename, const char *mode)
{
#if defined(HAVE_FILE64)
return fopen64(filename, mode);
#else
return fopen(filename, mode);
#endif
}
FILE *gp_open_scratch_file_64(const gs_memory_t *mem,
const char *prefix,
char fname[gp_file_name_sizeof],
const char *mode)
{
return gp_open_scratch_file_generic(mem, prefix, fname, mode, true);
}
/* gp_open_printer_64 is defined in gp_unix.h */
int64_t gp_ftell_64(FILE *strm)
{
#if defined(HAVE_FILE64)
return ftello64(strm);
#else
return ftello(strm);
#endif
}
int gp_fseek_64(FILE *strm, int64_t offset, int origin)
{
#if defined(HAVE_FILE64)
return fseeko64(strm, offset, origin);
#else
off_t offset1 = (off_t)offset;
if (offset != offset1)
return -1;
return fseeko(strm, offset1, origin);
#endif
}
|