OSDN Git Service

some notes of kpathsea.
[putex/putex.git] / src / texsourc / openinou.c
1 /* Copyright 1992 Karl Berry
2    Copyright 2007 TeX Users Group
3    Copyright 2014 Clerk Ma
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301 USA.  */
19
20 #ifdef _WINDOWS
21   #define NOCOMM
22   #define NOSOUND
23   #define NODRIVERS
24   #define STRICT
25   #pragma warning(disable:4115) // kill rpcasync.h complaint
26   #include <windows.h>
27   #define MYLIBAPI __declspec(dllexport)
28 #endif
29
30 #include "texwin.h"
31
32 #pragma warning(disable:4996)
33 #pragma warning(disable:4131) // old style declarator
34 #pragma warning(disable:4135) // conversion between different integral types 
35 #pragma warning(disable:4127) // conditional expression is constant
36
37 #include <setjmp.h>
38
39 #include <direct.h>           /* for _getcwd() */
40
41 #define EXTERN extern
42
43 #include "texd.h"
44
45 #define PATH_SEP        '/'
46 #define PATH_SEP_STRING "/"
47
48 /* openinout.c: open input and output files.  These routines used by
49    TeX, Metafont, and BibTeX.  */
50
51 /* #include <sys\stat.h> */       /* debugging 94/Mar/2 */
52 /* #include <sys\types.h> */      /* debugging 94/Mar/2 */
53 /* #include <conio.h> */        /* for getch */
54
55 // #include "config.h"
56 // #include "c-namemx.h"
57 // #include "c-pathch.h"
58
59 #define BUILDNAMEDIRECT         /* avoid malloc for string concat */
60
61 bool test_read_access (unsigned char *, int);   /* in ourpaths.c - bkph */
62 /* bool test_read_access (char *, int, int); */ /* in ourpaths.c - bkph */
63
64 extern char *unixify (char *);      /* in pathsrch.c bkph */
65
66 extern void try_and_open (char *);    /* in local.c bkph */
67
68 extern int shorten_file_name;       /* in local.c bkph */
69
70 #ifdef FUNNY_CORE_DUMP
71 /* This is defined in ./texmf.c.  */
72 extern void funny_core_dump();
73 #endif /* FUNNY_CORE_DUMP */
74
75 #ifdef MSDOS
76
77 #ifdef BUILDNAMEDIRECT
78 /* kpathsea/concat.c */
79 /* kpathsea/concat3.c */
80 /* similar to concat, but AVOIDS using malloc, pass in place to put result */
81 char *xconcat (char *buffer, char *s1, char *s2)
82 {
83   int n1 = strlen(s1);
84   int n2 = strlen(s2);
85   if (buffer == s2)
86   {     /* treat special case of overlap */
87     memmove (buffer + n1, buffer, n2 + 1); /* trailing null ! */
88     strncpy (buffer, s1, n1);
89   }
90   else
91   {
92     strcpy(buffer, s1);
93     strcat(buffer + n1, s2);
94   }
95   return buffer;
96 }
97 /* similar to concat3, but avoids using malloc, pass in place to put result */
98 char *xconcat3 (char *buffer, char *s1, char *s2, char *s3)
99 {
100   int n1 = strlen(s1);
101   int n2 = strlen(s2);
102   int n3 = strlen(s3);
103   if (buffer == s3) {     /* treat special case of overlap */
104     memmove (buffer + n1 + n2, buffer, n3 + 1); /* trailing null ! */
105     strncpy (buffer, s1, n1);
106     strncpy (buffer + n1, s2, n2);
107   }
108   else {
109     strcpy(buffer, s1);
110     strcat(buffer + n1, s2);
111     strcat(buffer + n1 + n2, s3);
112   }
113   return buffer;
114 }
115 #endif /* end of BUILDNAMEDIRECT  */
116
117 #endif  /* end of ifdef MSDOS ??? */
118
119 #ifdef MSDOS
120 /* separated out 1996/Jan/20 to make easier to read */
121 /* assumes path does not end in PATH_SEP */
122 void patch_in_path (unsigned char *buffer, unsigned char *name, unsigned char *path)
123 {
124 #ifdef BUILDNAMEDIRECT
125   if (*path == '\0')
126     strcpy((char *) buffer, (char *) name);
127   else
128     xconcat3((char *) buffer, (char *) path, PATH_SEP_STRING, (char *) name);
129 #else
130   string temp_name;
131   temp_name = concat3 (path, PATH_SEP_STRING, name);
132   strcpy (buffer, temp_name);
133   free (temp_name);
134 #endif
135 }
136 int qualified (unsigned char * name)
137 {
138   if (strchr((char *) name, PATH_SEP) != NULL ||
139       strchr((char *) name, '\\') != NULL ||
140       strchr((char *) name, ':') != NULL)
141     return 1;
142   else
143     return 0;
144 }
145 /* patch path if (i) path not empty (ii) name not qualified (iii) ext match */
146 int prepend_path_if (unsigned char *buffer, unsigned char *name, char *ext, unsigned char *path)
147 {
148   if (path == NULL)
149     return 0;
150   if (*path == '\0')
151     return 0;
152   if (qualified(name))
153     return 0;
154   if (strstr((char *)name, ext) == NULL)
155     return 0;
156   patch_in_path(buffer, name, path);
157   return 1;
158 }
159 #endif      /* end of MSDOS */
160
161 /*  Following works on null-terminated strings */
162
163 /* void check_short_name(void) { */       /* 1995/Feb/20 */
164 void check_short_name (unsigned char *s)
165 {         /* 1995/Sep/26 */
166   unsigned char *star, *sdot;
167   int n;
168
169 /*  if ((star = strrchr(name_of_file+1, '\\')) != NULL) star++;
170   else if ((star = strrchr(name_of_file+1, '/')) != NULL) star++;
171   else if ((star = strchr(name_of_file+1, ':')) != NULL) star++;
172   else star = name_of_file+1; */        /* 1995/Sep/26 */
173   if ((star = (unsigned char *) strrchr((char *) s, '\\')) != NULL)
174     star++;
175   else if ((star = (unsigned char *) strrchr((char *) s, '/')) != NULL)
176     star++;
177   else if ((star = (unsigned char *) strchr((char *) s, ':')) != NULL)
178     star++;
179   else
180     star = s;
181   if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
182     n = sdot - star;
183   else
184     n = strlen((char *) star);
185   if (n > 8)
186     strcpy((char *) star + 8, (char *) star + n);
187   if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
188   {
189     star = sdot + 1;
190     n = strlen((char *) star);
191     if (n > 3)
192       *(star + 3) = '\0';
193   }
194 }
195
196 /* Following works on both null-terminated names */
197 /* reconvert 254 to '~' in file name 95/Sep/26 */
198 /* reconvert 255 to ' ' in file name 95/Sep/26 */
199 /* we do this in tex3.c start_input() -> scan_file_name() now 95/Sep/26 */
200
201 void retwiddle (unsigned char *s)
202 { /* assumes null terminated - 97/June/5 */
203 /*  while (*s != '\0' && *s != ' ') { */
204   while (*s != '\0')
205   {
206     if (*s == (unsigned char) pseudo_tilde)
207       *s = '~';
208     else if (*s == (unsigned char) pseudo_space)
209       *s = ' ';
210     s++;
211   }
212 }
213
214 /* #endif */ /* ??? */
215
216 /* Open an input file F, using the path PATHSPEC and passing
217    FOPEN_MODE to fopen.  The filename is in `name_of_file', as a Pascal
218    string. We return whether or not the open succeeded.  If it did, we
219    also set `name_length' to the length of the full pathname that we
220    opened.  */
221
222 bool open_input (FILE **f, path_constant_type path_index, char *fopen_mode)
223 {
224   bool openable = false;
225
226 #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
227 /*  This only applies if a preloaded TeX (or Metafont) is being made;
228   it allows for automatic creation of the core dump (typing ^\
229   requires manual intervention).  */
230   if (path_index == TEXINPUTPATH &&
231       strncmp (name_of_file + 1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
232     funny_core_dump();
233 #endif /* FUNNY_CORE_DUMP and not BibTeX */
234
235 #ifdef MSDOS
236   if (return_flag)
237   {
238     if (strcmp(fopen_mode, "r") == 0)
239       fopen_mode = "rb";    /* so can catch `return' bkph */
240   }
241 #endif /* MSDOS */
242
243   name_of_file[name_length + 1] = '\0'; /* null terminate */
244
245 /* reinsert '~' and ' ' in file names -  95/June/5 */
246 /* done late to prevent problems with  null_terminate / space_terminate */  
247   if (pseudo_tilde != 0 || pseudo_space != 0)
248     retwiddle(name_of_file + 1);
249
250 #ifdef MSDOS
251 /* 8 + 3 file names on Windows NT 95/Feb/20 */
252   if (shorten_file_name)
253   {
254     check_short_name(name_of_file + 1);           /* 95/Sep/26 */
255 /*    space_terminate (name_of_file + 1); */
256   }
257 #endif  /* MSDOS */
258
259 #ifdef BibTeX
260   if (path_index == NO_FILE_PATH)
261   {
262     unsigned temp_length;
263
264 /*      null_terminate (name_of_file + 1); */
265 /*  if share_flag is non-zero and we are opening for reading use fsopen */
266 /*  but we can assume here that we are opening for *input* */
267 /*      *f = fopen (name_of_file + 1, fopen_mode); */
268     if (share_flag == 0)
269       *f = fopen (name_of_file + 1, fopen_mode);
270     else
271       *f = _fsopen (name_of_file + 1, fopen_mode, share_flag);
272     temp_length = strlen (name_of_file + 1);
273 /*      space_terminate (name_of_file + 1); */
274
275     if (*f != NULL)
276     {
277       name_length = temp_length;
278       openable = true;
279     }
280   }
281
282   else
283 #endif /* BibTeX */
284   
285   if (open_trace_flag)
286   {
287     sprintf(log_line, " Open `%s' for input ", name_of_file+1); /* Pascal */
288     show_line(log_line, 0);
289   }
290
291   if (test_read_access(name_of_file + 1, path_index))
292   {
293 /*  if (test_read_access(name_of_file, name_length, path_index)) */
294
295 /*    We can assume `name_of_file' is openable, */
296 /*    since `test_read_access' just returned true.  */
297 /*    *f = xfopen_pas (name_of_file, fopen_mode); */
298     *f = xfopen((char *) name_of_file + 1, fopen_mode);
299
300 //    should we check *f == NULL ??? (should be OK because of test_read_access)
301
302 /*    If we found the file in the current directory, don't leave the
303         `./' at the beginning of `name_of_file', since it looks dumb when
304         TeX says `(./foo.tex ...)', and analogously for Metafont.  */
305 #ifdef MSDOS
306     if (name_of_file[1] == '.' &&         /* 1994/Mar/1 */
307       (name_of_file[2] == PATH_SEP || name_of_file[2] == '\\'))
308 #else
309     if (name_of_file[1] == '.' && name_of_file[2] == PATH_SEP) 
310 #endif
311     {
312       unsigned i = 1;
313 /*        while (name_of_file[i + 2] != ' ') */
314       while (name_of_file[i + 2] != '\0')
315       {
316         name_of_file[i] = name_of_file[i + 2];
317         i++;
318       }
319 /*      name_of_file[i] = ' '; */
320       name_of_file[i] = '\0';
321       name_length = i - 1;
322     }
323     else
324 /*      name_length = strchr(name_of_file + 1, ' ') - (name_of_file + 1); */
325       name_length = strlen((char *) name_of_file + 1);
326       
327 #ifdef TeX
328 /*    If we just opened a TFM file, we have to read the first byte,
329         since TeX wants to look at it.  What a kludge.  */
330     if (path_index == TFMFILEPATH)
331     { /* See comments in ctex.ch for why we need this.  */
332 /*          extern integer tfm_temp; */ /* see texd.h for definition */
333       tfm_temp = getc (*f);
334     }
335 #endif /* TeX */  
336
337 #ifdef MSDOS
338 /*    code added 94/June/21 to show 'fmt' file opening in log */
339     if (strstr((char *) name_of_file + 1, ".fmt") != NULL)
340     {
341       if (format_file == NULL)
342       {
343         format_file = xstrdup((char *) name_of_file + 1);
344       }
345     } /* remember full format file name with path */
346     else if (strstr((char *)name_of_file+1, ".poo") != NULL)
347     {
348       if (string_file == NULL)
349       {
350         string_file = xstrdup((char *) name_of_file + 1);
351       }
352     } /* remember full pool file name with path */
353     else if (strstr((char *)name_of_file+1, ".tfm") != NULL)
354     {
355       if (show_tfm_flag && log_opened)
356       {
357 #ifdef WRAPLINES
358         int old_setting = selector;
359         char *s = name_of_file + 1;
360         selector = log_only;
361         print_char(' ');
362         print_char('(');
363         while (*s != '\0')
364           print_char (*s++);
365         print_char(')');
366         selector = old_setting;
367 #else
368         int n; 
369         n = strlen((char *) name_of_file + 1);
370         if (file_offset + n > max_print_line)
371         {
372           putc('\n', log_file);
373           file_offset = 0;
374         } /* somewhat risky ? */
375         else
376           putc(' ', log_file);
377         fprintf(log_file, "(%s)", name_of_file + 1);
378         file_offset += n+3;
379 /*        space_terminate (name_of_file + 1);  */
380 #endif  /*  end of WRAPLINES */
381       }
382     }
383 /*    code added 98/Sep/29 to catch first file input */
384 /*    is there a problem if this file bombs ? */
385     else if (source_direct == NULL) /* 98/Sep/29 */
386     {
387       char *s;
388       source_direct = xstrdup((char *) name_of_file + 1);
389       if (trace_flag)
390       {
391         sprintf(log_line, "Methinks the source %s is `%s'\n", "file", source_direct);
392         show_line(log_line, 0);
393       }
394
395       if ((s = strrchr(source_direct, '/')) == NULL)
396         *source_direct='\0';
397       else
398         *(s+1) = '\0';
399
400       if (trace_flag)
401       {
402         sprintf(log_line, "Methinks the source %s is `%s'\n", "directory", source_direct);
403         show_line(log_line, 0);
404       }
405     }
406 #endif  /* end of MSDOS */
407     openable = true;
408   }
409 /*  space_terminate (name_of_file + 1); */
410   {
411     unsigned temp_length = strlen((char *) name_of_file + 1);
412     name_of_file[temp_length + 1] = ' ';  /* space terminate */
413 /*    set up name_length ??? */
414   }
415   return openable;
416 }
417
418 /* Call the external program PROGRAM, passing it `name_of_file'.  */
419 /* This nonsense probably only works for Unix anyway. bkph */
420 /* For one thing, MakeTeXTFM etc is more than 8 characters ! */
421
422 #ifdef MSDOS
423   #define NO_MAKETEX
424 #endif
425
426 /* the string program is unreferenced in DOS NO_MAKETEX */
427
428 static bool
429 make_tex_file (string program)
430 {
431 #ifdef NO_MAKETEX
432   return 0;
433 #else
434   char cmd[NAME_MAX + 1 + PATH_MAX + 1];
435   unsigned cmd_len;
436   int ret;
437   unsigned i = 1; /* For copying from `name_of_file'.  */
438
439   /* Wrap another sh around the invocation of the MakeTeX program, so we
440      can avoid `sh: MakeTeXTFM: not found' errors confusing the user.
441      We don't use fork/exec ourselves, since we'd have to call sh anyway
442      to interpret the script.  */
443 #ifdef MSDOS
444   strcpy (cmd, "command.com ");
445 #else
446   strcpy (cmd, "sh -c ");
447 #endif
448   
449 /*  strcat (cmd, program); */ /* shrouded 93/Nov/20 */
450   strcat (cmd, "Make");
451 #ifndef MSDOS
452   strcat (cmd, "TeX");
453 #endif
454   strcat (cmd, program);
455   cmd_len = strlen (cmd);
456   cmd[cmd_len++] = ' ';
457
458   while (name_of_file[i] != ' ')
459     cmd[cmd_len++] = name_of_file[i++];
460
461   /* Add terminating null.  */
462   cmd[cmd_len] = 0;
463
464   /* Don't show any output.  */
465 #ifdef MSDOS
466   strcat (cmd, "> nul");  /* ? 93/Nov/20 */
467 #else
468   strcat (cmd, ">/dev/null 2>&1");
469 #endif
470
471 /* Run the command, and return whether or not it succeeded.  */
472   ret = system (cmd);
473   return ret == EXIT_SUCCESS_CODE;
474 #endif /* not NO_MAKE_TEX */
475 }
476
477 #define TEXONLY
478
479 /* This is called by TeX if an \input resp. TFM file can't be opened.  */
480
481 bool maketextex (void)          /* called in tex3.c and tex8.c */
482 {
483 /*  return make_tex_file ("MakeTeXTeX"); */
484   return make_tex_file ("TeX"); 
485 }
486
487 bool maketextfm (void)          /* called in tex3.c */
488 {
489 /*  return make_tex_file ("MakeTeXTFM"); */
490   return make_tex_file ("TFM");
491 }
492
493 #ifndef TEXONLY
494 bool maketexmf (void)
495 {
496 /*  return make_tex_file ("MakeTeXMF"); */
497   return make_tex_file ("MF");
498 }
499 #endif /* ifndef TEXONLY */
500
501 char *get_env_shroud (char *);    /* defined in texmf.c */
502
503 /* char outputdirectory[PATH_MAX]; */       /* defined in local.c */
504
505 extern char *dvi_directory;       /* defined in local.c */
506 extern char *log_directory;       /* defined in local.c */
507 extern char *aux_directory;       /* defined in local.c */
508
509 #ifdef IGNORED
510 /* Try and figure out if can write to current directory */
511 bool isitsafe (char *name)
512 {
513 /*  struct stat statbuf; */         /* debugging 94/Mar/2 */
514 /*  Can't test access on file, since fails if not exist */
515 /*  Can't test access on `nul', since always fails */ 
516 /*  Can   test access on `.', but its true on locked diskette! */
517 /*  stat on directory always says read an write permission */
518   return true;        /* for now */
519 }
520 #endif
521
522 /* open_output moved to end to avoid pragma problems 96/Sep/15 */
523
524 /* used only in start_input in tex3.c, and in open_or_close_in in tex8.c */
525 /* modified 97/June/5 to take null terminated (C) string */
526
527 #ifdef IGNORED
528 bool extensionirrelevantaux (char *base, char *suffix)
529
530   bool ret;
531 /*  make_c_string (&base);  */
532 /*  base[nlen+1] = '\0'; */     /* null terminate */
533 #ifdef MSDOS
534 /*  In DOS, an extension is irrelevant if there already is an extension ! */
535 /*  MAY NEED TO REVISE IN WIN32 where we can have foo.bar.chomp.tex ??? */
536   {               /* simplification 1996/Jan/20 ??? */
537     char *s, *t;
538     if ((s = strrchr (base, '.')) == NULL)
539       ret = 0; /* no extension */
540     else
541     {
542       if ((t = strrchr (base, PATH_SEP)) != NULL ||
543           (t = strrchr (base, '\\')) != NULL ||
544           (t = strrchr (base, ':')) != NULL)
545       {
546         if (t > s)
547           ret = 0; /* last dot occurs in path - no extension */
548         else
549           ret = 1;     /* last dot occurs in file name itself */
550       }
551       else
552         ret = 1;       /* name not qualified and has dot */
553     }
554   }
555 #else /*  not MSDOS */
556   {
557     char temp[PATH_MAX];
558     strcpy (temp, base);
559     strcat (temp, ".");
560     strcat (temp, suffix);
561     ret = same_file_p (base, temp);
562   }
563 #endif /* end of not MSDOS */
564 /*  make_pascal_string (&base); */
565 /*  base[nlen+1] = ' '; */      /* space terminate */
566   return ret;
567 }
568 #endif /* IGNORED */
569
570 /* Test if the Pascal string BASE concatenated with the extension
571    `.SUFFIX' is the same file as just BASE.  SUFFIX is a C string.  */
572
573 /* used in `start_input' (tex3.c) and open_or_close_in (tex8.c) */
574 /* used to always return true, since in DOS can have only one extension */
575 /* modified 98/Feb/7 to always return false */
576
577 bool extensionirrelevantp (unsigned char *base, int nlen, char *suffix)
578
579 #ifdef IGNORED
580   bool ret;
581   base[nlen+1] = '\0';      /* null terminate */
582   ret = extensionirrelevantaux(base+1, suffix);
583   base[nlen+1] = ' ';     /* space terminate */
584   return ret;
585 #endif
586   return false;
587 }
588
589 /* #define a_close(f) if (f) { if (ferror (f)) {perror(""); exit(1);} } if (f) (void) fclose (f) */
590 /* #define a_close(f) if (f) (void) check_fclose (f) */
591
592 /* At least check for I/O error (such as disk full) when closing */
593 /* Would be better to check while writing - but this is better than nothing */
594 /* This is used for both input and output files, but never mind ... */
595
596 /* now a_close returns -1 on error --- which could be used by caller */
597 /* probably want to ignore on input files ... */
598
599 void perrormod (char *s);       /* in local.c */
600
601 // check_fclose not used by anything
602 /* 1993/Nov/20 - bkph */
603 int check_fclose (FILE *f)
604 {
605   if (f == NULL)
606     return 0;      // sanity check
607   if (ferror(f) || fclose (f))
608   {
609     perrormod("\n! I/O Error");
610     uexit (1);    // ???
611   }
612   return 0;
613 }
614
615 /* open_output moved down here to avoid potential pragma problem */
616
617 /* #pragma optimize ("g", off) *//* try and avoid compiler bug here */
618
619 /* Open an output file F either in the current directory or in
620    $TEXMFOUTPUT/F, if the environment variable `TEXMFOUTPUT' exists.
621    (Actually, this applies to the BibTeX output files, also, but
622    `TEXMFBIBOUTPUT' was just too long.)  The filename is in the global
623    `name_of_file', as a Pascal string.  We return whether or not the open
624    succeeded.  If it did, the global `name_length' is set to the length
625    of the actual filename.  */
626
627 bool open_output (FILE **f, char *fopen_mode)
628 {
629   unsigned temp_length;
630
631 /*  null_terminate (name_of_file + 1);  */  /* moved here 95/Sep/26  */
632   name_of_file[name_length + 1] = '\0'; /* null terminate */
633
634 /* reinsert '~' and ' ' in file names -  95/June/5 */
635 /* done late to prevent problems with  null_terminate / space_terminate */  
636   if (pseudo_tilde != 0 || pseudo_space !=  0)
637   {
638     retwiddle(name_of_file + 1);
639   }
640
641 #ifdef MSDOS
642 /* 8 + 3 file names on Windows NT 95/Feb/20 */
643   if (shorten_file_name)
644   {
645     check_short_name(name_of_file + 1);         /* 95/Sep/26 */
646   }
647 #endif
648
649 /*  null_terminate (name_of_file + 1); */ /* Make the filename into a C string.  */
650
651 /*  if (debug_flag) try_and_open(name_of_file+1); */  /* debugging 94/Mar/20 */
652
653 #ifdef MSDOS
654 /* write into user specified output directory if given on command line */
655 /* following code added 1993/Dec/12 */ /* separated 1996/Jan/20 */
656   if (prepend_path_if (name_of_file+1, name_of_file+1, ".dvi", (unsigned char *) dvi_directory) ||
657       prepend_path_if (name_of_file+1, name_of_file+1, ".log", (unsigned char *) log_directory) ||
658       prepend_path_if (name_of_file+1, name_of_file+1, ".aux", (unsigned char *) aux_directory))
659   {
660     if (open_trace_flag)
661     {
662       sprintf(log_line, "After prepend %s\n", name_of_file+1);
663       show_line(log_line, 0);
664     }
665   }
666 #endif
667
668 /* name_length recomputed below so don't need to do it yet */
669
670   if (open_trace_flag)
671   {
672     sprintf(log_line, " Open `%s' for output ", name_of_file + 1); /* C string */
673     show_line(log_line, 0);
674   }   // debugging only
675
676 /* Is the filename openable as given?  */
677
678 /*  if share_flag is non-zero and we are opening for reading use fsopen */
679 /*  but we can assume this is opening here for *output* */
680   *f = fopen((char *) name_of_file + 1, fopen_mode);
681
682 /* Can't open as given.  Try the envvar.  */
683   if (*f == NULL)
684   {
685 /*    string temp_dir = getenv ("TEXMFOUTPUT"); */  /* 93/Nov/20 */
686 /*    string temp_dir = getenv ("TEXMFOUT"); */ /* 93/Nov/20 */
687 /*      string temp_dir = get_env_shroud ("UFYNGPVUQVU"); */
688     string temp_dir = get_env_shroud ("UFYNGPVU");
689
690 /*    if (deslash) unixify(temp_dir); */    /* deslashify 93/Dec/28 */
691
692     if (temp_dir != NULL)
693     {
694 #ifdef BUILDNAMEDIRECT
695       unsigned char temp_name[PATH_MAX];
696       xconcat3((char *) temp_name, temp_dir, PATH_SEP_STRING, (char *)name_of_file + 1);
697 #else
698 /*          string temp_name = concat3 (temp_dir, "/", name_of_file + 1); */
699       string temp_name = concat3 (temp_dir, PATH_SEP_STRING, name_of_file + 1);
700 #endif
701       if (deslash) unixify((char *) temp_name);     /* deslashify 93/Dec/28 */
702 /*  If share_flag is non-zero and we are opening for reading use fsopen */
703 /*  but we can assume this is opening here for *output* */
704       *f = fopen((char*)temp_name, fopen_mode);
705 /*  If this succeeded, change name_of_file accordingly.  */
706       if (*f) strcpy((char*) name_of_file + 1, (char *) temp_name);
707 #ifndef BUILDNAMEDIRECT
708       free (temp_name);
709 #endif
710     }
711   }
712
713 //  show_line(name_of_file+1, 1);   // debugging only
714 //  New code to remember complete dvi_file name and log_file_name
715 //  To remember for output at the end 2000 June 18
716   if (strstr((char *)name_of_file + 1, ".dvi") != NULL)
717   {
718     if (qualified(name_of_file+1))
719       *log_line = '\0';
720     else
721     {
722       (void) _getcwd(log_line, sizeof(log_line));
723       strcat(log_line, PATH_SEP_STRING);
724     }
725     strcat(log_line, (char*) name_of_file+1);
726     unixify(log_line);
727     dvi_file_name = xstrdup(log_line);
728 //    show_line(dvi_file_name, 1);  // debugging only
729   }
730   else if (strstr((char *)name_of_file + 1, ".log") != NULL)
731   {
732     if (qualified(name_of_file+1))
733       *log_line = '\0';
734     else
735     {
736       (void) _getcwd(log_line, sizeof(log_line));
737       strcat(log_line, PATH_SEP_STRING);
738     }
739     strcat(log_line, (char *) name_of_file + 1);
740     unixify(log_line);
741     log_file_name = xstrdup(log_line);
742 //    show_line(log_file_name, 1);  // debugging only
743   }
744 /* Back into a Pascal string, but first get its length.  */
745   temp_length = strlen ((char *)name_of_file + 1);
746 /*  space_terminate (name_of_file + 1); */
747   name_of_file[temp_length+1] = ' ';  /* space terminate */
748
749 /* Only set `name_length' if we succeeded.  I'm not sure why.  */
750   if (*f)               /* TEST ? 94/MAR/2 */
751     name_length = temp_length;
752   
753   return *f != NULL;
754 }
755
756 /* #pragma optimize ("g",)*/  /* try and avoid compiler bug here */
757 /* #pragma optimize ("", on) */   /* try and avoid compiler bug here */