dvitomp fix from Akira
[mplib] / src / texk / kpathsea / progname.c
1 /* progname.c: the executable name we were invoked as; general initialization.
2
3    Copyright 1994, 1996, 1997, 2008 Karl Berry.
4    Copyright 1998-2005 Olaf Weber.
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <kpathsea/config.h>
20 #include <kpathsea/absolute.h>
21 #include <kpathsea/c-pathch.h>
22 #include <kpathsea/c-stat.h>
23 #include <kpathsea/pathsearch.h>
24 /* For kpse_reset_progname */
25 #include <kpathsea/tex-file.h>
26
27 #if defined (WIN32) || defined (DJGPP)
28 #include <kpathsea/c-pathmx.h>
29 #endif
30
31 #if defined(__i386_pc_gnu__)
32 #ifndef _S_ISUID
33 #define _S_ISUID    04000  /* Set user ID on execution.  */
34 #endif
35 #ifndef _S_ISGID
36 #define _S_ISGID    02000  /* Set group ID on execution.  */
37 #endif
38 #ifndef _S_ISVTX
39 #define _S_ISVTX    01000  /* Save swapped text after use (sticky).  */
40 #endif
41 #ifndef _S_IREAD
42 #define _S_IREAD    0400   /* Read by owner.  */
43 #endif
44 #ifndef _S_IWRITE
45 #define _S_IWRITE   0200   /* Write by owner.  */
46 #endif
47 #ifndef _S_IEXEC
48 #define _S_IEXEC    0100   /* Execute by owner.  */
49 #endif
50 #endif
51
52 /* NeXT does not define the standard macros, but has the equivalent.
53    WIN32 doesn't define them either, and doesn't have them.
54    From: Gregor Hoffleit <flight@mathi.uni-heidelberg.de>.  */
55 #ifndef S_IXUSR
56 #ifdef WIN32
57 #define S_IXUSR 0
58 #define S_IXGRP 0
59 #define S_IXOTH 0
60 #else /* not WIN32 */
61 #define S_IXUSR 0100
62 #endif /* not WIN32 */
63 #endif /* not S_IXUSR */
64 #ifndef S_IXGRP
65 #define S_IXGRP 0010
66 #endif
67 #ifndef S_IXOTH
68 #define S_IXOTH 0001
69 #endif
70
71 #ifndef HAVE_PROGRAM_INVOCATION_NAME
72 /* Don't redefine the variables if glibc already has.  However, we do
73    not check for HAVE_PROGRAM_INVOCATION_NAME anywhere else in this
74    file; rather, we always use our own code to compute them, overwriting
75    anything that glibc may have provided.  This avoids
76    difficult-to-debug system-dependent behavior, and also universally
77    supports the second (`progname') argument for dotted texmf.cnf values.
78
79    It would have been better to simply use our own variable names (and
80    computations) in the first place, but it's not worth losing backward
81    compatibility to rename them now.  */
82
83 string program_invocation_name = NULL;
84 string program_invocation_short_name = NULL;
85 #endif
86
87 /* And the variable for the program we pretend to be. */
88 string kpse_program_name = NULL;
89 \f
90 #ifndef WIN32
91 /* From a standalone program `ll' to expand symlinks written by Kimbo Mundy.
92    Don't bother to compile if we don't have symlinks; thus we can assume
93    / as the separator.  Also don't try to use basename, etc., or
94    handle arbitrary filename length.  Mixed case function names because
95    that's what kimbo liked.  */
96
97 #ifdef S_ISLNK
98 static int ll_verbose = 0;
99 static int ll_loop = 0;
100
101 #undef BSIZE
102 #define BSIZE 2048 /* sorry */
103
104
105 /* Read link FN into SYM.  */
106
107 static void
108 ReadSymLink P2C(char *, fn, char *, sym)
109 {
110   register int n = readlink (fn, sym, BSIZE);
111   if (n < 0) {
112     perror (fn);
113     exit (1);
114   }
115   sym[n] = 0;
116 }
117
118
119 /* Strip first component from S, and also return it in a static buffer.  */
120
121 static char *
122 StripFirst P1C(register char *, s)
123 {
124   static char buf[BSIZE];
125   register char *s1;
126
127   /* Find the end of the first path element */
128   for (s1 = s; *s1 && (*s1 != '/' || s1 == s); s1++)
129     ;
130
131   /* Copy it into buf and null-terminate it. */
132   strncpy (buf, s, s1 - s);
133   buf[s1 - s] = 0;
134
135   /* Skip over the leading / (if any) */
136   if (*s1 == '/')
137     ++s1;
138
139   /* Squeeze out the element */
140   while ((*s++ = *s1++) != 0)
141     ;
142
143   return buf;
144 }
145
146
147 /* Strip last component from S, and also return it in a static buffer.  */
148
149 static char *
150 StripLast P1C(register char *, s)
151 {
152   static char buf[BSIZE];
153   register char *s1;
154
155   for (s1 = s + strlen (s); s1 > s && *s1 != '/'; s1--)
156     ;
157   strcpy (buf, s1 + (*s1 == '/'));
158   *s1 = 0;
159
160   return buf;
161 }
162
163
164 /* Copy first path element from B to A, removing it from B.  */
165
166 static void 
167 CopyFirst P2C(register char *, a, char *, b)
168 {
169   register int length = strlen (a);
170
171    if (length > 0 && a[length - 1] != '/') {
172    a[length] = '/';
173     a[length + 1] = 0;
174   }
175   strcat (a, StripFirst (b));
176 }
177
178 /* Returns NULL on error.  Prints intermediate results if global
179    `ll_verbose' is nonzero.  */
180
181 #define EX(s)           (strlen (s) && strcmp (s, "/") ? "/" : "")
182 #define EXPOS           EX(post)
183 #define EXPRE           EX(pre)
184
185 static char *
186 expand_symlinks P1C(char *, s)
187 {
188   static char pre[BSIZE];       /* return value */
189   char post[BSIZE], sym[BSIZE], tmp[BSIZE], before[BSIZE];
190   char *cp;
191   char a;
192   struct stat st;
193   int done;
194
195   /* Check for symlink loops.  It's difficult to check for all the
196      possibilities ourselves, so let the kernel do it.  And make it
197      conditional so that people can see where the infinite loop is
198      being caused (see engtools#1536).  */
199   if (!ll_loop) {
200     FILE *f = fopen (s, "r");
201     if (!f && errno == ELOOP) {
202       /* Not worried about other errors, we'll get to them in due course.  */
203       perror (s);
204       return NULL;
205     }
206     if (f) fclose (f);
207   }
208
209   strcpy (post, s);
210   strcpy (pre, "");
211
212   while (strlen (post) != 0) {
213     CopyFirst (pre, post);
214
215     if (lstat (pre, &st) != 0) {
216       fprintf (stderr, "lstat(%s) failed ...\n", pre);
217       perror (pre);
218       return NULL;
219     }
220
221     if (S_ISLNK (st.st_mode)) {
222       ReadSymLink (pre, sym);
223
224       if (!strncmp (sym, "/", 1)) {
225         if (ll_verbose)
226           printf ("[%s]%s%s -> [%s]%s%s\n", pre, EXPOS, post, sym, EXPOS,post);
227         strcpy (pre, "");
228
229       } else {
230         a = pre[0];     /* handle links through the root */
231         strcpy (tmp, StripLast (pre));
232         if (!strlen (pre) && a == '/')
233           strcpy (pre, "/");
234
235         if (ll_verbose) {
236           sprintf (before, "%s%s[%s]%s%s", pre, EXPRE, tmp, EXPOS, post);
237           printf ("%s -> %s%s[%s]%s%s\n", before, pre, EXPRE, sym, EXPOS,post);
238         }
239
240         /* Strip "../" path elements from the front of sym; print
241            new result if there were any such elements.  */
242         done = 0;
243         a = pre[0];     /* handle links through the root */
244         while (!strncmp (sym, "..", 2)
245                && (sym[2] == 0 || sym[2] == '/')
246                && strlen (pre) != 0
247                && strcmp (pre, ".")
248                && strcmp (pre, "..")
249                && (strlen (pre) < 3
250                    || strcmp (pre + strlen (pre) - 3, "/.."))) {
251           done = 1;
252           StripFirst (sym);
253           StripLast (pre);
254         }
255
256         if (done && ll_verbose) {
257           for (cp = before; *cp;)
258             *cp++ = ' ';
259           if (strlen (sym))
260             printf ("%s == %s%s%s%s%s\n", before, pre, EXPRE, sym, EXPOS,post);
261           else
262             printf ("%s == %s%s%s\n", before, pre, EXPOS, post);
263         }
264         if (!strlen (pre) && a == '/')
265           strcpy (pre, "/");
266       }
267
268       if (strlen (post) != 0 && strlen (sym) != 0)
269         strcat (sym, "/");
270
271       strcat (sym, post);
272       strcpy (post, sym);
273     }
274   }
275
276   return pre;
277 }
278 #else /* not S_ISLNK */
279 #define expand_symlinks(s) (s)
280 #endif /* not S_ISLNK */
281 \f
282 /* Remove .'s and ..'s in DIR, to avoid problems with relative symlinks
283    as the program name, etc.  This does not canonicalize symlinks.  */
284
285 static string
286 remove_dots P1C(string, dir)
287 {
288 #ifdef AMIGA
289   return dir;
290 #else
291   string c;
292   unsigned len;
293   string ret = (string) ""; /* We always reassign.  */
294   
295   for (c = kpse_filename_component (dir); c;
296        c = kpse_filename_component (NULL)) {
297     if (STREQ (c, ".")) {
298       /* If leading ., replace with cwd.  Else ignore.  */
299       if (*ret == 0) {
300         ret = xgetcwd ();
301       }
302
303     } else if (STREQ (c, "..")) {
304       /* If leading .., start with xdirname (cwd).  Else remove last
305          component from ret, if any.  */
306       if (*ret == 0) {
307         string dot = xgetcwd ();
308         ret = xdirname (dot);
309         free (dot);
310       } else {
311         unsigned last;
312         for (last = strlen (ret);
313              last > (NAME_BEGINS_WITH_DEVICE (ret) ? 2 : 0);
314              last--) {
315           if (IS_DIR_SEP (ret[last - 1])) {
316             /* If we have `/../', that's the same as `/'.  */
317             if (last > 1) {
318               ret[last - 1] = 0;
319             }
320             break;
321           }
322         }
323       }
324
325     } else {
326       /* Not . or ..; just append.  Include a directory separator unless
327          our string already ends with one.  This also changes all directory
328          separators into the canonical DIR_SEP_STRING.  */
329       string temp;
330       len = strlen (ret);
331       temp = concat3 (ret, ((len > 0 && ret[len - 1] == DIR_SEP)
332                             || (NAME_BEGINS_WITH_DEVICE (c) && *ret == 0))
333                            ? "" : DIR_SEP_STRING,
334                       c);
335       if (*ret)
336         free (ret);
337       ret = temp;
338     }
339   }
340   
341   /* Remove a trailing /, just in case it snuck in.  */
342   len = strlen (ret);
343   if (len > 0 && ret[len - 1] == DIR_SEP) {
344     ret[len - 1] = 0;
345   }
346
347   return ret;
348 #endif /* not AMIGA */
349 }
350 \f
351 /* Return directory ARGV0 comes from.  Check PATH if ARGV0 is not
352    absolute.  */
353
354 static string
355 selfdir P1C(const_string, argv0)
356 {
357   string ret = NULL;
358   string self = NULL;
359   
360   if (kpse_absolute_p (argv0, true)) {
361     self = xstrdup (argv0);
362   } else {
363 #ifdef AMIGA
364 #include <dos.h>
365 #include <proto/dos.h>
366 #include <proto/exec.h>
367     BPTR lock;
368     struct DosLibrary *DOSBase
369       = (struct DosLibrary *) OpenLibrary ("dos.library", 0L);
370     assert (DOSBase);
371
372     self = xmalloc (BUFSIZ);
373     lock = findpath (argv0);
374     if (lock != ((BPTR) -1)) {
375       if (getpath (lock, self) == -1) {
376         *self = '\0';
377       } else {
378         strcat (self,DIR_SEP_STRING);
379         strcat (self,argv0); 
380       }
381       UnLock (lock);
382     }
383     CloseLibrary((struct Library *) DOSBase);
384 #else /* not AMIGA */
385     const_string elt;
386     struct stat s;
387
388     /* Have to check PATH.  But don't call kpse_path_search since we don't
389        want to search any ls-R's or do anything special with //'s.  */
390     for (elt = kpse_path_element (getenv ("PATH")); !self && elt;
391          elt = kpse_path_element (NULL)) {
392       string name;
393       
394       /* UNIX tradition interprets the empty path element as "." */
395       if (*elt == 0) elt = ".";
396       
397       name = concat3 (elt, DIR_SEP_STRING, argv0);
398
399       /* In order to do this perfectly, we'd have to check the owner bits only
400          if we are the file owner, and the group bits only if we belong
401          to the file group.  That's a lot of work, though, and it's not
402          likely that kpathsea will ever be used with a program that's
403          only executable by some classes and not others.  See the
404          `file_status' function in execute_cmd.c in bash for what's
405          necessary if we were to do it right.  */
406       if (stat (name, &s) == 0 && s.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) {
407         /* Do not stop at directories. */
408         if (!S_ISDIR(s.st_mode)) 
409           self = name;
410       }
411     }
412 #endif /* not AMIGA */
413   }
414   
415   /* If argv0 is somehow dir/exename, `self' will still be NULL.  */
416   if (!self)
417     self = concat3 (".", DIR_SEP_STRING, argv0);
418     
419   ret = xdirname (remove_dots (expand_symlinks (self)));
420
421   free (self);
422   
423   return ret;
424 }
425 #endif /* not WIN32 */
426 \f
427 void
428 kpse_set_program_name P2C(const_string, argv0, const_string, progname)
429 {
430   string ext, sdir, sdir_parent, sdir_grandparent;
431   string s = getenv ("KPATHSEA_DEBUG");
432 #ifdef WIN32
433   string debug_output = getenv("KPATHSEA_DEBUG_OUTPUT");
434   string append_debug_output = getenv("KPATHSEA_DEBUG_APPEND");
435   int err, olderr;
436 #endif
437   
438   /* Set debugging stuff first, in case we end up doing debuggable stuff
439      during this initialization.  */
440   if (s) {
441     kpathsea_debug |= atoi (s);
442   }
443
444 #ifndef HAVE_PROGRAM_INVOCATION_NAME
445 #if defined(WIN32)
446   /* Set various info about user. Among many things,
447      ensure that HOME is set. If debug_paths is on, 
448      turn on some message if $HOME is not found. */
449   if (KPSE_DEBUG_P(KPSE_DEBUG_PATHS)) {
450 #ifndef __MINGW32__
451     set_home_warning();
452 #else
453     extern int output_home_warning;
454     output_home_warning = 1;
455 #endif
456   }
457   init_user_info();
458
459   /* redirect stderr to debug_output. Easier to send logfiles. */
460   if (debug_output) {
461     int flags =  _O_CREAT | _O_TRUNC | _O_RDWR;
462     err = -1;
463     if (_stricmp(debug_output, "con") == 0
464        || _stricmp(debug_output, "con:") == 0) {
465       err = _fileno(stdout);
466     } else {
467       if (append_debug_output) {
468         flags =  _O_CREAT | _O_APPEND | _O_WRONLY;
469       } else {
470         flags =  _O_CREAT | _O_TRUNC | _O_WRONLY;
471         xputenv("KPATHSEA_DEBUG_APPEND", "yes");
472       }
473     }
474
475     if ((err < 0)
476         && (err = _open(debug_output, flags, _S_IREAD | _S_IWRITE)) == -1)
477     {
478       WARNING1("Can't open %s for stderr redirection!\n", debug_output);
479       perror(debug_output);
480     } else if ((olderr = _dup(fileno(stderr))) == -1) {
481       WARNING("Can't dup() stderr!\n");
482       close(err);
483     } else if (_dup2(err, fileno(stderr)) == -1) {
484       WARNING1("Can't redirect stderr to %s!\n", debug_output);
485       close(olderr);
486       close(err);
487     } else {
488       close(err);
489     }
490   }
491   /* Win95 always gives the short filename for argv0, not the long one.
492      There is only this way to catch it. It makes all the selfdir stuff
493      useless for win32. */
494   {
495     char short_path[PATH_MAX], path[PATH_MAX], *fp;
496       
497     /* SearchPath() always gives back an absolute directory */
498     if (SearchPath(NULL, argv0, ".exe", PATH_MAX, short_path, &fp) == 0)
499         FATAL1("Can't determine where the executable %s is.\n", argv0);
500     if (!win32_get_long_filename(short_path, path, sizeof(path))) {
501         FATAL1("This path points to an invalid file : %s\n", short_path);
502     }
503     /* slashify the dirname */
504     for (fp = path; fp && *fp; fp++)
505         if (IS_DIR_SEP(*fp)) *fp = DIR_SEP;
506     /* sdir will be the directory of the executable, ie: c:/TeX/bin */
507     sdir = xdirname(path);
508     program_invocation_name = xstrdup(xbasename(path));
509   }
510
511 #elif defined(__DJGPP__)
512
513   /* DJGPP programs support long filenames on Windows 95, but ARGV0 there
514      is always made with the short 8+3 aliases of all the pathname elements.
515      If long names are supported, we need to convert that to a long name.
516
517      All we really need is to call `_truename', but most of the code
518      below is required to deal with the special case of networked drives.  */
519   if (pathconf (argv0, _PC_NAME_MAX) > 12) {
520     char long_progname[PATH_MAX];
521
522     if (_truename (argv0, long_progname)) {
523       char *fp;
524
525       if (long_progname[1] != ':') {
526         /* A complication: `_truename' returns network-specific string at
527            the beginning of `long_progname' when the program resides on a
528            networked drive, and DOS calls cannot grok such pathnames.  We
529            need to convert the filesystem name back to a drive letter.  */
530         char rootname[PATH_MAX], rootdir[4];
531
532         if (argv0[0] && argv0[1] == ':')
533           rootdir[0] = argv0[0]; /* explicit drive in `argv0' */
534         else
535           rootdir[0] = getdisk () + 'A';
536         rootdir[1] = ':';
537         rootdir[2] = '\\';
538         rootdir[3] = '\0';
539         if (_truename (rootdir, rootname)) {
540           /* Find out where `rootname' ends in `long_progname' and replace
541              it with the drive letter.  */
542           int root_len = strlen (rootname);
543
544           if (IS_DIR_SEP (rootname[root_len - 1]))
545             root_len--; /* keep the trailing slash */
546           long_progname[0] = rootdir[0];
547           long_progname[1] = ':';
548           memmove (long_progname + 2, long_progname + root_len,
549                    strlen (long_progname + root_len) + 1);
550         }
551       }
552
553       /* Convert everything to canonical form.  */
554       if (long_progname[0] >= 'A' && long_progname[0] <= 'Z')
555         long_progname[0] += 'a' - 'A'; /* make drive lower case, for beauty */
556       for (fp = long_progname; *fp; fp++)
557         if (IS_DIR_SEP (*fp))
558           *fp = DIR_SEP;
559
560       program_invocation_name = xstrdup (long_progname);
561     }
562     else
563       /* If `_truename' failed, God help them, because we won't...  */
564       program_invocation_name = xstrdup (argv0);
565   }
566   else
567     program_invocation_name = xstrdup (argv0);
568
569 #else /* !WIN32 && !__DJGPP__ */
570   program_invocation_name = xstrdup (argv0);
571 #endif
572 #endif /* not HAVE_PROGRAM_INVOCATION_NAME */
573
574   /* We need to find SELFAUTOLOC *before* removing the ".exe" suffix from
575      the program_name, otherwise the PATH search inside selfdir will fail,
576      since `prog' doesn't exists as a file, there's `prog.exe' instead.  */
577 #ifndef WIN32
578   sdir = selfdir (program_invocation_name);
579 #endif
580   /* SELFAUTODIR is actually the parent of the invocation directory,
581      and SELFAUTOPARENT the grandparent.  This is how teTeX did it.  */
582   xputenv ("SELFAUTOLOC", sdir);
583   sdir_parent = xdirname (sdir);
584   xputenv ("SELFAUTODIR", sdir_parent);
585   sdir_grandparent = xdirname (sdir_parent);
586   xputenv ("SELFAUTOPARENT", sdir_grandparent);
587
588   free (sdir);
589   free (sdir_parent);
590   free (sdir_grandparent);
591
592 #ifndef HAVE_PROGRAM_INVOCATION_NAME
593   program_invocation_short_name = (string)xbasename (program_invocation_name);
594 #endif
595
596   if (progname) {
597     kpse_program_name = xstrdup (progname);
598   } else {
599     /* If configured --enable-shared and running from the build directory
600        with the wrapper scripts (e.g., for make check), the binaries will
601        be named foo.exe instead of foo.  Or possibly if we're running on a
602        DOSISH system.  */
603     ext = find_suffix (program_invocation_short_name);
604     if (ext && FILESTRCASEEQ (ext, "exe")) {
605       kpse_program_name = remove_suffix (program_invocation_short_name);
606     } else {
607       kpse_program_name = xstrdup (program_invocation_short_name);
608     }
609   }
610
611   xputenv ("progname", kpse_program_name);
612 }
613
614 /* This function is deprecated, because when we pretend to have a different
615    name it will look for _that_ name in the PATH if program_invocation_name
616    is not defined.  */
617 void
618 kpse_set_progname P1C(const_string, argv0)
619 {
620   kpse_set_program_name (argv0, NULL);
621 }
622 \f
623 #ifdef TEST
624 void
625 main (int argc, char **argv)
626 {
627   puts (remove_dots ("/w/kpathsea"));
628   puts (remove_dots ("/w//kpathsea"));
629   puts (remove_dots ("/w/./kpathsea"));
630   puts (remove_dots ("."));
631   puts (remove_dots ("./"));
632   puts (remove_dots ("./."));
633   puts (remove_dots ("../kpathsea"));
634   puts (remove_dots ("/kpathsea/../foo"));
635   puts (remove_dots ("/../w/kpathsea"));
636   puts (remove_dots ("/../w/kpathsea/."));
637   puts (remove_dots ("/te/share/texmf/../../../../bin/gnu"));
638 }
639 /*
640 Local variables:
641 standalone-compile-command: "gcc -g -I. -I.. -DTEST progname.c STATIC/libkpathsea.a"
642 End:
643 */
644 #endif /* TEST */