1 /* progname.c: the executable name we were invoked as; general initialization.
3 Copyright 1994, 1996, 1997, 2008 Karl Berry.
4 Copyright 1998-2005 Olaf Weber.
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.
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.
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/>. */
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>
27 #if defined (WIN32) || defined (DJGPP)
28 #include <kpathsea/c-pathmx.h>
31 #if defined(__i386_pc_gnu__)
33 #define _S_ISUID 04000 /* Set user ID on execution. */
36 #define _S_ISGID 02000 /* Set group ID on execution. */
39 #define _S_ISVTX 01000 /* Save swapped text after use (sticky). */
42 #define _S_IREAD 0400 /* Read by owner. */
45 #define _S_IWRITE 0200 /* Write by owner. */
48 #define _S_IEXEC 0100 /* Execute by owner. */
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>. */
62 #endif /* not WIN32 */
63 #endif /* not S_IXUSR */
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.
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. */
83 string program_invocation_name = NULL;
84 string program_invocation_short_name = NULL;
87 /* And the variable for the program we pretend to be. */
88 string kpse_program_name = NULL;
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. */
98 static int ll_verbose = 0;
99 static int ll_loop = 0;
102 #define BSIZE 2048 /* sorry */
105 /* Read link FN into SYM. */
108 ReadSymLink P2C(char *, fn, char *, sym)
110 register int n = readlink (fn, sym, BSIZE);
119 /* Strip first component from S, and also return it in a static buffer. */
122 StripFirst P1C(register char *, s)
124 static char buf[BSIZE];
127 /* Find the end of the first path element */
128 for (s1 = s; *s1 && (*s1 != '/' || s1 == s); s1++)
131 /* Copy it into buf and null-terminate it. */
132 strncpy (buf, s, s1 - s);
135 /* Skip over the leading / (if any) */
139 /* Squeeze out the element */
140 while ((*s++ = *s1++) != 0)
147 /* Strip last component from S, and also return it in a static buffer. */
150 StripLast P1C(register char *, s)
152 static char buf[BSIZE];
155 for (s1 = s + strlen (s); s1 > s && *s1 != '/'; s1--)
157 strcpy (buf, s1 + (*s1 == '/'));
164 /* Copy first path element from B to A, removing it from B. */
167 CopyFirst P2C(register char *, a, char *, b)
169 register int length = strlen (a);
171 if (length > 0 && a[length - 1] != '/') {
175 strcat (a, StripFirst (b));
178 /* Returns NULL on error. Prints intermediate results if global
179 `ll_verbose' is nonzero. */
181 #define EX(s) (strlen (s) && strcmp (s, "/") ? "/" : "")
182 #define EXPOS EX(post)
183 #define EXPRE EX(pre)
186 expand_symlinks P1C(char *, s)
188 static char pre[BSIZE]; /* return value */
189 char post[BSIZE], sym[BSIZE], tmp[BSIZE], before[BSIZE];
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). */
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. */
212 while (strlen (post) != 0) {
213 CopyFirst (pre, post);
215 if (lstat (pre, &st) != 0) {
216 fprintf (stderr, "lstat(%s) failed ...\n", pre);
221 if (S_ISLNK (st.st_mode)) {
222 ReadSymLink (pre, sym);
224 if (!strncmp (sym, "/", 1)) {
226 printf ("[%s]%s%s -> [%s]%s%s\n", pre, EXPOS, post, sym, EXPOS,post);
230 a = pre[0]; /* handle links through the root */
231 strcpy (tmp, StripLast (pre));
232 if (!strlen (pre) && a == '/')
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);
240 /* Strip "../" path elements from the front of sym; print
241 new result if there were any such elements. */
243 a = pre[0]; /* handle links through the root */
244 while (!strncmp (sym, "..", 2)
245 && (sym[2] == 0 || sym[2] == '/')
248 && strcmp (pre, "..")
250 || strcmp (pre + strlen (pre) - 3, "/.."))) {
256 if (done && ll_verbose) {
257 for (cp = before; *cp;)
260 printf ("%s == %s%s%s%s%s\n", before, pre, EXPRE, sym, EXPOS,post);
262 printf ("%s == %s%s%s\n", before, pre, EXPOS, post);
264 if (!strlen (pre) && a == '/')
268 if (strlen (post) != 0 && strlen (sym) != 0)
278 #else /* not S_ISLNK */
279 #define expand_symlinks(s) (s)
280 #endif /* not S_ISLNK */
282 /* Remove .'s and ..'s in DIR, to avoid problems with relative symlinks
283 as the program name, etc. This does not canonicalize symlinks. */
286 remove_dots P1C(string, dir)
293 string ret = (string) ""; /* We always reassign. */
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. */
303 } else if (STREQ (c, "..")) {
304 /* If leading .., start with xdirname (cwd). Else remove last
305 component from ret, if any. */
307 string dot = xgetcwd ();
308 ret = xdirname (dot);
312 for (last = strlen (ret);
313 last > (NAME_BEGINS_WITH_DEVICE (ret) ? 2 : 0);
315 if (IS_DIR_SEP (ret[last - 1])) {
316 /* If we have `/../', that's the same as `/'. */
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. */
331 temp = concat3 (ret, ((len > 0 && ret[len - 1] == DIR_SEP)
332 || (NAME_BEGINS_WITH_DEVICE (c) && *ret == 0))
333 ? "" : DIR_SEP_STRING,
341 /* Remove a trailing /, just in case it snuck in. */
343 if (len > 0 && ret[len - 1] == DIR_SEP) {
348 #endif /* not AMIGA */
351 /* Return directory ARGV0 comes from. Check PATH if ARGV0 is not
355 selfdir P1C(const_string, argv0)
360 if (kpse_absolute_p (argv0, true)) {
361 self = xstrdup (argv0);
365 #include <proto/dos.h>
366 #include <proto/exec.h>
368 struct DosLibrary *DOSBase
369 = (struct DosLibrary *) OpenLibrary ("dos.library", 0L);
372 self = xmalloc (BUFSIZ);
373 lock = findpath (argv0);
374 if (lock != ((BPTR) -1)) {
375 if (getpath (lock, self) == -1) {
378 strcat (self,DIR_SEP_STRING);
383 CloseLibrary((struct Library *) DOSBase);
384 #else /* not AMIGA */
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)) {
394 /* UNIX tradition interprets the empty path element as "." */
395 if (*elt == 0) elt = ".";
397 name = concat3 (elt, DIR_SEP_STRING, argv0);
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))
412 #endif /* not AMIGA */
415 /* If argv0 is somehow dir/exename, `self' will still be NULL. */
417 self = concat3 (".", DIR_SEP_STRING, argv0);
419 ret = xdirname (remove_dots (expand_symlinks (self)));
425 #endif /* not WIN32 */
428 kpse_set_program_name P2C(const_string, argv0, const_string, progname)
430 string ext, sdir, sdir_parent, sdir_grandparent;
431 string s = getenv ("KPATHSEA_DEBUG");
433 string debug_output = getenv("KPATHSEA_DEBUG_OUTPUT");
434 string append_debug_output = getenv("KPATHSEA_DEBUG_APPEND");
438 /* Set debugging stuff first, in case we end up doing debuggable stuff
439 during this initialization. */
441 kpathsea_debug |= atoi (s);
444 #ifndef HAVE_PROGRAM_INVOCATION_NAME
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)) {
453 extern int output_home_warning;
454 output_home_warning = 1;
459 /* redirect stderr to debug_output. Easier to send logfiles. */
461 int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
463 if (_stricmp(debug_output, "con") == 0
464 || _stricmp(debug_output, "con:") == 0) {
465 err = _fileno(stdout);
467 if (append_debug_output) {
468 flags = _O_CREAT | _O_APPEND | _O_WRONLY;
470 flags = _O_CREAT | _O_TRUNC | _O_WRONLY;
471 xputenv("KPATHSEA_DEBUG_APPEND", "yes");
476 && (err = _open(debug_output, flags, _S_IREAD | _S_IWRITE)) == -1)
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");
483 } else if (_dup2(err, fileno(stderr)) == -1) {
484 WARNING1("Can't redirect stderr to %s!\n", debug_output);
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. */
495 char short_path[PATH_MAX], path[PATH_MAX], *fp;
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);
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));
511 #elif defined(__DJGPP__)
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.
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];
522 if (_truename (argv0, long_progname)) {
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];
532 if (argv0[0] && argv0[1] == ':')
533 rootdir[0] = argv0[0]; /* explicit drive in `argv0' */
535 rootdir[0] = getdisk () + 'A';
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);
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);
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))
560 program_invocation_name = xstrdup (long_progname);
563 /* If `_truename' failed, God help them, because we won't... */
564 program_invocation_name = xstrdup (argv0);
567 program_invocation_name = xstrdup (argv0);
569 #else /* !WIN32 && !__DJGPP__ */
570 program_invocation_name = xstrdup (argv0);
572 #endif /* not HAVE_PROGRAM_INVOCATION_NAME */
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. */
578 sdir = selfdir (program_invocation_name);
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);
590 free (sdir_grandparent);
592 #ifndef HAVE_PROGRAM_INVOCATION_NAME
593 program_invocation_short_name = (string)xbasename (program_invocation_name);
597 kpse_program_name = xstrdup (progname);
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
603 ext = find_suffix (program_invocation_short_name);
604 if (ext && FILESTRCASEEQ (ext, "exe")) {
605 kpse_program_name = remove_suffix (program_invocation_short_name);
607 kpse_program_name = xstrdup (program_invocation_short_name);
611 xputenv ("progname", kpse_program_name);
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
618 kpse_set_progname P1C(const_string, argv0)
620 kpse_set_program_name (argv0, NULL);
625 main (int argc, char **argv)
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"));
641 standalone-compile-command: "gcc -g -I. -I.. -DTEST progname.c STATIC/libkpathsea.a"