annotate lib/progreloc.c @ 40189:3ba41edecde6

progreloc: Speed up executable lookup on various platforms. * lib/progreloc.c: Include <errno.h>. (safe_read, full_read): New functions. (find_executable): On GNU/kFreeBSD, FreeBSD, DragonFly, NetBSD, Solaris, prefer the information from the /proc file system to a PATH search.
author Bruno Haible <bruno@clisp.org>
date Tue, 19 Feb 2019 21:42:54 +0100
parents e13b9344e430
children
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.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19595
diff changeset
2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8272
diff changeset
5 This program is free software: you can redistribute it and/or modify
5270
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8272
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8272
diff changeset
8 (at your option) any later version.
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
5270
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
13 GNU General Public License for more details.
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
5270
b084a75dc8bc Change copyright notice to GPL. The file is still under LGPL 2.0; this
Bruno Haible <bruno@clisp.org>
parents: 5164
diff changeset
15 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
14541
0f5c066f32ab * lib/progreloc.c (_GL_USE_STDLIB_ALLOC, malloc): Likewise.
Paul Eggert <eggert@cs.ucla.edu>
parents: 14399
diff changeset
19 #define _GL_USE_STDLIB_ALLOC 1
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
40189
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
25 #include <errno.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdbool.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <stdio.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <stdlib.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <string.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <fcntl.h>
8272
fd48e35f4e5e New modules relocatable, relocatable-lib, relocatable-script.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
31 #include <unistd.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include <sys/stat.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
16935
498a2211d839 Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
34 /* Get declaration of _NSGetExecutablePath on Mac OS X 10.2 or newer. */
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
35 #if HAVE_MACH_O_DYLD_H
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
36 # include <mach-o/dyld.h>
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
39 #if defined _WIN32 && !defined __CYGWIN__
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
40 # define WINDOWS_NATIVE
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
41 #endif
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
42
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
43 #ifdef WINDOWS_NATIVE
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 # define WIN32_LEAN_AND_MEAN
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 # include <windows.h>
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
17824
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
48 #ifdef __EMX__
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
49 # define INCL_DOS
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
50 # include <os2.h>
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
51 #endif
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
52
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 #include "relocatable.h"
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 #ifdef NO_XMALLOC
9547
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
56 # include "areadlink.h"
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
57 # define xreadlink areadlink
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
58 #else
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
59 # include "xreadlink.h"
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
60 #endif
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
61
c3d038b0b4d3 Fix link error in relocatability wrappers, due to xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
62 #ifdef NO_XMALLOC
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 # define xmalloc malloc
5583
5c8e3fc4efe4 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 5270
diff changeset
64 # define xstrdup strdup
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 #else
5583
5c8e3fc4efe4 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 5270
diff changeset
66 # include "xalloc.h"
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
13667
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
69 #ifndef O_EXEC
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
70 # define O_EXEC O_RDONLY /* This is often close enough in older systems. */
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
71 #endif
3211d36b9926 relocatable-prog-wrapper: Fix compilation failure due to O_EXEC.
Bruno Haible <bruno@clisp.org>
parents: 13664
diff changeset
72
12049
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
73 /* Declare canonicalize_file_name.
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
74 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
75 one. */
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
76 extern char * canonicalize_file_name (const char *name);
22a617bea642 Declare canonicalize_file_name.
Bruno Haible <bruno@clisp.org>
parents: 12034
diff changeset
77
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 /* Pathname support.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 ISSLASH(C) tests whether C is a directory separator character.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 */
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
82 #if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
83 /* Native Windows, OS/2, DOS */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 # define HAS_DEVICE(P) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 && (P)[1] == ':')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 # define IS_PATH_WITH_DIR(P) \
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
5164
773a21466082 Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents: 5053
diff changeset
90 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 #else
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 /* Unix */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 # define ISSLASH(C) ((C) == '/')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
5164
773a21466082 Rename FILESYSTEM_PREFIX_LEN.
Bruno Haible <bruno@clisp.org>
parents: 5053
diff changeset
95 # define FILE_SYSTEM_PREFIX_LEN(P) 0
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
13397
758ef73803db Avoid relocwrapper link errors due to gnulib replacement functions.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
98 /* Use the system functions, not the gnulib overrides in this file. */
758ef73803db Avoid relocwrapper link errors due to gnulib replacement functions.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
99 #undef sprintf
758ef73803db Avoid relocwrapper link errors due to gnulib replacement functions.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
100
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 #undef set_program_name
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
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 #if ENABLE_RELOCATABLE
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105
40189
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
106 #ifdef __sun
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
107
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
108 /* Helper function, from gnulib module 'safe-read'. */
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
109 static size_t
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
110 safe_read (int fd, void *buf, size_t count)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
111 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
112 for (;;)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
113 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
114 ssize_t result = read (fd, buf, count);
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
115
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
116 if (0 <= result || errno != EINTR)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
117 return result;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
118 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
119 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
120
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
121 /* Helper function, from gnulib module 'full-read'. */
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
122 static size_t
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
123 full_read (int fd, void *buf, size_t count)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
124 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
125 size_t total = 0;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
126 const char *ptr = (const char *) buf;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
127
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
128 while (count > 0)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
129 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
130 size_t n = safe_read (fd, ptr, count);
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
131 if (n == (size_t) -1)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
132 break;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
133 if (n == 0)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
134 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
135 errno = 0;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
136 break;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
137 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
138 total += n;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
139 ptr += n;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
140 count -= n;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
141 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
142
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
143 return total;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
144 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
145
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
146 #endif
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
147
40188
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
148 #if defined __linux__ || defined __CYGWIN__
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 /* File descriptor of the executable.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 (Only used to verify that we find the correct executable.) */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 static int executable_fd = -1;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 /* Tests whether a given pathname may belong to the executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 static bool
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 maybe_executable (const char *filename)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 {
16242
59c686e5b2df Talk about "native Windows API", not "Woe32".
Bruno Haible <bruno@clisp.org>
parents: 16214
diff changeset
158 /* The native Windows API lacks the access() function. */
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
159 #if !defined WINDOWS_NATIVE
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 if (access (filename, X_OK) < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 return false;
14399
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
162 #endif
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163
40188
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
164 #if defined __linux__ || defined __CYGWIN__
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 if (executable_fd >= 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 /* 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
168 the same inode. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 struct stat statexe;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 struct stat statfile;
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 if (fstat (executable_fd, &statexe) >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
173 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
174 if (stat (filename, &statfile) < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
175 return false;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
176 if (!(statfile.st_dev
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
177 && statfile.st_dev == statexe.st_dev
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
178 && statfile.st_ino == statexe.st_ino))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
179 return false;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
180 }
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 return true;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 }
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 /* Determine the full pathname of the current executable, freshly allocated.
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 Return NULL if unknown.
16242
59c686e5b2df Talk about "native Windows API", not "Woe32".
Bruno Haible <bruno@clisp.org>
parents: 16214
diff changeset
189 Guaranteed to work on Linux and native Windows. Likely to work on the
59c686e5b2df Talk about "native Windows API", not "Woe32".
Bruno Haible <bruno@clisp.org>
parents: 16214
diff changeset
190 other Unixes (maybe except BeOS), under most conditions. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 static char *
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 find_executable (const char *argv0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 {
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
194 #if defined WINDOWS_NATIVE
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
195 /* Native Windows only.
14399
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
196 On Cygwin, it is better to use the Cygwin provided /proc interface, than
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
197 to use native Windows API and cygwin_conv_to_posix_path, because it
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
198 supports longer file names
19192
d86e08b1f555 all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
199 (see <https://cygwin.com/ml/cygwin/2011-01/msg00410.html>). */
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
200 char location[MAX_PATH];
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
201 int length = GetModuleFileName (NULL, location, sizeof (location));
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 if (length < 0)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 return NULL;
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
204 if (!IS_PATH_WITH_DIR (location))
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 /* Shouldn't happen. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 return NULL;
14399
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
207 return xstrdup (location);
17824
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
208 #elif defined __EMX__
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
209 PPIB ppib;
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
210 char location[CCHMAXPATH];
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
211
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
212 /* See http://cyberkinetica.homeunix.net/os2tk45/cp1/619_L2H_DosGetInfoBlocksSynt.html
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
213 for specification of DosGetInfoBlocks(). */
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
214 if (DosGetInfoBlocks (NULL, &ppib))
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
215 return NULL;
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
216
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
217 /* See http://cyberkinetica.homeunix.net/os2tk45/cp1/1247_L2H_DosQueryModuleNameSy.html
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
218 for specification of DosQueryModuleName(). */
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
219 if (DosQueryModuleName (ppib->pib_hmte, sizeof (location), location))
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
220 return NULL;
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
221
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
222 _fnslashify (location);
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
223
542f0c385022 find_executable: port to EMX
KO Myung-Hun <komh78@gmail.com>
parents: 17666
diff changeset
224 return xstrdup (location);
14399
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
225 #else /* Unix */
40188
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
226 # if defined __linux__
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 /* The executable is accessible as /proc/<pid>/exe. In newer Linux
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 to the true pathname; older Linux versions give only device and ino,
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 enclosed in brackets, which we cannot use here. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 char *link;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 link = xreadlink ("/proc/self/exe");
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 if (link != NULL && link[0] != '[')
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 return link;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 if (executable_fd < 0)
13664
241057e2e60f fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines
Paul Eggert <eggert@cs.ucla.edu>
parents: 13397
diff changeset
238 executable_fd = open ("/proc/self/exe", O_EXEC, 0);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 char buf[6+10+5];
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 sprintf (buf, "/proc/%d/exe", getpid ());
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 link = xreadlink (buf);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 if (link != NULL && link[0] != '[')
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
245 return link;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 if (executable_fd < 0)
13664
241057e2e60f fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines
Paul Eggert <eggert@cs.ucla.edu>
parents: 13397
diff changeset
247 executable_fd = open (buf, O_EXEC, 0);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 }
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
250 # endif
40189
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
251 # if defined __ANDROID__ || defined __FreeBSD_kernel__
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
252 /* On Android and GNU/kFreeBSD, the executable is accessible as
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
253 /proc/<pid>/exe and /proc/self/exe. */
40188
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
254 {
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
255 char *link;
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
256
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
257 link = xreadlink ("/proc/self/exe");
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
258 if (link != NULL)
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
259 return link;
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
260 }
e13b9344e430 progreloc: Simplify code for Android.
Bruno Haible <bruno@clisp.org>
parents: 40151
diff changeset
261 # endif
40189
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
262 # if defined __FreeBSD__ || defined __DragonFly__
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
263 /* In FreeBSD >= 5.0, the executable is accessible as /proc/<pid>/file and
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
264 /proc/curproc/file. */
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
265 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
266 char *link;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
267
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
268 link = xreadlink ("/proc/curproc/file");
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
269 if (link != NULL)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
270 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
271 if (strcmp (link, "unknown") != 0)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
272 return link;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
273 free (link);
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
274 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
275 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
276 # endif
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
277 # if defined __NetBSD__
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
278 /* In NetBSD >= 4.0, the executable is accessible as /proc/<pid>/exe and
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
279 /proc/curproc/exe. */
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
280 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
281 char *link;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
282
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
283 link = xreadlink ("/proc/curproc/exe");
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
284 if (link != NULL)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
285 return link;
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
286 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
287 # endif
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
288 # if defined __sun
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
289 /* On Solaris >= 11.4, /proc/<pid>/execname and /proc/self/execname contains
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
290 the name of the executable, either as an absolute file name or relative to
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
291 the current directory. */
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
292 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
293 char namebuf[4096];
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
294 int fd = open ("/proc/self/execname", O_RDONLY, 0);
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
295 if (fd >= 0)
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
296 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
297 size_t len = full_read (fd, namebuf, sizeof (namebuf));
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
298 close (fd);
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
299 if (len > 0 && len < sizeof (namebuf))
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
300 {
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
301 namebuf[len] = '\0';
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
302 return canonicalize_file_name (namebuf);
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
303 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
304 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
305 }
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
306 # endif
3ba41edecde6 progreloc: Speed up executable lookup on various platforms.
Bruno Haible <bruno@clisp.org>
parents: 40188
diff changeset
307 # if defined __CYGWIN__
14399
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
308 /* The executable is accessible as /proc/<pid>/exe, at least in
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
309 Cygwin >= 1.5. */
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
310 {
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
311 char *link;
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
312
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
313 link = xreadlink ("/proc/self/exe");
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
314 if (link != NULL)
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
315 return link;
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
316 if (executable_fd < 0)
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
317 executable_fd = open ("/proc/self/exe", O_EXEC, 0);
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
318 }
5e10178f1466 On Cygwin, use /proc file system instead of win32 API.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
319 # endif
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
320 # if HAVE_MACH_O_DYLD_H && HAVE__NSGETEXECUTABLEPATH
16935
498a2211d839 Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
321 /* On Mac OS 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
322 int _NSGetExecutablePath (char *buf, uint32_t *bufsize);
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
323 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
324 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
325 unsigned int length = sizeof (location);
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
326 if (_NSGetExecutablePath (location, &length) == 0
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
327 && location[0] == '/')
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
328 return canonicalize_file_name (location);
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
329 # endif
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 /* Guess the executable's full path. We assume the executable has been
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 called via execlp() or execvp() with properly set up argv[0]. The
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 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
333 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 bool has_slash = false;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 const char *p;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 for (p = argv0; *p; p++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
338 if (*p == '/')
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 has_slash = true;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
341 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
342 }
4603
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 if (!has_slash)
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
346 /* 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
347 by $PATH. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
348 const char *path = getenv ("PATH");
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
350 if (path != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
351 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
352 const char *p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
353 const char *p_next;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
355 for (p = path; *p; p = p_next)
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 const char *q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
358 size_t p_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
359 char *concat_name;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
361 for (q = p; *q; q++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
362 if (*q == ':')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
363 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
364 p_len = q - p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
365 p_next = (*q == '\0' ? q : q + 1);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
367 /* 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
368 Now concatenate the path item and argv0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
369 concat_name = (char *) xmalloc (p_len + strlen (argv0) + 2);
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
370 # ifdef NO_XMALLOC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
371 if (concat_name == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
372 return NULL;
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
373 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
374 if (p_len == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
375 /* An empty PATH element designates the current directory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
376 strcpy (concat_name, argv0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
377 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
378 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
379 memcpy (concat_name, p, p_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
380 concat_name[p_len] = '/';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
381 strcpy (concat_name + p_len + 1, argv0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
382 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
383 if (maybe_executable (concat_name))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
384 return canonicalize_file_name (concat_name);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
385 free (concat_name);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
386 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
387 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
388 /* Not found in the PATH, assume the current directory. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 /* exec treats paths containing slashes as relative to the current
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 directory. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 if (maybe_executable (argv0))
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 return canonicalize_file_name (argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 /* No way to find the executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396 return NULL;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 #endif
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 /* Full pathname of executable, or NULL. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 static char *executable_fullname;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 static void
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 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
405 const char *argv0)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 {
9576
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
407 char *curr_prefix;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 /* Determine the full pathname of the current executable. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 executable_fullname = find_executable (argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 /* Determine the current installation prefix from it. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 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
414 executable_fullname);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 if (curr_prefix != NULL)
9576
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
416 {
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
417 /* 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
418 set_relocation_prefix (orig_installprefix, curr_prefix);
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
419
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
420 free (curr_prefix);
8cae100b8cd1 Improve memory cleanup in 'relocatable' module.
Bruno Haible <bruno@clisp.org>
parents: 9547
diff changeset
421 }
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424 /* Set program_name, based on argv[0], and original installation prefix and
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425 directory, for relocatability. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 void
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 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
428 const char *orig_installprefix,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
429 const char *orig_installdir)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431 const char *argv0_stripped = argv0;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
433 /* 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
434 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
435 Remove the ".bin" here. */
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
437 size_t argv0_len = strlen (argv0);
7023
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
438 const size_t exeext_len = sizeof (EXEEXT) - sizeof ("");
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
439 if (argv0_len > 4 + exeext_len)
d78abd0c0b70 Update from GNU gettext 0.15.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
440 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
441 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
442 if (sizeof (EXEEXT) > sizeof (""))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
443 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
444 /* 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
445 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
446 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
447 on one system and ".EXE" on another. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
448 static const char exeext[] = EXEEXT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
449 const char *s1 = argv0 + argv0_len - exeext_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
450 const char *s2 = exeext;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
451 for (; *s1 != '\0'; s1++, s2++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
452 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
453 unsigned char c1 = *s1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
454 unsigned char c2 = *s2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
455 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
456 != (c2 >= 'A' && c2 <= 'Z' ? c2 - 'A' + 'a' : c2))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
457 goto done_stripping;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
458 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
459 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
460 /* Remove ".bin" before EXEEXT or its equivalent. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
461 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
462 char *shorter = (char *) xmalloc (argv0_len - 4 + 1);
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
463 #ifdef NO_XMALLOC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
464 if (shorter != NULL)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
465 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
466 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
467 memcpy (shorter, argv0, argv0_len - exeext_len - 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
468 if (sizeof (EXEEXT) > sizeof (""))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
469 memcpy (shorter + argv0_len - exeext_len - 4,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
470 argv0 + argv0_len - exeext_len - 4,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
471 exeext_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
472 shorter[argv0_len - 4] = '\0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
473 argv0_stripped = shorter;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
474 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
475 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12049
diff changeset
476 done_stripping: ;
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
477 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
478 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
479
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
480 set_program_name (argv0_stripped);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
481
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
482 prepare_relocate (orig_installprefix, orig_installdir, argv0);
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
483 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
484
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
485 /* Return the full pathname of the current executable, based on the earlier
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
486 call to set_program_name_and_installdir. Return NULL if unknown. */
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
487 char *
4691
ce37d22a271f Remove K&R cruft.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4603
diff changeset
488 get_full_program_name (void)
4603
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
489 {
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
490 return executable_fullname;
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
491 }
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
492
fae2e3155b7a New module 'progname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
493 #endif