| 1 |
/* |
| 2 |
* The search routine takes a directory list, separated by PATHSEP, and |
| 3 |
* tries to open a file. Null directory components indicate current |
| 4 |
* directory. In an environment variable, null directory components |
| 5 |
* indicate substitution of the default path list at that point. |
| 6 |
* This source file assumes KPATHSEA is defined. |
| 7 |
*/ |
| 8 |
#include "dvips.h" /* The copyright notice in that file is included too! */ |
| 9 |
|
| 10 |
#ifndef KPATHSEA |
| 11 |
"goodbye, search.c can only be compiled with -DKPATHSEA" |
| 12 |
#endif |
| 13 |
|
| 14 |
#include "protos_add.h" /* external declarations */ |
| 15 |
|
| 16 |
#include <kpathsea/c-ctype.h> |
| 17 |
#include <kpathsea/tex-file.h> |
| 18 |
#include <kpathsea/tex-glyph.h> |
| 19 |
#include <kpathsea/absolute.h> |
| 20 |
#include <kpathsea/c-pathch.h> |
| 21 |
#include <kpathsea/variable.h> |
| 22 |
|
| 23 |
#ifdef WIN32 |
| 24 |
#undef fopen |
| 25 |
#undef popen |
| 26 |
#undef pclose |
| 27 |
extern FILE *generic_fsyscp_fopen(const char *name, const char *mode); |
| 28 |
#define fopen(file, fmode) generic_fsyscp_fopen(file, fmode) |
| 29 |
#define popen(pcmd, pmode) fsyscp_popen(pcmd, pmode) |
| 30 |
#define pclose(pstream) _pclose(pstream) |
| 31 |
|
| 32 |
#undef FATAL_PERROR |
| 33 |
#if defined (KPSE_COMPAT_API) |
| 34 |
#define FATAL_PERROR(str) do { \ |
| 35 |
win32_fprintf (stderr, "%s: ", kpse_def->invocation_name); \ |
| 36 |
win32_perror (str); exit (EXIT_FAILURE); } while (0) |
| 37 |
#else |
| 38 |
/* If there is no global variable available, just output the error */ |
| 39 |
#define FATAL_PERROR(str) do { \ |
| 40 |
win32_perror (str); exit (EXIT_FAILURE); } while (0) |
| 41 |
#endif |
| 42 |
|
| 43 |
#endif |
| 44 |
|
| 45 |
#ifndef GUNZIP |
| 46 |
#define GUNZIP "gzip -d" |
| 47 |
#endif |
| 48 |
|
| 49 |
int to_close; /* use fclose or pclose? */ |
| 50 |
char *realnameoffile; |
| 51 |
|
| 52 |
/* Return safely quoted version of NAME. That means using "..." on |
| 53 |
Windows and '...' on Unix, with any occurrences of the quote char |
| 54 |
quoted with a \. Result is always in new memory; NAME is unchanged. |
| 55 |
This is a generic function; if we end up needing it in other |
| 56 |
programs, we should move it to kpathsea. */ |
| 57 |
|
| 58 |
#ifdef WIN32 |
| 59 |
#define QUOTE '"' |
| 60 |
#else |
| 61 |
#define QUOTE '\'' |
| 62 |
#endif |
| 63 |
|
| 64 |
static char * |
| 65 |
quote_name (const char *name) |
| 66 |
{ |
| 67 |
char *quoted = xmalloc (strlen (name) * 2 + 3); |
| 68 |
char *q = quoted; |
| 69 |
const char *p = name; |
| 70 |
|
| 71 |
*q++ = QUOTE; |
| 72 |
while (*p) { |
| 73 |
if (*p == QUOTE) |
| 74 |
*q++ = QUOTE; |
| 75 |
*q++ = *p++; |
| 76 |
} |
| 77 |
*q++ = QUOTE; |
| 78 |
*q = 0; |
| 79 |
|
| 80 |
return quoted; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/* On Windows, return PROG with SELFAUTOLOC prepended, and / separators |
| 85 |
changed to \. On Unix, return a copy of PROG. Result is always in |
| 86 |
new memory. No safeness or other quoting is done on PROG. |
| 87 |
|
| 88 |
This is necessary because Windows likes to run programs out of the |
| 89 |
current directory. So for security, we want to ensure that we are |
| 90 |
invoking programs from our own binary directory, not via PATH. |
| 91 |
|
| 92 |
This is a generic function; if we end up needing it in other |
| 93 |
programs, we should move it to kpathsea. */ |
| 94 |
|
| 95 |
#ifdef WIN32 |
| 96 |
#include <kpathsea/concatn.h> |
| 97 |
#endif |
| 98 |
|
| 99 |
static char * |
| 100 |
selfautoloc_prog (const char *prog) |
| 101 |
{ |
| 102 |
char *ret; |
| 103 |
#ifdef WIN32 |
| 104 |
char *p, *q, *r; |
| 105 |
/* Get value of SELFAUTOLOC, but \ works better for path separator. */ |
| 106 |
char *selfautoloc = kpse_var_value ("SELFAUTOLOC"); |
| 107 |
for (p = selfautoloc; *p; p++) { |
| 108 |
if (*p == '/') |
| 109 |
*p = '\\'; |
| 110 |
} |
| 111 |
q = xstrdup (prog); |
| 112 |
r = q; |
| 113 |
while (*r && *r != ' ' && *r != '\t') |
| 114 |
r++; |
| 115 |
if (*r == '\0') |
| 116 |
ret = concatn ("\"", selfautoloc, "\\", q, "\"", NULL); |
| 117 |
else { |
| 118 |
*r = '\0'; |
| 119 |
r++; |
| 120 |
if (*r) |
| 121 |
ret = concatn ("\"", selfautoloc, "\\", q, "\" ", r, NULL); |
| 122 |
else |
| 123 |
ret = concatn ("\"", selfautoloc, "\\", q, "\"", NULL); |
| 124 |
} |
| 125 |
free (q); |
| 126 |
free (selfautoloc); |
| 127 |
|
| 128 |
#else /* not WIN32 */ |
| 129 |
ret = xstrdup (prog); |
| 130 |
#endif |
| 131 |
|
| 132 |
return ret; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
FILE * |
| 138 |
search(kpse_file_format_type format, const char *file, const char *mode) |
| 139 |
{ |
| 140 |
FILE *ret; |
| 141 |
string found_name; |
| 142 |
|
| 143 |
/* This change suggested by [email protected] to disallow reading of |
| 144 |
arbitrary files. Further modified by Y. Oiwa. */ |
| 145 |
#ifndef SECURE |
| 146 |
if (secure == 2) { |
| 147 |
#endif |
| 148 |
/* an absolute path is denied */ |
| 149 |
if (kpse_absolute_p (file, false)) |
| 150 |
return NULL; |
| 151 |
|
| 152 |
if (file[0] == '.' && file[1] == '.' && IS_DIR_SEP(file[2])) { |
| 153 |
/* a relative path starting with ../ is denied */ |
| 154 |
return NULL; |
| 155 |
} else { |
| 156 |
/* a relative path containing /../ is denied */ |
| 157 |
const char *p; |
| 158 |
|
| 159 |
if (file[0] != '\0') { |
| 160 |
for(p = file + 1; *p && ((p = strstr(p, "..")) != NULL); p += 2) { |
| 161 |
/* p[2] is valid, because p points to string starting with ".." */ |
| 162 |
/* p[-1] is valid, because p does not point the top of file */ |
| 163 |
if ((p[2] == '\0' || IS_DIR_SEP(p[2])) && IS_DIR_SEP(p[-1])) { |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
#ifndef SECURE |
| 170 |
} |
| 171 |
#endif |
| 172 |
/* Most file looked for through here must exist -- the exception is |
| 173 |
VF's. Bitmap fonts go through pksearch. */ |
| 174 |
found_name = kpse_find_file (file, format, |
| 175 |
format != vfpath && format != ofmpath); |
| 176 |
if (found_name) { |
| 177 |
unsigned len = strlen (found_name); |
| 178 |
if ((format == figpath || format == headerpath) |
| 179 |
&& ((len > 2 && FILESTRCASEEQ (found_name + len - 2, ".Z")) |
| 180 |
|| (len > 3 && FILESTRCASEEQ (found_name + len - 3, ".gz")))) { |
| 181 |
/* automatically but safely decompress. */ |
| 182 |
char *quoted_name = quote_name (found_name); |
| 183 |
char *cmd; |
| 184 |
char *prog = selfautoloc_prog (GUNZIP); |
| 185 |
cmd = concat3 (prog, " -c ", quoted_name); |
| 186 |
ret = popen (cmd, "r"); |
| 187 |
if (dd(D_FILES)) { |
| 188 |
fprintf_str(stderr, "popen(%s) %s\n", cmd, |
| 189 |
ret == NULL ? "failed" : "succeeded"); |
| 190 |
} |
| 191 |
SET_BINARY(fileno(ret)); |
| 192 |
to_close = USE_PCLOSE; |
| 193 |
free (cmd); |
| 194 |
free (quoted_name); |
| 195 |
free (prog); |
| 196 |
} else { |
| 197 |
ret = fopen (found_name, mode); |
| 198 |
to_close = USE_FCLOSE; |
| 199 |
} |
| 200 |
if (!ret) |
| 201 |
FATAL_PERROR (found_name); |
| 202 |
|
| 203 |
/* Free result of previous search. */ |
| 204 |
if (realnameoffile) |
| 205 |
free (realnameoffile); |
| 206 |
|
| 207 |
/* Save in `name' and `realnameoffile' because other routines |
| 208 |
access those globals. */ |
| 209 |
realnameoffile = found_name; |
| 210 |
strcpy(name, realnameoffile); |
| 211 |
} else |
| 212 |
ret = NULL; |
| 213 |
|
| 214 |
return ret; |
| 215 |
} /* end search */ |
| 216 |
|
| 217 |
|
| 218 |
FILE * |
| 219 |
pksearch(const char *file, const char *mode, halfword dpi, |
| 220 |
const char **name_ret, int *dpi_ret) |
| 221 |
{ |
| 222 |
FILE *ret; |
| 223 |
kpse_glyph_file_type font_file; |
| 224 |
string found_name = kpse_find_pk (file, dpi, &font_file); |
| 225 |
|
| 226 |
if (found_name) { |
| 227 |
ret = fopen (found_name, mode); |
| 228 |
if (!ret) |
| 229 |
FATAL_PERROR (name); |
| 230 |
|
| 231 |
/* Free result of previous search. */ |
| 232 |
if (realnameoffile) |
| 233 |
free (realnameoffile); |
| 234 |
/* Save in `name' and `realnameoffile' because other routines |
| 235 |
access those globals. Sigh. */ |
| 236 |
realnameoffile = found_name; |
| 237 |
strcpy(name, realnameoffile); |
| 238 |
*name_ret = font_file.name; |
| 239 |
*dpi_ret = font_file.dpi; |
| 240 |
} else |
| 241 |
ret = NULL; |
| 242 |
|
| 243 |
return ret; |
| 244 |
} /* end search */ |
| 245 |
|
| 246 |
|
| 247 |
/* do we report file openings? */ |
| 248 |
|
| 249 |
#ifdef DEBUG |
| 250 |
# ifdef fopen |
| 251 |
# undef fopen |
| 252 |
# endif |
| 253 |
# ifdef VMCMS /* IBM: VM/CMS */ |
| 254 |
# define fopen cmsfopen |
| 255 |
# endif /* IBM: VM/CMS */ |
| 256 |
FILE * |
| 257 |
my_real_fopen(register const char *n, register const char *t) |
| 258 |
{ |
| 259 |
FILE *tf; |
| 260 |
if (dd(D_FILES)) { |
| 261 |
fprintf_str(stderr, "<%s(%s)> ", n, t); |
| 262 |
tf = fopen(n, t); |
| 263 |
if (tf == 0) |
| 264 |
fprintf(stderr, "failed\n"); |
| 265 |
else |
| 266 |
fprintf(stderr, "succeeded\n"); |
| 267 |
} else |
| 268 |
tf = fopen(n, t); |
| 269 |
#ifdef OS2 |
| 270 |
if (tf == (FILE *)NULL) |
| 271 |
tf = fat_fopen(n, t); /* try again with filename truncated to 8.3 */ |
| 272 |
#endif |
| 273 |
return tf; |
| 274 |
} |
| 275 |
#endif |
| 276 |
|
| 277 |
#ifdef OS2 |
| 278 |
/* truncate filename at end of fname to FAT filesystem 8.3 limit */ |
| 279 |
/* if truncated, return fopen() with new name */ |
| 280 |
FILE * |
| 281 |
fat_fopen(char *fname, char *t) |
| 282 |
{ |
| 283 |
char *np; /* pointer to name within path */ |
| 284 |
char nbuf[13], *ns, *nd; |
| 285 |
char n[MAXPATHLEN]; |
| 286 |
int ni, ne; |
| 287 |
FILE *tf; |
| 288 |
strcpy(n, fname); |
| 289 |
for (ns=n; *ns; ns++) { |
| 290 |
if (*ns=='/') |
| 291 |
*ns=DIRSEP; |
| 292 |
} |
| 293 |
np = strrchr(n,DIRSEP); |
| 294 |
if (np==(char *)NULL) |
| 295 |
np = n; |
| 296 |
else |
| 297 |
np++; |
| 298 |
/* fail if it contains more than one '.' */ |
| 299 |
ni = 0; |
| 300 |
for (ns=np; *ns; ns++) { |
| 301 |
if (*ns=='.') |
| 302 |
ni++; |
| 303 |
} |
| 304 |
if (ni>1) |
| 305 |
return (FILE *)NULL; |
| 306 |
/* copy it to nbuf, truncating to 8.3 */ |
| 307 |
ns = np; |
| 308 |
nd = nbuf; |
| 309 |
ni = 0; |
| 310 |
while ((*ns!='.') && (*ns) && (ni<8)) { |
| 311 |
*nd++ = *ns++; |
| 312 |
ni++; |
| 313 |
} |
| 314 |
while ((*ns!='.') && (*ns)) { |
| 315 |
ns++; |
| 316 |
ni++; |
| 317 |
} |
| 318 |
ne = 0; |
| 319 |
if (*ns=='.') { |
| 320 |
*nd++ = *ns++; |
| 321 |
while ((*ns!='.') && (*ns) && (ne<3)) { |
| 322 |
*nd++ = *ns++; |
| 323 |
ne++; |
| 324 |
} |
| 325 |
while (*ns) { |
| 326 |
ns++; |
| 327 |
ne++; |
| 328 |
} |
| 329 |
} |
| 330 |
*nd++='\0'; |
| 331 |
if ((ni>8) || (ne>3)) { |
| 332 |
strcpy(np,nbuf); |
| 333 |
/* now code copied from my_real_fopen() */ |
| 334 |
if (dd(D_FILES)) { |
| 335 |
fprintf_str(stderr, "<%s(%s)> ", n, t); |
| 336 |
tf = fopen(n, t); |
| 337 |
if (tf == 0) |
| 338 |
fprintf(stderr, "failed\n"); |
| 339 |
else |
| 340 |
fprintf(stderr, "succeeded\n"); |
| 341 |
} |
| 342 |
else |
| 343 |
tf = fopen(n, t); |
| 344 |
return tf; |
| 345 |
} |
| 346 |
return (FILE *)NULL; |
| 347 |
} |
| 348 |
#endif |
| 349 |
|
| 350 |
int |
| 351 |
close_file(FILE *f) |
| 352 |
{ |
| 353 |
switch(to_close) { |
| 354 |
case USE_PCLOSE: return pclose(f); |
| 355 |
case USE_FCLOSE: return fclose(f); |
| 356 |
default: return -1; |
| 357 |
} |
| 358 |
} |