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. */
73 string program_invocation_name = NULL;
74 string program_invocation_short_name = NULL;
76 /* And the variable for the program we pretend to be. */
77 string kpse_program_name = NULL;
80 /* From a standalone program `ll' to expand symlinks written by Kimbo Mundy.
81 Don't bother to compile if we don't have symlinks; thus we can assume
82 / as the separator. Also don't try to use basename, etc., or
83 handle arbitrary filename length. Mixed case function names because
84 that's what kimbo liked. */
87 static int ll_verbose = 0;
88 static int ll_loop = 0;
91 #define BSIZE 2048 /* sorry */
94 /* Read link FN into SYM. */
97 ReadSymLink P2C(char *, fn, char *, sym)
99 register int n = readlink (fn, sym, BSIZE);
108 /* Strip first component from S, and also return it in a static buffer. */
111 StripFirst P1C(register char *, s)
113 static char buf[BSIZE];
116 /* Find the end of the first path element */
117 for (s1 = s; *s1 && (*s1 != '/' || s1 == s); s1++)
120 /* Copy it into buf and null-terminate it. */
121 strncpy (buf, s, s1 - s);
124 /* Skip over the leading / (if any) */
128 /* Squeeze out the element */
129 while ((*s++ = *s1++) != 0)
136 /* Strip last component from S, and also return it in a static buffer. */
139 StripLast P1C(register char *, s)
141 static char buf[BSIZE];
144 for (s1 = s + strlen (s); s1 > s && *s1 != '/'; s1--)
146 strcpy (buf, s1 + (*s1 == '/'));
153 /* Copy first path element from B to A, removing it from B. */
156 CopyFirst P2C(register char *, a, char *, b)
158 register int length = strlen (a);
160 if (length > 0 && a[length - 1] != '/') {
164 strcat (a, StripFirst (b));
167 /* Returns NULL on error. Prints intermediate results if global
168 `ll_verbose' is nonzero. */
170 #define EX(s) (strlen (s) && strcmp (s, "/") ? "/" : "")
171 #define EXPOS EX(post)
172 #define EXPRE EX(pre)
175 expand_symlinks P1C(char *, s)
177 static char pre[BSIZE]; /* return value */
178 char post[BSIZE], sym[BSIZE], tmp[BSIZE], before[BSIZE];
184 /* Check for symlink loops. It's difficult to check for all the
185 possibilities ourselves, so let the kernel do it. And make it
186 conditional so that people can see where the infinite loop is
187 being caused (see engtools#1536). */
189 FILE *f = fopen (s, "r");
190 if (!f && errno == ELOOP) {
191 /* Not worried about other errors, we'll get to them in due course. */
201 while (strlen (post) != 0) {
202 CopyFirst (pre, post);
204 if (lstat (pre, &st) != 0) {
205 fprintf (stderr, "lstat(%s) failed ...\n", pre);
210 if (S_ISLNK (st.st_mode)) {
211 ReadSymLink (pre, sym);
213 if (!strncmp (sym, "/", 1)) {
215 printf ("[%s]%s%s -> [%s]%s%s\n", pre, EXPOS, post, sym, EXPOS,post);
219 a = pre[0]; /* handle links through the root */
220 strcpy (tmp, StripLast (pre));
221 if (!strlen (pre) && a == '/')
225 sprintf (before, "%s%s[%s]%s%s", pre, EXPRE, tmp, EXPOS, post);
226 printf ("%s -> %s%s[%s]%s%s\n", before, pre, EXPRE, sym, EXPOS,post);
229 /* Strip "../" path elements from the front of sym; print
230 new result if there were any such elements. */
232 a = pre[0]; /* handle links through the root */
233 while (!strncmp (sym, "..", 2)
234 && (sym[2] == 0 || sym[2] == '/')
237 && strcmp (pre, "..")
239 || strcmp (pre + strlen (pre) - 3, "/.."))) {
245 if (done && ll_verbose) {
246 for (cp = before; *cp;)
249 printf ("%s == %s%s%s%s%s\n", before, pre, EXPRE, sym, EXPOS,post);
251 printf ("%s == %s%s%s\n", before, pre, EXPOS, post);
253 if (!strlen (pre) && a == '/')
257 if (strlen (post) != 0 && strlen (sym) != 0)
267 #else /* not S_ISLNK */
268 #define expand_symlinks(s) (s)
269 #endif /* not S_ISLNK */
271 /* Remove .'s and ..'s in DIR, to avoid problems with relative symlinks
272 as the program name, etc. This does not canonicalize symlinks. */
275 remove_dots P1C(string, dir)
282 string ret = (string) ""; /* We always reassign. */
284 for (c = kpse_filename_component (dir); c;
285 c = kpse_filename_component (NULL)) {
286 if (STREQ (c, ".")) {
287 /* If leading ., replace with cwd. Else ignore. */
292 } else if (STREQ (c, "..")) {
293 /* If leading .., start with xdirname (cwd). Else remove last
294 component from ret, if any. */
296 string dot = xgetcwd ();
297 ret = xdirname (dot);
301 for (last = strlen (ret);
302 last > (NAME_BEGINS_WITH_DEVICE (ret) ? 2 : 0);
304 if (IS_DIR_SEP (ret[last - 1])) {
305 /* If we have `/../', that's the same as `/'. */
315 /* Not . or ..; just append. Include a directory separator unless
316 our string already ends with one. This also changes all directory
317 separators into the canonical DIR_SEP_STRING. */
320 temp = concat3 (ret, ((len > 0 && ret[len - 1] == DIR_SEP)
321 || (NAME_BEGINS_WITH_DEVICE (c) && *ret == 0))
322 ? "" : DIR_SEP_STRING,
330 /* Remove a trailing /, just in case it snuck in. */
332 if (len > 0 && ret[len - 1] == DIR_SEP) {
337 #endif /* not AMIGA */
340 /* Return directory ARGV0 comes from. Check PATH if ARGV0 is not
344 selfdir P1C(const_string, argv0)
349 if (kpse_absolute_p (argv0, true)) {
350 self = xstrdup (argv0);
354 #include <proto/dos.h>
355 #include <proto/exec.h>
357 struct DosLibrary *DOSBase
358 = (struct DosLibrary *) OpenLibrary ("dos.library", 0L);
361 self = xmalloc (BUFSIZ);
362 lock = findpath (argv0);
363 if (lock != ((BPTR) -1)) {
364 if (getpath (lock, self) == -1) {
367 strcat (self,DIR_SEP_STRING);
372 CloseLibrary((struct Library *) DOSBase);
373 #else /* not AMIGA */
377 /* Have to check PATH. But don't call kpse_path_search since we don't
378 want to search any ls-R's or do anything special with //'s. */
379 for (elt = kpse_path_element (getenv ("PATH")); !self && elt;
380 elt = kpse_path_element (NULL)) {
383 /* UNIX tradition interprets the empty path element as "." */
384 if (*elt == 0) elt = ".";
386 name = concat3 (elt, DIR_SEP_STRING, argv0);
388 /* In order to do this perfectly, we'd have to check the owner bits only
389 if we are the file owner, and the group bits only if we belong
390 to the file group. That's a lot of work, though, and it's not
391 likely that kpathsea will ever be used with a program that's
392 only executable by some classes and not others. See the
393 `file_status' function in execute_cmd.c in bash for what's
394 necessary if we were to do it right. */
395 if (stat (name, &s) == 0 && s.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) {
396 /* Do not stop at directories. */
397 if (!S_ISDIR(s.st_mode))
401 #endif /* not AMIGA */
404 /* If argv0 is somehow dir/exename, `self' will still be NULL. */
406 self = concat3 (".", DIR_SEP_STRING, argv0);
408 ret = xdirname (remove_dots (expand_symlinks (self)));
414 #endif /* not WIN32 */
417 kpse_set_program_name P2C(const_string, argv0, const_string, progname)
419 string ext, sdir, sdir_parent, sdir_grandparent;
420 string s = getenv ("KPATHSEA_DEBUG");
422 string debug_output = getenv("KPATHSEA_DEBUG_OUTPUT");
423 string append_debug_output = getenv("KPATHSEA_DEBUG_APPEND");
427 /* Set debugging stuff first, in case we end up doing debuggable stuff
428 during this initialization. */
430 kpathsea_debug |= atoi (s);
433 #ifndef HAVE_PROGRAM_INVOCATION_NAME
435 /* Set various info about user. Among many things,
436 ensure that HOME is set. If debug_paths is on,
437 turn on some message if $HOME is not found. */
438 if (KPSE_DEBUG_P(KPSE_DEBUG_PATHS)) {
442 extern int output_home_warning;
443 output_home_warning = 1;
448 /* redirect stderr to debug_output. Easier to send logfiles. */
450 int flags = _O_CREAT | _O_TRUNC | _O_RDWR;
452 if (_stricmp(debug_output, "con") == 0
453 || _stricmp(debug_output, "con:") == 0) {
454 err = _fileno(stdout);
456 if (append_debug_output) {
457 flags = _O_CREAT | _O_APPEND | _O_WRONLY;
459 flags = _O_CREAT | _O_TRUNC | _O_WRONLY;
460 xputenv("KPATHSEA_DEBUG_APPEND", "yes");
465 && (err = _open(debug_output, flags, _S_IREAD | _S_IWRITE)) == -1)
467 WARNING1("Can't open %s for stderr redirection!\n", debug_output);
468 perror(debug_output);
469 } else if ((olderr = _dup(fileno(stderr))) == -1) {
470 WARNING("Can't dup() stderr!\n");
472 } else if (_dup2(err, fileno(stderr)) == -1) {
473 WARNING1("Can't redirect stderr to %s!\n", debug_output);
480 /* Win95 always gives the short filename for argv0, not the long one.
481 There is only this way to catch it. It makes all the selfdir stuff
482 useless for win32. */
484 char short_path[PATH_MAX], path[PATH_MAX], *fp;
486 /* SearchPath() always gives back an absolute directory */
487 if (SearchPath(NULL, argv0, ".exe", PATH_MAX, short_path, &fp) == 0)
488 FATAL1("Can't determine where the executable %s is.\n", argv0);
489 if (!win32_get_long_filename(short_path, path, sizeof(path))) {
490 FATAL1("This path points to an invalid file : %s\n", short_path);
492 /* slashify the dirname */
493 for (fp = path; fp && *fp; fp++)
494 if (IS_DIR_SEP(*fp)) *fp = DIR_SEP;
495 /* sdir will be the directory of the executable, ie: c:/TeX/bin */
496 sdir = xdirname(path);
497 program_invocation_name = xstrdup(xbasename(path));
500 #elif defined(__DJGPP__)
502 /* DJGPP programs support long filenames on Windows 95, but ARGV0 there
503 is always made with the short 8+3 aliases of all the pathname elements.
504 If long names are supported, we need to convert that to a long name.
506 All we really need is to call `_truename', but most of the code
507 below is required to deal with the special case of networked drives. */
508 if (pathconf (argv0, _PC_NAME_MAX) > 12) {
509 char long_progname[PATH_MAX];
511 if (_truename (argv0, long_progname)) {
514 if (long_progname[1] != ':') {
515 /* A complication: `_truename' returns network-specific string at
516 the beginning of `long_progname' when the program resides on a
517 networked drive, and DOS calls cannot grok such pathnames. We
518 need to convert the filesystem name back to a drive letter. */
519 char rootname[PATH_MAX], rootdir[4];
521 if (argv0[0] && argv0[1] == ':')
522 rootdir[0] = argv0[0]; /* explicit drive in `argv0' */
524 rootdir[0] = getdisk () + 'A';
528 if (_truename (rootdir, rootname)) {
529 /* Find out where `rootname' ends in `long_progname' and replace
530 it with the drive letter. */
531 int root_len = strlen (rootname);
533 if (IS_DIR_SEP (rootname[root_len - 1]))
534 root_len--; /* keep the trailing slash */
535 long_progname[0] = rootdir[0];
536 long_progname[1] = ':';
537 memmove (long_progname + 2, long_progname + root_len,
538 strlen (long_progname + root_len) + 1);
542 /* Convert everything to canonical form. */
543 if (long_progname[0] >= 'A' && long_progname[0] <= 'Z')
544 long_progname[0] += 'a' - 'A'; /* make drive lower case, for beauty */
545 for (fp = long_progname; *fp; fp++)
546 if (IS_DIR_SEP (*fp))
549 program_invocation_name = xstrdup (long_progname);
552 /* If `_truename' failed, God help them, because we won't... */
553 program_invocation_name = xstrdup (argv0);
556 program_invocation_name = xstrdup (argv0);
558 #else /* !WIN32 && !__DJGPP__ */
560 program_invocation_name = xstrdup (argv0);
563 #endif /* not HAVE_PROGRAM_INVOCATION_NAME */
565 /* We need to find SELFAUTOLOC *before* removing the ".exe" suffix from
566 the program_name, otherwise the PATH search inside selfdir will fail,
567 since `prog' doesn't exists as a file, there's `prog.exe' instead. */
569 sdir = selfdir (program_invocation_name);
571 /* SELFAUTODIR is actually the parent of the invocation directory,
572 and SELFAUTOPARENT the grandparent. This is how teTeX did it. */
573 xputenv ("SELFAUTOLOC", sdir);
574 sdir_parent = xdirname (sdir);
575 xputenv ("SELFAUTODIR", sdir_parent);
576 sdir_grandparent = xdirname (sdir_parent);
577 xputenv ("SELFAUTOPARENT", sdir_grandparent);
581 free (sdir_grandparent);
583 #ifndef HAVE_PROGRAM_INVOCATION_NAME
584 program_invocation_short_name = (string)xbasename (program_invocation_name);
588 kpse_program_name = xstrdup (progname);
590 /* If configured --enable-shared and running from the build directory
591 with the wrapper scripts (e.g., for make check), the binaries will
592 be named foo.exe instead of foo. Or possibly if we're running on a
594 ext = find_suffix (program_invocation_short_name);
595 if (ext && FILESTRCASEEQ (ext, "exe")) {
596 kpse_program_name = remove_suffix (program_invocation_short_name);
598 kpse_program_name = xstrdup (program_invocation_short_name);
602 xputenv("progname", kpse_program_name);
605 /* This function is deprecated, because when we pretend to have a different
606 name it will look for _that_ name in the PATH if program_invocation_name
609 kpse_set_progname P1C(const_string, argv0)
611 kpse_set_program_name (argv0, NULL);
616 main (int argc, char **argv)
618 puts (remove_dots ("/w/kpathsea"));
619 puts (remove_dots ("/w//kpathsea"));
620 puts (remove_dots ("/w/./kpathsea"));
621 puts (remove_dots ("."));
622 puts (remove_dots ("./"));
623 puts (remove_dots ("./."));
624 puts (remove_dots ("../kpathsea"));
625 puts (remove_dots ("/kpathsea/../foo"));
626 puts (remove_dots ("/../w/kpathsea"));
627 puts (remove_dots ("/../w/kpathsea/."));
628 puts (remove_dots ("/te/share/texmf/../../../../bin/gnu"));
632 standalone-compile-command: "gcc -g -I. -I.. -DTEST progname.c STATIC/libkpathsea.a"