Debian Codegraph @

texk/xdvipsk/search.c

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