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