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
|
/* Copyright (C) 2001-2012 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 the license contained in the file LICENSE in this distribution.
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.
*/
/* MS-DOS file enumeration. */
#include "stdio_.h"
#include <fcntl.h>
#include "dos_.h"
#include "memory_.h"
#include "string_.h"
#include "gstypes.h"
#include "gsmemory.h"
#include "gsstruct.h"
#include "gp.h"
#include "gsutil.h"
struct file_enum_s {
ff_struct_t ffblk;
char *pattern; /* orig pattern + modified pattern */
int patlen; /* orig pattern length */
int pat_size; /* allocate space for pattern */
int head_size; /* pattern length through last */
/* :, / or \ */
int first_time;
gs_memory_t *memory;
};
gs_private_st_ptrs1(st_file_enum, struct file_enum_s, "file_enum",
file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern);
/* Initialize an enumeration. Note that * and ? in a directory */
/* don't work, and \ is taken literally unless a second \ follows. */
file_enum *
gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem)
{
file_enum *pfen = gs_alloc_struct(mem, file_enum, &st_file_enum, "gp_enumerate_files");
int pat_size = 2 * patlen + 1;
char *pattern;
char *p;
int hsize = 0;
int i;
int dot = 0;
if (pfen == 0)
return 0;
/* pattern could be allocated as a string, */
/* but it's simpler for GC and freeing to allocate it as bytes. */
pattern = (char *)gs_alloc_bytes(mem, pat_size,
"gp_enumerate_files(pattern)");
if (pattern == 0)
return 0;
memcpy(pattern, pat, patlen);
p = pattern + patlen;
for (i = 0; i < patlen; i++) {
switch (pat[i]) {
case '*':
/* Skip to . or end of string so DOS can do it. */
*p++ = '*';
while (i < patlen && pat[i] != '.')
i++;
if (i == patlen && !dot) { /* DOS doesn't interpret * alone as */
/* matching all files; we need *.*. */
*p++ = '.';
*p++ = '*';
}
i--;
continue;
case '.':
dot = 1;
break;
case '\\':
if (i + 1 < patlen && pat[i + 1] == '\\')
i++;
/* falls through */
case ':':
case '/':
hsize = p + 1 - (pattern + patlen);
dot = 0;
}
*p++ = pat[i];
}
*p = 0;
pfen->pattern = pattern;
pfen->patlen = patlen;
pfen->pat_size = pat_size;
pfen->head_size = hsize;
pfen->memory = mem;
pfen->first_time = 1;
return pfen;
}
/* Enumerate the next file. */
static const string_match_params smp_file =
{'*', '?', -1, true, true};
uint
gp_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen)
{
int code;
char *p, *q;
uint len;
const char *fpat = pfen->pattern + pfen->patlen;
top:if (pfen->first_time) {
code = dos_findfirst(fpat, &pfen->ffblk);
pfen->first_time = 0;
} else
code = dos_findnext(&pfen->ffblk);
if (code != 0) { /* All done, clean up. */
gp_enumerate_files_close(pfen);
return ~(uint) 0;
}
if (maxlen < 13 + pfen->head_size)
return maxlen + 1; /* cop out! */
memcpy(ptr, fpat, pfen->head_size);
for (p = &pfen->ffblk.ff_name[0], q = ptr + pfen->head_size; *p; p++)
if (*p != ' ')
*q++ = *p;
len = q - ptr;
/* Make sure this file really matches the pattern. */
if (!string_match(ptr, len, pfen->pattern, pfen->patlen, &smp_file))
goto top;
return len;
}
/* Clean up the file enumeration. */
void
gp_enumerate_files_close(file_enum * pfen)
{
gs_memory_t *mem = pfen->memory;
gs_free_object(mem, pfen->pattern,
"gp_enumerate_files_close(pattern)");
gs_free_object(mem, pfen, "gp_enumerate_files_close");
}
|