annotate lib/progreloc.c @ 14384:588ed4cdea87

Fix misindentation of preprocessor directives. * lib/argp-namefrob.h: Reindent preprocessor directives. * lib/getopt_int.h (struct _getopt_data): Likewise. * lib/progreloc.c (maybe_executable, find_executable): Likewise. * lib/vasnprintf.c (decode_long_double): Likewise. * tests/test-argmatch.c: Insert blank lines, for clarity. * tests/test-exclude.c: Likewise.
author Bruno Haible <bruno@clisp.org>
date Wed, 23 Feb 2011 10:58:06 +0100
parents 97fc9a21a8fb
children 5e10178f1466
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Provide relocatable programs.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13667
diff changeset
2 Copyright (C) 2003-2011 Free Software Foundation, Inc.
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8272
diff changeset
5 This program is free software: you can redistribute it and/or modify
5270
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8272
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8272
diff changeset
8 (at your option) any later version.
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
5270
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
13 GNU General Public License for more details.
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
5270
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8272
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7023
diff changeset
19 #include <config.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "progname.h"
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdbool.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdio.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdlib.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <string.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <fcntl.h>
8272
fd48e35f4e5e New modules relocatable, relocatable-lib, relocatable-script.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
29 #include <unistd.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <sys/stat.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
32 /* Get declaration of _NSGetExecutablePath on MacOS X 10.2 or newer. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
33 #if HAVE_MACH_O_DYLD_H
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
34 # include <mach-o/dyld.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
37 #if defined _WIN32 || defined __WIN32__
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
38 # define WIN32_NATIVE
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
39 #endif
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
40
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
41 #if defined WIN32_NATIVE || defined __CYGWIN__
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 # define WIN32_LEAN_AND_MEAN
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 # include <windows.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 #include "relocatable.h"
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #ifdef NO_XMALLOC
9547
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
49 # include "areadlink.h"
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
50 # define xreadlink areadlink
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
51 #else
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
52 # include "xreadlink.h"
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
53 #endif
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
54
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
55 #ifdef NO_XMALLOC
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 # define xmalloc malloc
5583
5c8e3fc4efe4 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 5270
diff changeset
57 # define xstrdup strdup
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 #else
5583
5c8e3fc4efe4 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 5270
diff changeset
59 # include "xalloc.h"
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
13667
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
62 #ifndef O_EXEC
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
63 # define O_EXEC O_RDONLY /* This is often close enough in older systems. */
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
64 #endif
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
65
12049
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
66 /* Declare canonicalize_file_name.
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
67 The <stdlib.h> included above may be the system's one, not the gnulib
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
68 one. */
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
69 extern char * canonicalize_file_name (const char *name);
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
70
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 /* Pathname support.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 ISSLASH(C) tests whether C is a directory separator character.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 */
5053
eebff8c51a9b Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents: 4691
diff changeset
75 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
eebff8c51a9b Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents: 4691
diff changeset
76 /* Win32, Cygwin, OS/2, DOS */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 # define HAS_DEVICE(P) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 && (P)[1] == ':')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 # define IS_PATH_WITH_DIR(P) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
5164
773a21466082 Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents: 5053
diff changeset
83 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 #else
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 /* Unix */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 # define ISSLASH(C) ((C) == '/')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
5164
773a21466082 Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents: 5053
diff changeset
88 # define FILE_SYSTEM_PREFIX_LEN(P) 0
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90
7863
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
91 /* The results of open() in this file are not used with fchdir,
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
92 therefore save some unnecessary work in fchdir.c. */
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
93 #undef open
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
94 #undef close
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
95
13397
758ef73803db Avoid relocwrapper link errors due to gnulib replacement functions.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
96 /* Use the system functions, not the gnulib overrides in this file. */
758ef73803db Avoid relocwrapper link errors due to gnulib replacement functions.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
97 #undef malloc
758ef73803db Avoid relocwrapper link errors due to gnulib replacement functions.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
98 #undef sprintf
758ef73803db Avoid relocwrapper link errors due to gnulib replacement functions.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
99
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 #undef set_program_name
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 #if ENABLE_RELOCATABLE
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 #ifdef __linux__
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 /* File descriptor of the executable.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 (Only used to verify that we find the correct executable.) */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 static int executable_fd = -1;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 /* Tests whether a given pathname may belong to the executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 static bool
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 maybe_executable (const char *filename)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 {
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
115 /* Woe32 lacks the access() function, but Cygwin doesn't. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
116 #if !(defined WIN32_NATIVE && !defined __CYGWIN__)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 if (access (filename, X_OK) < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 return false;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
120 # ifdef __linux__
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 if (executable_fd >= 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 /* If we already have an executable_fd, check that filename points to
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
124 the same inode. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 struct stat statexe;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 struct stat statfile;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 if (fstat (executable_fd, &statexe) >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
129 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
130 if (stat (filename, &statfile) < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
131 return false;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
132 if (!(statfile.st_dev
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
133 && statfile.st_dev == statexe.st_dev
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
134 && statfile.st_ino == statexe.st_ino))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
135 return false;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
136 }
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 }
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
138 # endif
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 return true;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 /* Determine the full pathname of the current executable, freshly allocated.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 Return NULL if unknown.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 Guaranteed to work on Linux and Woe32. Likely to work on the other
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 Unixes (maybe except BeOS), under most conditions. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 static char *
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 find_executable (const char *argv0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 {
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
151 #if defined WIN32_NATIVE || defined __CYGWIN__
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
152 char location[MAX_PATH];
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
153 int length = GetModuleFileName (NULL, location, sizeof (location));
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 if (length < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 return NULL;
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
156 if (!IS_PATH_WITH_DIR (location))
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 /* Shouldn't happen. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 return NULL;
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
159 {
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
160 # if defined __CYGWIN__
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
161 /* cygwin-1.5.13 (2005-03-01) or newer would also allow a Linux-like
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
162 implementation: readlink of "/proc/self/exe". But using the
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
163 result of the Win32 system call is simpler and is consistent with the
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
164 code in relocatable.c. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
165 /* On Cygwin, we need to convert paths coming from Win32 system calls
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
166 to the Unix-like slashified notation. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
167 static char location_as_posix_path[2 * MAX_PATH];
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
168 /* There's no error return defined for cygwin_conv_to_posix_path.
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
169 See cygwin-api/func-cygwin-conv-to-posix-path.html.
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
170 Does it overflow the buffer of expected size MAX_PATH or does it
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
171 truncate the path? I don't know. Let's catch both. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
172 cygwin_conv_to_posix_path (location, location_as_posix_path);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
173 location_as_posix_path[MAX_PATH - 1] = '\0';
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
174 if (strlen (location_as_posix_path) >= MAX_PATH - 1)
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
175 /* A sign of buffer overflow or path truncation. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
176 return NULL;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
177 /* Call canonicalize_file_name, because Cygwin supports symbolic links. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
178 return canonicalize_file_name (location_as_posix_path);
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
179 # else
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
180 return xstrdup (location);
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
181 # endif
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
182 }
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
183 #else /* Unix && !Cygwin */
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
184 # ifdef __linux__
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 /* The executable is accessible as /proc/<pid>/exe. In newer Linux
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 to the true pathname; older Linux versions give only device and ino,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 enclosed in brackets, which we cannot use here. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 char *link;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 link = xreadlink ("/proc/self/exe");
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 if (link != NULL && link[0] != '[')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 return link;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 if (executable_fd < 0)
13664
241057e2e60f fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines
Paul Eggert <eggert@cs.ucla.edu>
parents: 13397
diff changeset
196 executable_fd = open ("/proc/self/exe", O_EXEC, 0);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 char buf[6+10+5];
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 sprintf (buf, "/proc/%d/exe", getpid ());
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 link = xreadlink (buf);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 if (link != NULL && link[0] != '[')
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
203 return link;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 if (executable_fd < 0)
13664
241057e2e60f fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines
Paul Eggert <eggert@cs.ucla.edu>
parents: 13397
diff changeset
205 executable_fd = open (buf, O_EXEC, 0);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 }
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
208 # endif
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
209 # if HAVE_MACH_O_DYLD_H && HAVE__NSGETEXECUTABLEPATH
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
210 /* On MacOS X 10.2 or newer, the function
11025
6d85e95ca18f Update use of API that has changed in MacOS X 10.4.
Bruno Haible <bruno@clisp.org>
parents: 9576
diff changeset
211 int _NSGetExecutablePath (char *buf, uint32_t *bufsize);
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
212 can be used to retrieve the executable's full path. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
213 char location[4096];
11025
6d85e95ca18f Update use of API that has changed in MacOS X 10.4.
Bruno Haible <bruno@clisp.org>
parents: 9576
diff changeset
214 unsigned int length = sizeof (location);
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
215 if (_NSGetExecutablePath (location, &length) == 0
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
216 && location[0] == '/')
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
217 return canonicalize_file_name (location);
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
218 # endif
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 /* Guess the executable's full path. We assume the executable has been
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 called via execlp() or execvp() with properly set up argv[0]. The
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 login(1) convention to add a '-' prefix to argv[0] is not supported. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 bool has_slash = false;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 const char *p;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 for (p = argv0; *p; p++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
227 if (*p == '/')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
228 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
229 has_slash = true;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
230 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
231 }
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 if (!has_slash)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
235 /* exec searches paths without slashes in the directory list given
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
236 by $PATH. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
237 const char *path = getenv ("PATH");
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
239 if (path != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
240 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
241 const char *p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
242 const char *p_next;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
244 for (p = path; *p; p = p_next)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
245 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
246 const char *q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
247 size_t p_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
248 char *concat_name;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
250 for (q = p; *q; q++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
251 if (*q == ':')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
252 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
253 p_len = q - p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
254 p_next = (*q == '\0' ? q : q + 1);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
256 /* We have a path item at p, of length p_len.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
257 Now concatenate the path item and argv0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
258 concat_name = (char *) xmalloc (p_len + strlen (argv0) + 2);
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
259 # ifdef NO_XMALLOC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
260 if (concat_name == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
261 return NULL;
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
262 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
263 if (p_len == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
264 /* An empty PATH element designates the current directory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
265 strcpy (concat_name, argv0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
266 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
267 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
268 memcpy (concat_name, p, p_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
269 concat_name[p_len] = '/';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
270 strcpy (concat_name + p_len + 1, argv0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
271 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
272 if (maybe_executable (concat_name))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
273 return canonicalize_file_name (concat_name);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
274 free (concat_name);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
275 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
276 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
277 /* Not found in the PATH, assume the current directory. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 /* exec treats paths containing slashes as relative to the current
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 directory. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 if (maybe_executable (argv0))
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 return canonicalize_file_name (argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 /* No way to find the executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 return NULL;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 /* Full pathname of executable, or NULL. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 static char *executable_fullname;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 static void
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 prepare_relocate (const char *orig_installprefix, const char *orig_installdir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
294 const char *argv0)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 {
9576
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
296 char *curr_prefix;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 /* Determine the full pathname of the current executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 executable_fullname = find_executable (argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 /* Determine the current installation prefix from it. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 curr_prefix = compute_curr_prefix (orig_installprefix, orig_installdir,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
303 executable_fullname);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 if (curr_prefix != NULL)
9576
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
305 {
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
306 /* Now pass this prefix to all copies of the relocate.c source file. */
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
307 set_relocation_prefix (orig_installprefix, curr_prefix);
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
308
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
309 free (curr_prefix);
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
310 }
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 /* Set program_name, based on argv[0], and original installation prefix and
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 directory, for relocatability. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 void
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 set_program_name_and_installdir (const char *argv0,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
317 const char *orig_installprefix,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
318 const char *orig_installdir)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 const char *argv0_stripped = argv0;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
322 /* Relocatable programs are renamed to .bin by install-reloc. Or, more
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
323 generally, their suffix is changed from $exeext to .bin$exeext.
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
324 Remove the ".bin" here. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 size_t argv0_len = strlen (argv0);
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
327 const size_t exeext_len = sizeof (EXEEXT) - sizeof ("");
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
328 if (argv0_len > 4 + exeext_len)
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
329 if (memcmp (argv0 + argv0_len - exeext_len - 4, ".bin", 4) == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
330 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
331 if (sizeof (EXEEXT) > sizeof (""))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
332 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
333 /* Compare using an inlined copy of c_strncasecmp(), because
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
334 the filenames may have undergone a case conversion since
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
335 they were packaged. In other words, EXEEXT may be ".exe"
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
336 on one system and ".EXE" on another. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
337 static const char exeext[] = EXEEXT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
338 const char *s1 = argv0 + argv0_len - exeext_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
339 const char *s2 = exeext;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
340 for (; *s1 != '\0'; s1++, s2++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
341 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
342 unsigned char c1 = *s1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
343 unsigned char c2 = *s2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
344 if ((c1 >= 'A' && c1 <= 'Z' ? c1 - 'A' + 'a' : c1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
345 != (c2 >= 'A' && c2 <= 'Z' ? c2 - 'A' + 'a' : c2))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
346 goto done_stripping;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
347 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
348 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
349 /* Remove ".bin" before EXEEXT or its equivalent. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
350 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
351 char *shorter = (char *) xmalloc (argv0_len - 4 + 1);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 #ifdef NO_XMALLOC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
353 if (shorter != NULL)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
355 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
356 memcpy (shorter, argv0, argv0_len - exeext_len - 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
357 if (sizeof (EXEEXT) > sizeof (""))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
358 memcpy (shorter + argv0_len - exeext_len - 4,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
359 argv0 + argv0_len - exeext_len - 4,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
360 exeext_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
361 shorter[argv0_len - 4] = '\0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
362 argv0_stripped = shorter;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
363 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
364 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
365 done_stripping: ;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 set_program_name (argv0_stripped);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 prepare_relocate (orig_installprefix, orig_installdir, argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 /* Return the full pathname of the current executable, based on the earlier
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 call to set_program_name_and_installdir. Return NULL if unknown. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 char *
4691
ce37d22a271f Remove K&R cruft.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4603
diff changeset
377 get_full_program_name (void)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 return executable_fullname;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 #endif