1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gp_dosfe.c 8250 2007-09-25 13:31:24Z giles $ */
15
/* MS-DOS file enumeration. */
29
char *pattern; /* orig pattern + modified pattern */
30
int patlen; /* orig pattern length */
31
int pat_size; /* allocate space for pattern */
32
int head_size; /* pattern length through last */
37
gs_private_st_ptrs1(st_file_enum, struct file_enum_s, "file_enum",
38
file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern);
40
/* Initialize an enumeration. Note that * and ? in a directory */
41
/* don't work, and \ is taken literally unless a second \ follows. */
43
gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem)
45
file_enum *pfen = gs_alloc_struct(mem, file_enum, &st_file_enum, "gp_enumerate_files");
46
int pat_size = 2 * patlen + 1;
56
/* pattern could be allocated as a string, */
57
/* but it's simpler for GC and freeing to allocate it as bytes. */
59
pattern = (char *)gs_alloc_bytes(mem, pat_size,
60
"gp_enumerate_files(pattern)");
63
memcpy(pattern, pat, patlen);
65
for (i = 0; i < patlen; i++) {
68
/* Skip to . or end of string so DOS can do it. */
70
while (i < patlen && pat[i] != '.')
72
if (i == patlen && !dot) { /* DOS doesn't interpret * alone as */
73
/* matching all files; we need *.*. */
83
if (i + 1 < patlen && pat[i + 1] == '\\')
88
hsize = p + 1 - (pattern + patlen);
94
pfen->pattern = pattern;
95
pfen->patlen = patlen;
96
pfen->pat_size = pat_size;
97
pfen->head_size = hsize;
103
/* Enumerate the next file. */
104
static const string_match_params smp_file =
105
{'*', '?', -1, true, true};
108
gp_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen)
113
const char *fpat = pfen->pattern + pfen->patlen;
115
top:if (pfen->first_time) {
116
code = dos_findfirst(fpat, &pfen->ffblk);
117
pfen->first_time = 0;
119
code = dos_findnext(&pfen->ffblk);
120
if (code != 0) { /* All done, clean up. */
121
gp_enumerate_files_close(pfen);
124
if (maxlen < 13 + pfen->head_size)
125
return maxlen + 1; /* cop out! */
126
memcpy(ptr, fpat, pfen->head_size);
127
for (p = &pfen->ffblk.ff_name[0], q = ptr + pfen->head_size; *p; p++)
131
/* Make sure this file really matches the pattern. */
132
if (!string_match(ptr, len, pfen->pattern, pfen->patlen, &smp_file))
137
/* Clean up the file enumeration. */
139
gp_enumerate_files_close(file_enum * pfen)
141
gs_memory_t *mem = pfen->memory;
143
gs_free_object(mem, pfen->pattern,
144
"gp_enumerate_files_close(pattern)");
145
gs_free_object(mem, pfen, "gp_enumerate_files_close");