annotate lib/progreloc.c @ 7304:1c4ed7637c24

Include <config.h> unconditionally.
author Bruno Haible <bruno@clisp.org>
date Thu, 14 Sep 2006 14:18:36 +0000
parents d78abd0c0b70
children cb2590895141
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.
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
2 Copyright (C) 2003-2006 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
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
5 This program is free software; you can redistribute it and/or modify
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
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
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
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5583
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7023
diff changeset
20 #include <config.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* Specification. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include "progname.h"
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdbool.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdio.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <stdlib.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <string.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <fcntl.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #if HAVE_UNISTD_H
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 # include <unistd.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #include <sys/stat.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
35 /* 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
36 #if HAVE_MACH_O_DYLD_H
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
37 # include <mach-o/dyld.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
40 #if defined _WIN32 || defined __WIN32__
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
41 # define WIN32_NATIVE
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
42 #endif
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
43
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
44 #if defined WIN32_NATIVE || defined __CYGWIN__
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 # define WIN32_LEAN_AND_MEAN
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 # include <windows.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 #include "xreadlink.h"
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #include "canonicalize.h"
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #include "relocatable.h"
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 #ifdef NO_XMALLOC
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 # define xmalloc malloc
5583
5c8e3fc4efe4 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 5270
diff changeset
55 # define xstrdup strdup
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 #else
5583
5c8e3fc4efe4 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 5270
diff changeset
57 # include "xalloc.h"
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* Pathname support.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 ISSLASH(C) tests whether C is a directory separator character.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 */
5053
eebff8c51a9b Treat Cygwin like Windows regarding pathname syntax.
Bruno Haible <bruno@clisp.org>
parents: 4691
diff changeset
64 #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
65 /* Win32, Cygwin, OS/2, DOS */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 # define HAS_DEVICE(P) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 && (P)[1] == ':')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 # define IS_PATH_WITH_DIR(P) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
5164
773a21466082 Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents: 5053
diff changeset
72 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 #else
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 /* Unix */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 # define ISSLASH(C) ((C) == '/')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
5164
773a21466082 Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents: 5053
diff changeset
77 # define FILE_SYSTEM_PREFIX_LEN(P) 0
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 #undef set_program_name
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 #if ENABLE_RELOCATABLE
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 #ifdef __linux__
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 /* File descriptor of the executable.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 (Only used to verify that we find the correct executable.) */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 static int executable_fd = -1;
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
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 /* Tests whether a given pathname may belong to the executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 static bool
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 maybe_executable (const char *filename)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 {
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
95 /* 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
96 #if !(defined WIN32_NATIVE && !defined __CYGWIN__)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 if (access (filename, X_OK) < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 return false;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 #ifdef __linux__
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 if (executable_fd >= 0)
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 we already have an executable_fd, check that filename points to
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 the same inode. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 struct stat statexe;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 struct stat statfile;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 if (fstat (executable_fd, &statexe) >= 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 if (stat (filename, &statfile) < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 return false;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 if (!(statfile.st_dev
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 && statfile.st_dev == statexe.st_dev
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 && statfile.st_ino == statexe.st_ino))
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 return false;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 return true;
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
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 /* Determine the full pathname of the current executable, freshly allocated.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 Return NULL if unknown.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 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
127 Unixes (maybe except BeOS), under most conditions. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 static char *
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 find_executable (const char *argv0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 {
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
131 #if defined WIN32_NATIVE || defined __CYGWIN__
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
132 char location[MAX_PATH];
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
133 int length = GetModuleFileName (NULL, location, sizeof (location));
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 if (length < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 return NULL;
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
136 if (!IS_PATH_WITH_DIR (location))
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 /* Shouldn't happen. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 return NULL;
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
139 {
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
140 #if defined __CYGWIN__
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
141 /* 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
142 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
143 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
144 code in relocatable.c. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
145 /* 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
146 to the Unix-like slashified notation. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
147 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
148 /* 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
149 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
150 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
151 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
152 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
153 location_as_posix_path[MAX_PATH - 1] = '\0';
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
154 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
155 /* A sign of buffer overflow or path truncation. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
156 return NULL;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
157 /* 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
158 return canonicalize_file_name (location_as_posix_path);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
159 #else
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
160 return xstrdup (location);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
161 #endif
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
162 }
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
163 #else /* Unix && !Cygwin */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 #ifdef __linux__
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 /* The executable is accessible as /proc/<pid>/exe. In newer Linux
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 to the true pathname; older Linux versions give only device and ino,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 enclosed in brackets, which we cannot use here. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 char *link;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 link = xreadlink ("/proc/self/exe");
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 if (link != NULL && link[0] != '[')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 return link;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 if (executable_fd < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 executable_fd = open ("/proc/self/exe", O_RDONLY, 0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 char buf[6+10+5];
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 sprintf (buf, "/proc/%d/exe", getpid ());
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 link = xreadlink (buf);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 if (link != NULL && link[0] != '[')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 return link;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 if (executable_fd < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 executable_fd = open (buf, O_RDONLY, 0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 #endif
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
189 #if HAVE_MACH_O_DYLD_H && HAVE__NSGETEXECUTABLEPATH
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
190 /* On MacOS X 10.2 or newer, the function
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
191 int _NSGetExecutablePath (char *buf, unsigned long *bufsize);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
192 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
193 char location[4096];
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
194 unsigned long length = sizeof (location);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
195 if (_NSGetExecutablePath (location, &length) == 0
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
196 && location[0] == '/')
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
197 return canonicalize_file_name (location);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
198 #endif
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 /* Guess the executable's full path. We assume the executable has been
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 called via execlp() or execvp() with properly set up argv[0]. The
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 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
202 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 bool has_slash = false;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 const char *p;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 for (p = argv0; *p; p++)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 if (*p == '/')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 has_slash = true;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 break;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 if (!has_slash)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 /* exec searches paths without slashes in the directory list given
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 by $PATH. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 const char *path = getenv ("PATH");
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 if (path != NULL)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 const char *p;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 const char *p_next;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 for (p = path; *p; p = p_next)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 const char *q;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 size_t p_len;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 char *concat_name;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 for (q = p; *q; q++)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 if (*q == ':')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 break;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 p_len = q - p;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 p_next = (*q == '\0' ? q : q + 1);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 /* We have a path item at p, of length p_len.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 Now concatenate the path item and argv0. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 concat_name = (char *) xmalloc (p_len + strlen (argv0) + 2);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 #ifdef NO_XMALLOC
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 if (concat_name == NULL)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 return NULL;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 if (p_len == 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 /* An empty PATH element designates the current directory. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 strcpy (concat_name, argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 else
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 memcpy (concat_name, p, p_len);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 concat_name[p_len] = '/';
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 strcpy (concat_name + p_len + 1, argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 if (maybe_executable (concat_name))
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 return canonicalize_file_name (concat_name);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 free (concat_name);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 /* Not found in the PATH, assume the current directory. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 /* exec treats paths containing slashes as relative to the current
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 directory. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 if (maybe_executable (argv0))
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 return canonicalize_file_name (argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 /* No way to find the executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 return NULL;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 /* Full pathname of executable, or NULL. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 static char *executable_fullname;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 static void
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 prepare_relocate (const char *orig_installprefix, const char *orig_installdir,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 const char *argv0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 const char *curr_prefix;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 /* Determine the full pathname of the current executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 executable_fullname = find_executable (argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 /* Determine the current installation prefix from it. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 curr_prefix = compute_curr_prefix (orig_installprefix, orig_installdir,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 executable_fullname);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 if (curr_prefix != NULL)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 /* Now pass this prefix to all copies of the relocate.c source file. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 set_relocation_prefix (orig_installprefix, curr_prefix);
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 /* Set program_name, based on argv[0], and original installation prefix and
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 directory, for relocatability. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 void
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 set_program_name_and_installdir (const char *argv0,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 const char *orig_installprefix,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 const char *orig_installdir)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 const char *argv0_stripped = argv0;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
298 /* 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
299 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
300 Remove the ".bin" here. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 size_t argv0_len = strlen (argv0);
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
303 const size_t exeext_len = sizeof (EXEEXT) - sizeof ("");
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
304 if (argv0_len > 4 + exeext_len)
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
305 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
306 {
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
307 if (sizeof (EXEEXT) > sizeof (""))
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
308 {
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
309 /* 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
310 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
311 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
312 on one system and ".EXE" on another. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
313 static const char exeext[] = EXEEXT;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
314 const char *s1 = argv0 + argv0_len - exeext_len;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
315 const char *s2 = exeext;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
316 for (; *s1 != '\0'; s1++, s2++)
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 unsigned char c1 = *s1;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
319 unsigned char c2 = *s2;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
320 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
321 != (c2 >= 'A' && c2 <= 'Z' ? c2 - 'A' + 'a' : c2))
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
322 goto done_stripping;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
323 }
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
324 }
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
325 /* Remove ".bin" before EXEEXT or its equivalent. */
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
326 {
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
327 char *shorter = (char *) xmalloc (argv0_len - 4 + 1);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 #ifdef NO_XMALLOC
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
329 if (shorter != NULL)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 #endif
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
331 {
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
332 memcpy (shorter, argv0, argv0_len - exeext_len - 4);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
333 if (sizeof (EXEEXT) > sizeof (""))
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
334 memcpy (shorter + argv0_len - exeext_len - 4,
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
335 argv0 + argv0_len - exeext_len - 4,
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
336 exeext_len);
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
337 shorter[argv0_len - 4] = '\0';
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
338 argv0_stripped = shorter;
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
339 }
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 }
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
341 done_stripping: ;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 set_program_name (argv0_stripped);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 prepare_relocate (orig_installprefix, orig_installdir, argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 /* Return the full pathname of the current executable, based on the earlier
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 call to set_program_name_and_installdir. Return NULL if unknown. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 char *
4691
ce37d22a271f Remove K&R cruft.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4603
diff changeset
353 get_full_program_name (void)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 return executable_fullname;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 #endif