Mercurial > gnulib
annotate lib/progreloc.c @ 12421:e8d2c6fc33ad
Use spaces for indentation, not tabs.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 10 Dec 2009 20:28:30 +0100 |
parents | 22a617bea642 |
children | c2cbabec01dd |
rev | line source |
---|---|
4603 | 1 /* Provide relocatable programs. |
11025
6d85e95ca18f
Update use of API that has changed in MacOS X 10.4.
Bruno Haible <bruno@clisp.org>
parents:
9576
diff
changeset
|
2 Copyright (C) 2003-2009 Free Software Foundation, Inc. |
4603 | 3 Written by Bruno Haible <bruno@clisp.org>, 2003. |
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 | 9 |
10 This program is distributed in the hope that it will be useful, | |
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 | 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 | 17 |
18 | |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
7023
diff
changeset
|
19 #include <config.h> |
4603 | 20 |
21 /* Specification. */ | |
22 #include "progname.h" | |
23 | |
24 #include <stdbool.h> | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
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 | 30 #include <sys/stat.h> |
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 | 35 #endif |
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 | 42 # define WIN32_LEAN_AND_MEAN |
43 # include <windows.h> | |
44 #endif | |
45 | |
46 #include "relocatable.h" | |
47 | |
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 | 56 # define xmalloc malloc |
5583 | 57 # define xstrdup strdup |
4603 | 58 #else |
5583 | 59 # include "xalloc.h" |
4603 | 60 #endif |
61 | |
12049
22a617bea642
Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents:
12034
diff
changeset
|
62 /* Declare canonicalize_file_name. |
22a617bea642
Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents:
12034
diff
changeset
|
63 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
|
64 one. */ |
22a617bea642
Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents:
12034
diff
changeset
|
65 extern char * canonicalize_file_name (const char *name); |
22a617bea642
Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents:
12034
diff
changeset
|
66 |
4603 | 67 /* Pathname support. |
68 ISSLASH(C) tests whether C is a directory separator character. | |
69 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification. | |
70 */ | |
5053
eebff8c51a9b
Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents:
4691
diff
changeset
|
71 #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
|
72 /* Win32, Cygwin, OS/2, DOS */ |
4603 | 73 # define ISSLASH(C) ((C) == '/' || (C) == '\\') |
74 # define HAS_DEVICE(P) \ | |
75 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \ | |
76 && (P)[1] == ':') | |
77 # define IS_PATH_WITH_DIR(P) \ | |
78 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P)) | |
5164
773a21466082
Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents:
5053
diff
changeset
|
79 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0) |
4603 | 80 #else |
81 /* Unix */ | |
82 # define ISSLASH(C) ((C) == '/') | |
83 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL) | |
5164
773a21466082
Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents:
5053
diff
changeset
|
84 # define FILE_SYSTEM_PREFIX_LEN(P) 0 |
4603 | 85 #endif |
86 | |
7863 | 87 /* The results of open() in this file are not used with fchdir, |
88 therefore save some unnecessary work in fchdir.c. */ | |
89 #undef open | |
90 #undef close | |
91 | |
4603 | 92 #undef set_program_name |
93 | |
94 | |
95 #if ENABLE_RELOCATABLE | |
96 | |
97 #ifdef __linux__ | |
98 /* File descriptor of the executable. | |
99 (Only used to verify that we find the correct executable.) */ | |
100 static int executable_fd = -1; | |
101 #endif | |
102 | |
103 /* Tests whether a given pathname may belong to the executable. */ | |
104 static bool | |
105 maybe_executable (const char *filename) | |
106 { | |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
107 /* 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
|
108 #if !(defined WIN32_NATIVE && !defined __CYGWIN__) |
4603 | 109 if (access (filename, X_OK) < 0) |
110 return false; | |
111 | |
112 #ifdef __linux__ | |
113 if (executable_fd >= 0) | |
114 { | |
115 /* 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
|
116 the same inode. */ |
4603 | 117 struct stat statexe; |
118 struct stat statfile; | |
119 | |
120 if (fstat (executable_fd, &statexe) >= 0) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
121 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
122 if (stat (filename, &statfile) < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
123 return false; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
124 if (!(statfile.st_dev |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
125 && statfile.st_dev == statexe.st_dev |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
126 && statfile.st_ino == statexe.st_ino)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
127 return false; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
128 } |
4603 | 129 } |
130 #endif | |
131 #endif | |
132 | |
133 return true; | |
134 } | |
135 | |
136 /* Determine the full pathname of the current executable, freshly allocated. | |
137 Return NULL if unknown. | |
138 Guaranteed to work on Linux and Woe32. Likely to work on the other | |
139 Unixes (maybe except BeOS), under most conditions. */ | |
140 static char * | |
141 find_executable (const char *argv0) | |
142 { | |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
143 #if defined WIN32_NATIVE || defined __CYGWIN__ |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
144 char location[MAX_PATH]; |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
145 int length = GetModuleFileName (NULL, location, sizeof (location)); |
4603 | 146 if (length < 0) |
147 return NULL; | |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
148 if (!IS_PATH_WITH_DIR (location)) |
4603 | 149 /* Shouldn't happen. */ |
150 return NULL; | |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
151 { |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
152 #if defined __CYGWIN__ |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
153 /* 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
|
154 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
|
155 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
|
156 code in relocatable.c. */ |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
157 /* 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
|
158 to the Unix-like slashified notation. */ |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
159 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
|
160 /* 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 location_as_posix_path[MAX_PATH - 1] = '\0'; |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
166 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
|
167 /* A sign of buffer overflow or path truncation. */ |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
168 return NULL; |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
169 /* 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
|
170 return canonicalize_file_name (location_as_posix_path); |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
171 #else |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
172 return xstrdup (location); |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
173 #endif |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
174 } |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
175 #else /* Unix && !Cygwin */ |
4603 | 176 #ifdef __linux__ |
177 /* The executable is accessible as /proc/<pid>/exe. In newer Linux | |
178 versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink | |
179 to the true pathname; older Linux versions give only device and ino, | |
180 enclosed in brackets, which we cannot use here. */ | |
181 { | |
182 char *link; | |
183 | |
184 link = xreadlink ("/proc/self/exe"); | |
185 if (link != NULL && link[0] != '[') | |
186 return link; | |
187 if (executable_fd < 0) | |
188 executable_fd = open ("/proc/self/exe", O_RDONLY, 0); | |
189 | |
190 { | |
191 char buf[6+10+5]; | |
192 sprintf (buf, "/proc/%d/exe", getpid ()); | |
193 link = xreadlink (buf); | |
194 if (link != NULL && link[0] != '[') | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
195 return link; |
4603 | 196 if (executable_fd < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
197 executable_fd = open (buf, O_RDONLY, 0); |
4603 | 198 } |
199 } | |
200 #endif | |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
201 #if HAVE_MACH_O_DYLD_H && HAVE__NSGETEXECUTABLEPATH |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
202 /* 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
|
203 int _NSGetExecutablePath (char *buf, uint32_t *bufsize); |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
204 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
|
205 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
|
206 unsigned int length = sizeof (location); |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
207 if (_NSGetExecutablePath (location, &length) == 0 |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
208 && location[0] == '/') |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
209 return canonicalize_file_name (location); |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
210 #endif |
4603 | 211 /* Guess the executable's full path. We assume the executable has been |
212 called via execlp() or execvp() with properly set up argv[0]. The | |
213 login(1) convention to add a '-' prefix to argv[0] is not supported. */ | |
214 { | |
215 bool has_slash = false; | |
216 { | |
217 const char *p; | |
218 for (p = argv0; *p; p++) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
219 if (*p == '/') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
220 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
221 has_slash = true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
222 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
223 } |
4603 | 224 } |
225 if (!has_slash) | |
226 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
227 /* 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
|
228 by $PATH. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
229 const char *path = getenv ("PATH"); |
4603 | 230 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
231 if (path != NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
232 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
233 const char *p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
234 const char *p_next; |
4603 | 235 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
236 for (p = path; *p; p = p_next) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
237 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
238 const char *q; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
239 size_t p_len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
240 char *concat_name; |
4603 | 241 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
242 for (q = p; *q; q++) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
243 if (*q == ':') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
244 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
245 p_len = q - p; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
246 p_next = (*q == '\0' ? q : q + 1); |
4603 | 247 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
248 /* 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
|
249 Now concatenate the path item and argv0. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
250 concat_name = (char *) xmalloc (p_len + strlen (argv0) + 2); |
4603 | 251 #ifdef NO_XMALLOC |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
252 if (concat_name == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
253 return NULL; |
4603 | 254 #endif |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
255 if (p_len == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
256 /* An empty PATH element designates the current directory. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
257 strcpy (concat_name, argv0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
258 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
259 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
260 memcpy (concat_name, p, p_len); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
261 concat_name[p_len] = '/'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
262 strcpy (concat_name + p_len + 1, argv0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
263 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
264 if (maybe_executable (concat_name)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
265 return canonicalize_file_name (concat_name); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
266 free (concat_name); |
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 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
269 /* Not found in the PATH, assume the current directory. */ |
4603 | 270 } |
271 /* exec treats paths containing slashes as relative to the current | |
272 directory. */ | |
273 if (maybe_executable (argv0)) | |
274 return canonicalize_file_name (argv0); | |
275 } | |
276 /* No way to find the executable. */ | |
277 return NULL; | |
278 #endif | |
279 } | |
280 | |
281 /* Full pathname of executable, or NULL. */ | |
282 static char *executable_fullname; | |
283 | |
284 static void | |
285 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
|
286 const char *argv0) |
4603 | 287 { |
9576
8cae100b8cd1
Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents:
9547
diff
changeset
|
288 char *curr_prefix; |
4603 | 289 |
290 /* Determine the full pathname of the current executable. */ | |
291 executable_fullname = find_executable (argv0); | |
292 | |
293 /* Determine the current installation prefix from it. */ | |
294 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
|
295 executable_fullname); |
4603 | 296 if (curr_prefix != NULL) |
9576
8cae100b8cd1
Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents:
9547
diff
changeset
|
297 { |
8cae100b8cd1
Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents:
9547
diff
changeset
|
298 /* 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
|
299 set_relocation_prefix (orig_installprefix, curr_prefix); |
8cae100b8cd1
Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents:
9547
diff
changeset
|
300 |
8cae100b8cd1
Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents:
9547
diff
changeset
|
301 free (curr_prefix); |
8cae100b8cd1
Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents:
9547
diff
changeset
|
302 } |
4603 | 303 } |
304 | |
305 /* Set program_name, based on argv[0], and original installation prefix and | |
306 directory, for relocatability. */ | |
307 void | |
308 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
|
309 const char *orig_installprefix, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
310 const char *orig_installdir) |
4603 | 311 { |
312 const char *argv0_stripped = argv0; | |
313 | |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
314 /* 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
|
315 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
|
316 Remove the ".bin" here. */ |
4603 | 317 { |
318 size_t argv0_len = strlen (argv0); | |
7023
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
319 const size_t exeext_len = sizeof (EXEEXT) - sizeof (""); |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
320 if (argv0_len > 4 + exeext_len) |
d78abd0c0b70
Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
321 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
|
322 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
323 if (sizeof (EXEEXT) > sizeof ("")) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
324 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
325 /* 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
|
326 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
|
327 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
|
328 on one system and ".EXE" on another. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
329 static const char exeext[] = EXEEXT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
330 const char *s1 = argv0 + argv0_len - exeext_len; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
331 const char *s2 = exeext; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
332 for (; *s1 != '\0'; s1++, s2++) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
333 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
334 unsigned char c1 = *s1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
335 unsigned char c2 = *s2; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
336 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
|
337 != (c2 >= 'A' && c2 <= 'Z' ? c2 - 'A' + 'a' : c2)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
338 goto done_stripping; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
339 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
340 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
341 /* Remove ".bin" before EXEEXT or its equivalent. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
342 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
343 char *shorter = (char *) xmalloc (argv0_len - 4 + 1); |
4603 | 344 #ifdef NO_XMALLOC |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
345 if (shorter != NULL) |
4603 | 346 #endif |
12421
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 memcpy (shorter, argv0, argv0_len - exeext_len - 4); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
349 if (sizeof (EXEEXT) > sizeof ("")) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
350 memcpy (shorter + argv0_len - exeext_len - 4, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
351 argv0 + argv0_len - exeext_len - 4, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
352 exeext_len); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
353 shorter[argv0_len - 4] = '\0'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
354 argv0_stripped = shorter; |
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 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12049
diff
changeset
|
357 done_stripping: ; |
4603 | 358 } |
359 } | |
360 | |
361 set_program_name (argv0_stripped); | |
362 | |
363 prepare_relocate (orig_installprefix, orig_installdir, argv0); | |
364 } | |
365 | |
366 /* Return the full pathname of the current executable, based on the earlier | |
367 call to set_program_name_and_installdir. Return NULL if unknown. */ | |
368 char * | |
4691 | 369 get_full_program_name (void) |
4603 | 370 { |
371 return executable_fullname; | |
372 } | |
373 | |
374 #endif |