This source file includes following definitions.
- add_dotbin
- relocate_libdirs
- activate_libdirs
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 #define _GL_USE_STDLIB_ALLOC 1
73 #include <config.h>
74
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <unistd.h>
79 #include <errno.h>
80
81 #include "progname.h"
82 #include "relocatable.h"
83 #include "c-ctype.h"
84 #include "verify.h"
85
86
87 #undef fprintf
88 #undef strerror
89
90
91
92 static char *
93 add_dotbin (const char *filename)
94 {
95 size_t filename_len = strlen (filename);
96 char *result = (char *) malloc (filename_len + 4 + 1);
97
98 if (result != NULL)
99 {
100 if (sizeof (EXEEXT) > sizeof (""))
101 {
102
103 const size_t exeext_len = sizeof (EXEEXT) - sizeof ("");
104 static const char exeext[] = EXEEXT;
105 if (filename_len > exeext_len)
106 {
107
108
109
110
111 const char *s1 = filename + filename_len - exeext_len;
112 const char *s2 = exeext;
113 for (; *s1 != '\0'; s1++, s2++)
114 {
115 unsigned char c1 = *s1;
116 unsigned char c2 = *s2;
117 if (c_tolower (c1) != c_tolower (c2))
118 goto simple_append;
119 }
120
121 memcpy (result, filename, filename_len - exeext_len);
122 memcpy (result + filename_len - exeext_len, ".bin", 4);
123 memcpy (result + filename_len - exeext_len + 4,
124 filename + filename_len - exeext_len,
125 exeext_len + 1);
126 return result;
127 }
128 }
129 simple_append:
130
131 memcpy (result, filename, filename_len);
132 memcpy (result + filename_len, ".bin", 4 + 1);
133 return result;
134 }
135 else
136 {
137 fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
138 exit (1);
139 }
140 }
141
142
143 static const char *libdirs[] = { LIBDIRS NULL };
144
145 verify (sizeof (libdirs) / sizeof (libdirs[0]) > 1);
146
147
148 static void
149 relocate_libdirs ()
150 {
151 size_t i;
152
153 for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
154 libdirs[i] = relocate (libdirs[i]);
155 }
156
157
158 static void
159 activate_libdirs ()
160 {
161 const char *old_value;
162 size_t total;
163 size_t i;
164 char *value;
165 char *p;
166
167 old_value = getenv (LIBPATHVAR);
168 if (old_value == NULL)
169 old_value = "";
170
171 total = 0;
172 for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
173 total += strlen (libdirs[i]) + 1;
174 total += strlen (old_value) + 1;
175
176 value = (char *) malloc (total);
177 if (value == NULL)
178 {
179 fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
180 exit (1);
181 }
182 p = value;
183 for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
184 {
185 size_t len = strlen (libdirs[i]);
186 memcpy (p, libdirs[i], len);
187 p += len;
188 *p++ = ':';
189 }
190 if (old_value[0] != '\0')
191 strcpy (p, old_value);
192 else
193 p[-1] = '\0';
194
195 if (setenv (LIBPATHVAR, value, 1) < 0)
196 {
197 fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
198 exit (1);
199 }
200 }
201
202 int
203 main (int argc, char *argv[])
204 {
205 char *full_program_name;
206
207
208
209 set_program_name_and_installdir (argv[0], INSTALLPREFIX, INSTALLDIR);
210
211
212 full_program_name = get_full_program_name ();
213 if (full_program_name == NULL)
214 full_program_name = argv[0];
215
216
217 argv[0] = add_dotbin (full_program_name);
218 relocate_libdirs ();
219 activate_libdirs ();
220 execv (argv[0], argv);
221 fprintf (stderr, "%s: could not execute %s: %s\n",
222 program_name, argv[0], strerror (errno));
223 exit (127);
224 }