| 1 |
/* progname.c: the executable name we were invoked as; general initialization. |
| 2 |
|
| 3 |
Copyright 1994, 1996, 1997, 2008-2013, 2016-2019 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-pathmx.h> |
| 23 |
#include <kpathsea/c-stat.h> |
| 24 |
#include <kpathsea/pathsearch.h> |
| 25 |
/* For kpse_reset_progname */ |
| 26 |
#include <kpathsea/tex-file.h> |
| 27 |
|
| 28 |
|
| 29 |
#if defined(__i386_pc_gnu__) |
| 30 |
#ifndef _S_ISUID |
| 31 |
#define _S_ISUID 04000 /* Set user ID on execution. */ |
| 32 |
#endif |
| 33 |
#ifndef _S_ISGID |
| 34 |
#define _S_ISGID 02000 /* Set group ID on execution. */ |
| 35 |
#endif |
| 36 |
#ifndef _S_ISVTX |
| 37 |
#define _S_ISVTX 01000 /* Save swapped text after use (sticky). */ |
| 38 |
#endif |
| 39 |
#ifndef _S_IREAD |
| 40 |
#define _S_IREAD 0400 /* Read by owner. */ |
| 41 |
#endif |
| 42 |
#ifndef _S_IWRITE |
| 43 |
#define _S_IWRITE 0200 /* Write by owner. */ |
| 44 |
#endif |
| 45 |
#ifndef _S_IEXEC |
| 46 |
#define _S_IEXEC 0100 /* Execute by owner. */ |
| 47 |
#endif |
| 48 |
#endif |
| 49 |
|
| 50 |
/* NeXT does not define the standard macros, but has the equivalent. |
| 51 |
WIN32 doesn't define them either, and doesn't have them. |
| 52 |
From: Gregor Hoffleit <[email protected]>. */ |
| 53 |
#ifndef S_IXUSR |
| 54 |
#ifdef WIN32 |
| 55 |
#define S_IXUSR 0 |
| 56 |
#define S_IXGRP 0 |
| 57 |
#define S_IXOTH 0 |
| 58 |
#else /* not WIN32 */ |
| 59 |
#define S_IXUSR 0100 |
| 60 |
#endif /* not WIN32 */ |
| 61 |
#endif /* not S_IXUSR */ |
| 62 |
#ifndef S_IXGRP |
| 63 |
#define S_IXGRP 0010 |
| 64 |
#endif |
| 65 |
#ifndef S_IXOTH |
| 66 |
#define S_IXOTH 0001 |
| 67 |
#endif |
| 68 |
|
| 69 |
|
| 70 |
#ifndef WIN32 |
| 71 |
/* From a standalone program `ll' to expand symlinks written by Kimbo Mundy. |
| 72 |
Don't bother to compile if we don't have symlinks; thus we can assume |
| 73 |
/ as the separator. Also don't try to use basename, etc., or |
| 74 |
handle arbitrary filename length. Mixed case function names because |
| 75 |
that's what kimbo liked. */ |
| 76 |
|
| 77 |
#ifdef S_ISLNK |
| 78 |
|
| 79 |
#undef BSIZE |
| 80 |
#define BSIZE 2048 /* sorry */ |
| 81 |
|
| 82 |
|
| 83 |
/* Read link FN into SYM. */ |
| 84 |
|
| 85 |
static void |
| 86 |
ReadSymLink (char *fn, char *sym) |
| 87 |
{ |
| 88 |
int n = readlink (fn, sym, BSIZE); |
| 89 |
if (n < 0) { |
| 90 |
perror (fn); |
| 91 |
exit (1); |
| 92 |
} |
| 93 |
sym[n] = 0; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/* Strip first component from S, and also return it in a static buffer. */ |
| 98 |
|
| 99 |
static char * |
| 100 |
StripFirst (char *s) |
| 101 |
{ |
| 102 |
static char buf[BSIZE]; |
| 103 |
char *s1; |
| 104 |
|
| 105 |
/* Find the end of the first path element */ |
| 106 |
for (s1 = s; *s1 && (*s1 != '/' || s1 == s); s1++) |
| 107 |
; |
| 108 |
|
| 109 |
/* Copy it into buf and null-terminate it. */ |
| 110 |
strncpy (buf, s, s1 - s); |
| 111 |
buf[s1 - s] = 0; |
| 112 |
|
| 113 |
/* Skip over the leading / (if any) */ |
| 114 |
if (*s1 == '/') |
| 115 |
++s1; |
| 116 |
|
| 117 |
/* Squeeze out the element */ |
| 118 |
while ((*s++ = *s1++) != 0) |
| 119 |
; |
| 120 |
|
| 121 |
return buf; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/* Strip last component from S, and also return it in a static buffer. */ |
| 126 |
|
| 127 |
static char * |
| 128 |
StripLast (char *s) |
| 129 |
{ |
| 130 |
static char buf[BSIZE]; |
| 131 |
char *s1; |
| 132 |
|
| 133 |
for (s1 = s + strlen (s); s1 > s && *s1 != '/'; s1--) |
| 134 |
; |
| 135 |
strcpy (buf, s1 + (*s1 == '/')); |
| 136 |
*s1 = 0; |
| 137 |
|
| 138 |
return buf; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/* Copy first path element from B to A, removing it from B. */ |
| 143 |
|
| 144 |
static void |
| 145 |
CopyFirst (char *a, char *b) |
| 146 |
{ |
| 147 |
int length = strlen (a); |
| 148 |
|
| 149 |
if (length > 0 && a[length - 1] != '/') { |
| 150 |
a[length] = '/'; |
| 151 |
a[length + 1] = 0; |
| 152 |
} |
| 153 |
strcat (a, StripFirst (b)); |
| 154 |
} |
| 155 |
|
| 156 |
/* Returns NULL on error, such as an unresolvable symlink. Prints |
| 157 |
intermediate results if global `ll_verbose' is nonzero. Otherwise, |
| 158 |
returns a pointer to a static buffer (sorry). */ |
| 159 |
|
| 160 |
#define EMPTY_STRING(s) (*(s) == 0) |
| 161 |
#define EX(s) (!EMPTY_STRING (s) && strcmp (s, "/") ? "/" : "") |
| 162 |
#define EXPOS EX(post) |
| 163 |
#define EXPRE EX(pre) |
| 164 |
|
| 165 |
static char * |
| 166 |
expand_symlinks (kpathsea kpse, char *s) |
| 167 |
{ |
| 168 |
static char pre[BSIZE]; /* return value */ |
| 169 |
char post[BSIZE], sym[BSIZE], tmp[BSIZE], before[BSIZE]; |
| 170 |
char *cp; |
| 171 |
char a; |
| 172 |
struct stat st; |
| 173 |
int done; |
| 174 |
|
| 175 |
strcpy (post, s); |
| 176 |
strcpy (pre, ""); |
| 177 |
|
| 178 |
while (!EMPTY_STRING (post)) { |
| 179 |
CopyFirst (pre, post); |
| 180 |
|
| 181 |
if (lstat (pre, &st) != 0) { |
| 182 |
fprintf (stderr, "lstat(%s) failed: ", pre); |
| 183 |
perror (pre); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
if (S_ISLNK (st.st_mode)) { |
| 188 |
ReadSymLink (pre, sym); |
| 189 |
|
| 190 |
if (!strncmp (sym, "/", 1)) { |
| 191 |
if (kpse->ll_verbose) |
| 192 |
printf ("[%s]%s%s -> [%s]%s%s\n", pre, EXPOS, post, sym, EXPOS,post); |
| 193 |
strcpy (pre, ""); |
| 194 |
|
| 195 |
} else { |
| 196 |
a = pre[0]; /* handle links through the root */ |
| 197 |
strcpy (tmp, StripLast (pre)); |
| 198 |
if (EMPTY_STRING (pre) && a == '/') |
| 199 |
strcpy (pre, "/"); |
| 200 |
|
| 201 |
if (kpse->ll_verbose) { |
| 202 |
sprintf (before, "%s%s[%s]%s%s", pre, EXPRE, tmp, EXPOS, post); |
| 203 |
printf ("%s -> %s%s[%s]%s%s\n", before, pre, EXPRE, sym, EXPOS,post); |
| 204 |
} |
| 205 |
|
| 206 |
/* Strip "../" path elements from the front of sym; print |
| 207 |
new result if there were any such elements. */ |
| 208 |
done = 0; |
| 209 |
a = pre[0]; /* handle links through the root */ |
| 210 |
while (!strncmp (sym, "..", 2) |
| 211 |
&& (sym[2] == 0 || sym[2] == '/') |
| 212 |
&& !EMPTY_STRING (pre) |
| 213 |
&& strcmp (pre, ".") |
| 214 |
&& strcmp (pre, "..") |
| 215 |
&& (strlen (pre) < 3 |
| 216 |
|| strcmp (pre + strlen (pre) - 3, "/.."))) { |
| 217 |
done = 1; |
| 218 |
StripFirst (sym); |
| 219 |
StripLast (pre); |
| 220 |
} |
| 221 |
|
| 222 |
if (done && kpse->ll_verbose) { |
| 223 |
for (cp = before; *cp;) |
| 224 |
*cp++ = ' '; |
| 225 |
if (strlen (sym)) |
| 226 |
printf ("%s == %s%s%s%s%s\n", before, pre, EXPRE, sym, EXPOS,post); |
| 227 |
else |
| 228 |
printf ("%s == %s%s%s\n", before, pre, EXPOS, post); |
| 229 |
} |
| 230 |
if (EMPTY_STRING (pre) && a == '/') |
| 231 |
strcpy (pre, "/"); |
| 232 |
} |
| 233 |
|
| 234 |
if (!EMPTY_STRING (post) && !EMPTY_STRING (sym)) |
| 235 |
strcat (sym, "/"); |
| 236 |
|
| 237 |
strcat (sym, post); |
| 238 |
strcpy (post, sym); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return pre; |
| 243 |
} |
| 244 |
#else /* not S_ISLNK */ |
| 245 |
#define expand_symlinks(k,s) (s) |
| 246 |
#endif /* not S_ISLNK */ |
| 247 |
|
| 248 |
/* Remove .'s and ..'s in DIR, to avoid problems with relative symlinks |
| 249 |
as the program name, etc. This does not canonicalize symlinks. */ |
| 250 |
|
| 251 |
static string |
| 252 |
remove_dots (kpathsea kpse, string dir) |
| 253 |
{ |
| 254 |
#ifdef AMIGA |
| 255 |
return dir; |
| 256 |
#else |
| 257 |
string c; |
| 258 |
unsigned len; |
| 259 |
string ret = NULL; |
| 260 |
|
| 261 |
for (c = kpathsea_filename_component (kpse, dir); c; |
| 262 |
c = kpathsea_filename_component (kpse, NULL)) { |
| 263 |
if (STREQ (c, ".")) { |
| 264 |
/* If leading ., replace with cwd. Else ignore. */ |
| 265 |
if (!ret) { |
| 266 |
ret = xgetcwd (); |
| 267 |
} |
| 268 |
|
| 269 |
} else if (STREQ (c, "..")) { |
| 270 |
/* If leading .., start with xdirname (cwd). Else remove last |
| 271 |
component from ret, if any. */ |
| 272 |
if (!ret) { |
| 273 |
string dot = xgetcwd (); |
| 274 |
ret = xdirname (dot); |
| 275 |
free (dot); |
| 276 |
} else { |
| 277 |
unsigned last; |
| 278 |
string p = NAME_BEGINS_WITH_DEVICE (ret) ? ret + 2 : ret; |
| 279 |
for (last = strlen (p); last > 0; last--) { |
| 280 |
if (IS_DIR_SEP_CH (p[last - 1])) { |
| 281 |
/* If we have `/../', that's the same as `/'. */ |
| 282 |
p[(last > 1 ? last - 1 : 1)] = 0; |
| 283 |
break; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
} else { |
| 289 |
/* Not . or ..; just append. Include a directory separator unless |
| 290 |
our string already ends with one. This also changes all directory |
| 291 |
separators into the canonical DIR_SEP_STRING. */ |
| 292 |
if (!ret) { |
| 293 |
ret = concat (NAME_BEGINS_WITH_DEVICE (c) ? "" : DIR_SEP_STRING, c); |
| 294 |
} else { |
| 295 |
string temp = ret; |
| 296 |
len = strlen (ret); |
| 297 |
ret = concat3 (ret, ret[len - 1] == DIR_SEP ? "" : DIR_SEP_STRING, c); |
| 298 |
free (temp); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
assert (ret); |
| 303 |
|
| 304 |
/* Remove a trailing /, just in case it snuck in. */ |
| 305 |
len = strlen (ret); |
| 306 |
if (len > 0 && ret[len - 1] == DIR_SEP) { |
| 307 |
ret[len - 1] = 0; |
| 308 |
} |
| 309 |
|
| 310 |
return ret; |
| 311 |
#endif /* not AMIGA */ |
| 312 |
} |
| 313 |
|
| 314 |
/* Return directory ARGV0 comes from. Check PATH if ARGV0 is not |
| 315 |
absolute. If ARGV0 cannot be found (e.g., --progname=nonesuch), quit. */ |
| 316 |
|
| 317 |
string |
| 318 |
kpathsea_selfdir (kpathsea kpse, const_string argv0) |
| 319 |
{ |
| 320 |
string name; |
| 321 |
string ret; |
| 322 |
string self = NULL; |
| 323 |
|
| 324 |
if (kpathsea_absolute_p (kpse, argv0, true)) { |
| 325 |
self = xstrdup (argv0); |
| 326 |
} else { |
| 327 |
#ifdef AMIGA |
| 328 |
#include <dos.h> |
| 329 |
#include <proto/dos.h> |
| 330 |
#include <proto/exec.h> |
| 331 |
BPTR lock; |
| 332 |
struct DosLibrary *DOSBase |
| 333 |
= (struct DosLibrary *) OpenLibrary ("dos.library", 0L); |
| 334 |
assert (DOSBase); |
| 335 |
|
| 336 |
self = xmalloc (BUFSIZ); |
| 337 |
lock = findpath (argv0); |
| 338 |
if (lock != ((BPTR) -1)) { |
| 339 |
if (getpath (lock, self) == -1) { |
| 340 |
*self = '\0'; |
| 341 |
} else { |
| 342 |
strcat (self,DIR_SEP_STRING); |
| 343 |
strcat (self,argv0); |
| 344 |
} |
| 345 |
UnLock (lock); |
| 346 |
} |
| 347 |
CloseLibrary((struct Library *) DOSBase); |
| 348 |
#else /* not AMIGA */ |
| 349 |
const_string elt; |
| 350 |
struct stat s; |
| 351 |
|
| 352 |
/* Have to check PATH. But don't call kpse_path_search since we don't |
| 353 |
want to search any ls-R's or do anything special with //'s. */ |
| 354 |
for (elt = kpathsea_path_element (kpse, getenv ("PATH")); !self && elt; |
| 355 |
elt = kpathsea_path_element (kpse, NULL)) { |
| 356 |
/* UNIX tradition interprets the empty path element as "." */ |
| 357 |
if (*elt == 0) elt = "."; |
| 358 |
|
| 359 |
name = concat3 (elt, DIR_SEP_STRING, argv0); |
| 360 |
|
| 361 |
/* In order to do this perfectly, we'd have to check the owner bits only |
| 362 |
if we are the file owner, and the group bits only if we belong |
| 363 |
to the file group. That's a lot of work, though, and it's not |
| 364 |
likely that kpathsea will ever be used with a program that's |
| 365 |
only executable by some classes and not others. See the |
| 366 |
`file_status' function in execute_cmd.c in bash for what's |
| 367 |
necessary if we were to do it right. */ |
| 368 |
if (stat (name, &s) == 0 && s.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH) |
| 369 |
/* Do not stop at directories. */ |
| 370 |
&& !S_ISDIR(s.st_mode)) |
| 371 |
self = name; |
| 372 |
else |
| 373 |
free (name); |
| 374 |
} |
| 375 |
#endif /* not AMIGA */ |
| 376 |
} |
| 377 |
|
| 378 |
/* If argv0 is somehow dir/exename, `self' will still be NULL. */ |
| 379 |
if (!self) |
| 380 |
self = concat3 (".", DIR_SEP_STRING, argv0); |
| 381 |
|
| 382 |
/* If we can't expand symlinks (--progname=nonesuch), give up. */ |
| 383 |
name = expand_symlinks (kpse, self); |
| 384 |
if (!name) { |
| 385 |
fprintf (stderr, "kpathsea: Can't get directory of program name: %s\n", |
| 386 |
self); |
| 387 |
exit (1); |
| 388 |
} |
| 389 |
|
| 390 |
/* If we have something real, we can resolve ./ and ../ elements. */ |
| 391 |
name = remove_dots (kpse, name); |
| 392 |
|
| 393 |
#ifndef AMIGA |
| 394 |
free (self); |
| 395 |
#endif |
| 396 |
|
| 397 |
ret = xdirname (name); |
| 398 |
free (name); |
| 399 |
return ret; |
| 400 |
} |
| 401 |
|
| 402 |
#if defined (KPSE_COMPAT_API) |
| 403 |
string |
| 404 |
kpse_selfdir (const_string argv0) |
| 405 |
{ |
| 406 |
return kpathsea_selfdir (kpse_def, argv0); |
| 407 |
} |
| 408 |
#endif |
| 409 |
|
| 410 |
#endif /* not WIN32 */ |
| 411 |
|
| 412 |
#if defined(WIN32) || defined(__CYGWIN__) |
| 413 |
|
| 414 |
/* Create a list of executable suffixes of files not to be written. */ |
| 415 |
#define EXE_SUFFIXES ".com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.ws;.tcl;.py;.pyw" |
| 416 |
|
| 417 |
static void |
| 418 |
mk_suffixlist (kpathsea kpse) |
| 419 |
{ |
| 420 |
char **p; |
| 421 |
char *q, *r, *v; |
| 422 |
int n; |
| 423 |
|
| 424 |
#if defined(__CYGWIN__) |
| 425 |
v = xstrdup (EXE_SUFFIXES); |
| 426 |
#else |
| 427 |
v = getenv ("PATHEXT"); |
| 428 |
if (v) /* strlwr() exists also in MingW */ |
| 429 |
v = strlwr (xstrdup (v)); |
| 430 |
else |
| 431 |
v = xstrdup (EXE_SUFFIXES); |
| 432 |
#endif |
| 433 |
|
| 434 |
q = v; |
| 435 |
n = 0; |
| 436 |
|
| 437 |
while ((r = strchr (q, ';')) != NULL) { |
| 438 |
n++; |
| 439 |
r++; |
| 440 |
q = r; |
| 441 |
} |
| 442 |
if (*q) |
| 443 |
n++; |
| 444 |
kpse->suffixlist = (char **) xmalloc ((n + 2) * sizeof (char *)); |
| 445 |
p = kpse->suffixlist; |
| 446 |
*p = xstrdup (".dll"); |
| 447 |
p++; |
| 448 |
q = v; |
| 449 |
while ((r = strchr (q, ';')) != NULL) { |
| 450 |
*r = '\0'; |
| 451 |
*p = xstrdup (q); |
| 452 |
p++; |
| 453 |
r++; |
| 454 |
q = r; |
| 455 |
} |
| 456 |
if (*q) { |
| 457 |
*p = xstrdup (q); |
| 458 |
p++; |
| 459 |
} |
| 460 |
*p = NULL; |
| 461 |
free (v); |
| 462 |
} |
| 463 |
#endif /* WIN32 || __CYGWIN__ */ |
| 464 |
|
| 465 |
/* On win32 SELFAUTO{LOC,DIR,PARENT} must not be just `/', otherwise, |
| 466 |
e.g., $SELFAUTODIR/texmf/tex would be mistaken as UNC name. */ |
| 467 |
static inline string |
| 468 |
fix_selfdir (string dir) |
| 469 |
{ |
| 470 |
#if defined(WIN32) |
| 471 |
if (IS_DIR_SEP_CH (*dir) && dir[1] == 0) |
| 472 |
dir++; |
| 473 |
#endif |
| 474 |
return dir; |
| 475 |
} |
| 476 |
|
| 477 |
void |
| 478 |
kpathsea_set_program_name (kpathsea kpse, const_string argv0, |
| 479 |
const_string progname) |
| 480 |
{ |
| 481 |
const_string ext; |
| 482 |
string sdir, sdir_parent, sdir_grandparent, sdir_greatgrandparent; |
| 483 |
string s = getenv ("KPATHSEA_DEBUG"); |
| 484 |
#ifdef WIN32 |
| 485 |
string debug_output = getenv("KPATHSEA_DEBUG_OUTPUT"); |
| 486 |
string append_debug_output = getenv("KPATHSEA_DEBUG_APPEND"); |
| 487 |
int err, olderr, cp; |
| 488 |
#endif |
| 489 |
|
| 490 |
/* Set debugging stuff first, in case we end up doing debuggable stuff |
| 491 |
during this initialization. */ |
| 492 |
if (s) { |
| 493 |
kpse->debug |= atoi (s); |
| 494 |
} |
| 495 |
|
| 496 |
#if defined(WIN32) |
| 497 |
cp = AreFileApisANSI() ? GetACP() : GetOEMCP(); |
| 498 |
if (!kpse->File_system_codepage) |
| 499 |
kpse->File_system_codepage = cp; |
| 500 |
kpse->Win32_codepage = cp; |
| 501 |
if (cp == 932 || cp == 936 || cp == 950) { |
| 502 |
kpse->Is_cp932_system = cp; |
| 503 |
} |
| 504 |
else |
| 505 |
kpse->Is_cp932_system = 0; |
| 506 |
|
| 507 |
#if defined(__MINGW32__) |
| 508 |
/* Set various info about user. Among many things, |
| 509 |
ensure that HOME is set. */ |
| 510 |
init_user_info(); |
| 511 |
#else /* !__MINGW32__ */ |
| 512 |
kpse->the_passwd.pw_name = kpse->the_passwd_name; |
| 513 |
kpse->the_passwd.pw_passwd = kpse->the_passwd_passwd; |
| 514 |
kpse->the_passwd.pw_uid = 0; |
| 515 |
kpse->the_passwd.pw_gid = 0; |
| 516 |
kpse->the_passwd.pw_quota = 0; |
| 517 |
kpse->the_passwd.pw_gecos = kpse->the_passwd_gecos; |
| 518 |
kpse->the_passwd.pw_dir = kpse->the_passwd_dir; |
| 519 |
kpse->the_passwd.pw_shell = kpse->the_passwd_shell; |
| 520 |
kpse->__system_allow_multiple_cmds = 0; |
| 521 |
|
| 522 |
/* Set various info about user. Among many things, |
| 523 |
ensure that HOME is set. */ |
| 524 |
kpathsea_init_user_info(kpse); |
| 525 |
#endif /* !__MINGW32__ */ |
| 526 |
|
| 527 |
/* redirect stderr to debug_output. Easier to send logfiles. */ |
| 528 |
if (debug_output) { |
| 529 |
int flags = _O_CREAT | _O_TRUNC | _O_RDWR; |
| 530 |
err = -1; |
| 531 |
if (_stricmp(debug_output, "con") == 0 |
| 532 |
|| _stricmp(debug_output, "con:") == 0) { |
| 533 |
err = _fileno(stdout); |
| 534 |
} else { |
| 535 |
if (append_debug_output) { |
| 536 |
flags = _O_CREAT | _O_APPEND | _O_WRONLY; |
| 537 |
} else { |
| 538 |
flags = _O_CREAT | _O_TRUNC | _O_WRONLY; |
| 539 |
kpathsea_xputenv(kpse, "KPATHSEA_DEBUG_APPEND", "yes"); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
if ((err < 0) |
| 544 |
&& (err = _open(debug_output, flags, _S_IREAD | _S_IWRITE)) == -1) |
| 545 |
{ |
| 546 |
WARNING1 ("kpathsea: Can't open %s for stderr redirection!\n", |
| 547 |
debug_output); |
| 548 |
perror (debug_output); |
| 549 |
} else if ((olderr = _dup(fileno(stderr))) == -1) { |
| 550 |
WARNING ("kpathsea: Can't dup() stderr!\n"); |
| 551 |
close (err); |
| 552 |
} else if (_dup2(err, fileno(stderr)) == -1) { |
| 553 |
WARNING1 ("kpathsea: Can't redirect stderr to %s!\n", debug_output); |
| 554 |
close (olderr); |
| 555 |
close (err); |
| 556 |
} else { |
| 557 |
close (err); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
/* Win95 always gives the short filename for argv0, not the long one. |
| 562 |
There is only this way to catch it. It makes all the kpse_selfdir |
| 563 |
stuff useless for win32. */ |
| 564 |
{ |
| 565 |
char short_path[PATH_MAX], path[PATH_MAX], *fp; |
| 566 |
#if !defined(__MINGW32__) |
| 567 |
HANDLE hnd; |
| 568 |
WIN32_FIND_DATA ffd; |
| 569 |
#endif |
| 570 |
|
| 571 |
/* SearchPath() always gives back an absolute directory */ |
| 572 |
if (SearchPath(NULL, argv0, ".exe", PATH_MAX, short_path, &fp) == 0) |
| 573 |
LIB_FATAL1("Can't determine where the executable %s is.\n", argv0); |
| 574 |
#if defined(__MINGW32__) |
| 575 |
if (!win32_get_long_filename(short_path, path, sizeof(path))) { |
| 576 |
LIB_FATAL1("This path points to an invalid file : %s\n", short_path); |
| 577 |
} |
| 578 |
/* slashify the dirname */ |
| 579 |
for (fp = path; fp && *fp; fp++) |
| 580 |
if (IS_DIR_SEP(*fp)) *fp = DIR_SEP; |
| 581 |
#else /* !__MINGW32__ */ |
| 582 |
if (kpathsea_getlongpath(kpse, path, short_path, PATH_MAX) == 0) |
| 583 |
FATAL1("Can't get long name for %s.\n", short_path); |
| 584 |
if ((hnd = FindFirstFile(short_path, &ffd)) == INVALID_HANDLE_VALUE) |
| 585 |
FATAL1("The following path points to an invalid file : %s\n", path); |
| 586 |
FindClose(hnd); |
| 587 |
/* dirname aleady slashified in WIN32 */ |
| 588 |
#endif /* !__MINGW32__ */ |
| 589 |
/* sdir will be the directory of the executable, ie: c:/TeX/bin */ |
| 590 |
sdir = xdirname(path); |
| 591 |
kpse->invocation_name = xstrdup(xbasename(path)); |
| 592 |
} |
| 593 |
|
| 594 |
#elif defined(__DJGPP__) |
| 595 |
|
| 596 |
/* DJGPP programs support long filenames on Windows 95, but ARGV0 there |
| 597 |
is always made with the short 8+3 aliases of all the pathname elements. |
| 598 |
If long names are supported, we need to convert that to a long name. |
| 599 |
|
| 600 |
All we really need is to call `_truename', but most of the code |
| 601 |
below is required to deal with the special case of networked drives. */ |
| 602 |
if (pathconf (argv0, _PC_NAME_MAX) > 12) { |
| 603 |
char long_progname[PATH_MAX]; |
| 604 |
|
| 605 |
if (_truename (argv0, long_progname)) { |
| 606 |
char *fp; |
| 607 |
|
| 608 |
if (long_progname[1] != ':') { |
| 609 |
/* A complication: `_truename' returns network-specific string at |
| 610 |
the beginning of `long_progname' when the program resides on a |
| 611 |
networked drive, and DOS calls cannot grok such pathnames. We |
| 612 |
need to convert the filesystem name back to a drive letter. */ |
| 613 |
char rootname[PATH_MAX], rootdir[4]; |
| 614 |
|
| 615 |
if (argv0[0] && argv0[1] == ':') |
| 616 |
rootdir[0] = argv0[0]; /* explicit drive in `argv0' */ |
| 617 |
else |
| 618 |
rootdir[0] = getdisk () + 'A'; |
| 619 |
rootdir[1] = ':'; |
| 620 |
rootdir[2] = '\\'; |
| 621 |
rootdir[3] = '\0'; |
| 622 |
if (_truename (rootdir, rootname)) { |
| 623 |
/* Find out where `rootname' ends in `long_progname' and replace |
| 624 |
it with the drive letter. */ |
| 625 |
int root_len = strlen (rootname); |
| 626 |
|
| 627 |
if (IS_DIR_SEP (rootname[root_len - 1])) |
| 628 |
root_len--; /* keep the trailing slash */ |
| 629 |
long_progname[0] = rootdir[0]; |
| 630 |
long_progname[1] = ':'; |
| 631 |
memmove (long_progname + 2, long_progname + root_len, |
| 632 |
strlen (long_progname + root_len) + 1); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
/* Convert everything to canonical form. */ |
| 637 |
if (long_progname[0] >= 'A' && long_progname[0] <= 'Z') |
| 638 |
long_progname[0] += 'a' - 'A'; /* make drive lower case, for beauty */ |
| 639 |
for (fp = long_progname; *fp; fp++) |
| 640 |
if (IS_DIR_SEP (*fp)) |
| 641 |
*fp = DIR_SEP; |
| 642 |
|
| 643 |
kpse->invocation_name = xstrdup (long_progname); |
| 644 |
} |
| 645 |
else |
| 646 |
/* If `_truename' failed, God help them, because we won't... */ |
| 647 |
kpse->invocation_name = xstrdup (argv0); |
| 648 |
} |
| 649 |
else |
| 650 |
kpse->invocation_name = xstrdup (argv0); |
| 651 |
|
| 652 |
#else /* !WIN32 && !__DJGPP__ */ |
| 653 |
kpse->invocation_name = xstrdup (argv0); |
| 654 |
#endif |
| 655 |
|
| 656 |
/* We need to find SELFAUTOLOC *before* removing the ".exe" suffix from |
| 657 |
the program_name, otherwise the PATH search inside kpse_selfdir will fail, |
| 658 |
since `prog' doesn't exists as a file, there's `prog.exe' instead. */ |
| 659 |
#ifndef WIN32 |
| 660 |
sdir = kpathsea_selfdir (kpse, kpse->invocation_name); |
| 661 |
#endif |
| 662 |
/* SELFAUTODIR is actually the parent of the invocation directory, |
| 663 |
and SELFAUTOPARENT the grandparent. This is how teTeX did it. */ |
| 664 |
kpathsea_xputenv (kpse, "SELFAUTOLOC", fix_selfdir (sdir)); |
| 665 |
sdir_parent = xdirname (sdir); |
| 666 |
kpathsea_xputenv (kpse, "SELFAUTODIR", fix_selfdir (sdir_parent)); |
| 667 |
sdir_grandparent = xdirname (sdir_parent); |
| 668 |
kpathsea_xputenv (kpse, "SELFAUTOPARENT", fix_selfdir (sdir_grandparent)); |
| 669 |
sdir_greatgrandparent = xdirname (sdir_grandparent); |
| 670 |
kpathsea_xputenv (kpse, "SELFAUTOGRANDPARENT", fix_selfdir (sdir_greatgrandparent)); |
| 671 |
|
| 672 |
#if defined(WIN32) || defined(__CYGWIN__) |
| 673 |
mk_suffixlist(kpse); |
| 674 |
#endif /* WIN32 || __CYGWIN__ */ |
| 675 |
|
| 676 |
free (sdir); |
| 677 |
free (sdir_parent); |
| 678 |
free (sdir_grandparent); |
| 679 |
free (sdir_greatgrandparent); |
| 680 |
|
| 681 |
kpse->invocation_short_name |
| 682 |
= xstrdup (xbasename (kpse->invocation_name)); |
| 683 |
|
| 684 |
if (progname) { |
| 685 |
kpse->program_name = xstrdup (progname); |
| 686 |
} else { |
| 687 |
/* If configured --enable-shared and running from the build directory |
| 688 |
with the wrapper scripts (e.g., for make check), the binaries will |
| 689 |
be named foo.exe instead of foo. Or possibly if we're running on a |
| 690 |
DOSISH system. */ |
| 691 |
ext = find_suffix (kpse->invocation_short_name); |
| 692 |
if (ext && FILESTRCASEEQ (ext, "exe")) { |
| 693 |
kpse->program_name = remove_suffix (kpse->invocation_short_name); |
| 694 |
} else { |
| 695 |
kpse->program_name = xstrdup (kpse->invocation_short_name); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
/* Runtime check that snprintf always writes a trailing NUL byte. */ |
| 700 |
{ |
| 701 |
char buf[4] = "old"; |
| 702 |
assert (snprintf (buf, 2, "a") == 1 && buf[1] == '\0'); |
| 703 |
assert ((unsigned)snprintf (buf, 2, "ab") >= 2 && buf[1] == '\0'); |
| 704 |
assert ((unsigned)snprintf (buf, 2, "abc") >= 2 && buf[1] == '\0'); |
| 705 |
} |
| 706 |
/* Some of the utility routines (like atou() and xfopen()) will use |
| 707 |
FATAL and variations thereof (see lib.h) if there is a problem. |
| 708 |
|
| 709 |
The next trick makes it possible for that message to report some useful |
| 710 |
name instead of (NULL), if the backward compatible is compiled in. */ |
| 711 |
|
| 712 |
#if defined (KPSE_COMPAT_API) |
| 713 |
if (kpse!=kpse_def) { |
| 714 |
kpse_def->invocation_name = xstrdup(kpse->invocation_name); |
| 715 |
kpse_def->invocation_short_name = xstrdup(kpse->invocation_short_name); |
| 716 |
} |
| 717 |
#endif |
| 718 |
|
| 719 |
kpathsea_xputenv (kpse, "progname", kpse->program_name); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
#if defined (KPSE_COMPAT_API) |
| 724 |
void |
| 725 |
kpse_set_program_name (const_string argv0, const_string progname) |
| 726 |
{ |
| 727 |
kpathsea_set_program_name (kpse_def, argv0, progname); |
| 728 |
} |
| 729 |
#endif |
| 730 |
|
| 731 |
/* Returns ARGV0 with any leading path and on some systems the suffix |
| 732 |
for executables stripped off. This returns a new string. |
| 733 |
For example, `kpse_program_basename ("/foo/bar.EXE")' returns "bar" |
| 734 |
on WIndows or Cygwin and "bar.EXE" otherwise. */ |
| 735 |
|
| 736 |
string |
| 737 |
kpse_program_basename (const_string argv0) |
| 738 |
{ |
| 739 |
string base = xstrdup (xbasename (argv0)); |
| 740 |
#ifdef EXEEXT |
| 741 |
string dot = strrchr (base, '.'); |
| 742 |
if (dot && FILESTRCASEEQ (dot, EXEEXT)) |
| 743 |
*dot = 0; |
| 744 |
#endif |
| 745 |
return base; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
#ifdef TEST |
| 750 |
static const char *tab[] = { |
| 751 |
/* 'normal' names */ |
| 752 |
"/w/kpathsea", |
| 753 |
"/w//kpathsea", |
| 754 |
"/w/./kpathsea", |
| 755 |
".", |
| 756 |
"./", |
| 757 |
"./.", |
| 758 |
"../kpathsea", |
| 759 |
"/kpathsea/../foo", |
| 760 |
"/../w/kpathsea", |
| 761 |
"/../w/kpathsea/.", |
| 762 |
"/te/share/texmf/../../../../bin/gnu", |
| 763 |
NULL |
| 764 |
}; |
| 765 |
|
| 766 |
int |
| 767 |
main (int argc, char **argv) |
| 768 |
{ |
| 769 |
const char **p; |
| 770 |
kpathsea kpse = xcalloc(1, sizeof(kpathsea_instance)); |
| 771 |
|
| 772 |
kpathsea_set_program_name(kpse, argv[0], NULL); |
| 773 |
|
| 774 |
#if defined(WIN32) |
| 775 |
printf("\n%s: Nothing to do for WIN32\n", |
| 776 |
kpse->invocation_short_name); |
| 777 |
#else |
| 778 |
printf("\n%s: name -> remove_dots(name)\n\n", |
| 779 |
kpse->invocation_short_name); |
| 780 |
|
| 781 |
for (p = tab; *p; p++) { |
| 782 |
char *q = xstrdup(*p); |
| 783 |
char *s = remove_dots(kpse, q); |
| 784 |
|
| 785 |
printf("%s -> %s\n", q, s); |
| 786 |
free (q); |
| 787 |
free (s); |
| 788 |
} |
| 789 |
#endif |
| 790 |
|
| 791 |
return 0; |
| 792 |
} |
| 793 |
#endif /* TEST */ |