/src/binutils-gdb/libiberty/make-relative-prefix.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Relative (relocatable) prefix support. |
2 | | Copyright (C) 1987-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of libiberty. |
5 | | |
6 | | GCC is free software; you can redistribute it and/or modify it under |
7 | | the terms of the GNU General Public License as published by the Free |
8 | | Software Foundation; either version 2, or (at your option) any later |
9 | | version. |
10 | | |
11 | | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | | for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with GCC; see the file COPYING. If not, write to the Free |
18 | | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | | 02110-1301, USA. */ |
20 | | |
21 | | /* |
22 | | |
23 | | @deftypefn Extension {const char*} make_relative_prefix (const char *@var{progname}, @ |
24 | | const char *@var{bin_prefix}, const char *@var{prefix}) |
25 | | |
26 | | Given three paths @var{progname}, @var{bin_prefix}, @var{prefix}, |
27 | | return the path that is in the same position relative to |
28 | | @var{progname}'s directory as @var{prefix} is relative to |
29 | | @var{bin_prefix}. That is, a string starting with the directory |
30 | | portion of @var{progname}, followed by a relative pathname of the |
31 | | difference between @var{bin_prefix} and @var{prefix}. |
32 | | |
33 | | If @var{progname} does not contain any directory separators, |
34 | | @code{make_relative_prefix} will search @env{PATH} to find a program |
35 | | named @var{progname}. Also, if @var{progname} is a symbolic link, |
36 | | the symbolic link will be resolved. |
37 | | |
38 | | For example, if @var{bin_prefix} is @code{/alpha/beta/gamma/gcc/delta}, |
39 | | @var{prefix} is @code{/alpha/beta/gamma/omega/}, and @var{progname} is |
40 | | @code{/red/green/blue/gcc}, then this function will return |
41 | | @code{/red/green/blue/../../omega/}. |
42 | | |
43 | | The return value is normally allocated via @code{malloc}. If no |
44 | | relative prefix can be found, return @code{NULL}. |
45 | | |
46 | | @end deftypefn |
47 | | |
48 | | */ |
49 | | |
50 | | #ifdef HAVE_CONFIG_H |
51 | | #include "config.h" |
52 | | #endif |
53 | | |
54 | | #ifdef HAVE_STDLIB_H |
55 | | #include <stdlib.h> |
56 | | #endif |
57 | | #ifdef HAVE_UNISTD_H |
58 | | #include <unistd.h> |
59 | | #endif |
60 | | #ifdef HAVE_SYS_STAT_H |
61 | | #include <sys/stat.h> |
62 | | #endif |
63 | | |
64 | | #include <string.h> |
65 | | |
66 | | #include "ansidecl.h" |
67 | | #include "libiberty.h" |
68 | | |
69 | | #ifndef R_OK |
70 | | #define R_OK 4 |
71 | | #define W_OK 2 |
72 | | #define X_OK 1 |
73 | | #endif |
74 | | |
75 | | #ifndef DIR_SEPARATOR |
76 | 0 | # define DIR_SEPARATOR '/' |
77 | | #endif |
78 | | |
79 | | #if defined (_WIN32) || defined (__MSDOS__) \ |
80 | | || defined (__DJGPP__) || defined (__OS2__) |
81 | | # define HAVE_DOS_BASED_FILE_SYSTEM |
82 | | # define HAVE_HOST_EXECUTABLE_SUFFIX |
83 | | # define HOST_EXECUTABLE_SUFFIX ".exe" |
84 | | # ifndef DIR_SEPARATOR_2 |
85 | | # define DIR_SEPARATOR_2 '\\' |
86 | | # endif |
87 | | # define PATH_SEPARATOR ';' |
88 | | #else |
89 | 0 | # define PATH_SEPARATOR ':' |
90 | | #endif |
91 | | |
92 | | #ifndef DIR_SEPARATOR_2 |
93 | 0 | # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) |
94 | | #else |
95 | | # define IS_DIR_SEPARATOR(ch) \ |
96 | | (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) |
97 | | #endif |
98 | | |
99 | 0 | #define DIR_UP ".." |
100 | | |
101 | | static char *save_string (const char *, int); |
102 | | static char **split_directories (const char *, int *); |
103 | | static void free_split_directories (char **); |
104 | | |
105 | | static char * |
106 | | save_string (const char *s, int len) |
107 | 0 | { |
108 | 0 | char *result = (char *) malloc (len + 1); |
109 | |
|
110 | 0 | memcpy (result, s, len); |
111 | 0 | result[len] = 0; |
112 | 0 | return result; |
113 | 0 | } |
114 | | |
115 | | /* Split a filename into component directories. */ |
116 | | |
117 | | static char ** |
118 | | split_directories (const char *name, int *ptr_num_dirs) |
119 | 0 | { |
120 | 0 | int num_dirs = 0; |
121 | 0 | char **dirs; |
122 | 0 | const char *p, *q; |
123 | 0 | int ch; |
124 | |
|
125 | 0 | if (!*name) |
126 | 0 | return NULL; |
127 | | |
128 | | /* Count the number of directories. Special case MSDOS disk names as part |
129 | | of the initial directory. */ |
130 | 0 | p = name; |
131 | | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
132 | | if (name[1] == ':' && IS_DIR_SEPARATOR (name[2])) |
133 | | { |
134 | | p += 3; |
135 | | num_dirs++; |
136 | | } |
137 | | #endif /* HAVE_DOS_BASED_FILE_SYSTEM */ |
138 | |
|
139 | 0 | while ((ch = *p++) != '\0') |
140 | 0 | { |
141 | 0 | if (IS_DIR_SEPARATOR (ch)) |
142 | 0 | { |
143 | 0 | num_dirs++; |
144 | 0 | while (IS_DIR_SEPARATOR (*p)) |
145 | 0 | p++; |
146 | 0 | } |
147 | 0 | } |
148 | |
|
149 | 0 | dirs = (char **) malloc (sizeof (char *) * (num_dirs + 2)); |
150 | 0 | if (dirs == NULL) |
151 | 0 | return NULL; |
152 | | |
153 | | /* Now copy the directory parts. */ |
154 | 0 | num_dirs = 0; |
155 | 0 | p = name; |
156 | | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
157 | | if (name[1] == ':' && IS_DIR_SEPARATOR (name[2])) |
158 | | { |
159 | | dirs[num_dirs++] = save_string (p, 3); |
160 | | if (dirs[num_dirs - 1] == NULL) |
161 | | { |
162 | | free (dirs); |
163 | | return NULL; |
164 | | } |
165 | | p += 3; |
166 | | } |
167 | | #endif /* HAVE_DOS_BASED_FILE_SYSTEM */ |
168 | |
|
169 | 0 | q = p; |
170 | 0 | while ((ch = *p++) != '\0') |
171 | 0 | { |
172 | 0 | if (IS_DIR_SEPARATOR (ch)) |
173 | 0 | { |
174 | 0 | while (IS_DIR_SEPARATOR (*p)) |
175 | 0 | p++; |
176 | |
|
177 | 0 | dirs[num_dirs++] = save_string (q, p - q); |
178 | 0 | if (dirs[num_dirs - 1] == NULL) |
179 | 0 | { |
180 | 0 | dirs[num_dirs] = NULL; |
181 | 0 | free_split_directories (dirs); |
182 | 0 | return NULL; |
183 | 0 | } |
184 | 0 | q = p; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 0 | if (p - 1 - q > 0) |
189 | 0 | dirs[num_dirs++] = save_string (q, p - 1 - q); |
190 | 0 | dirs[num_dirs] = NULL; |
191 | |
|
192 | 0 | if (dirs[num_dirs - 1] == NULL) |
193 | 0 | { |
194 | 0 | free_split_directories (dirs); |
195 | 0 | return NULL; |
196 | 0 | } |
197 | | |
198 | 0 | if (ptr_num_dirs) |
199 | 0 | *ptr_num_dirs = num_dirs; |
200 | 0 | return dirs; |
201 | 0 | } |
202 | | |
203 | | /* Release storage held by split directories. */ |
204 | | |
205 | | static void |
206 | | free_split_directories (char **dirs) |
207 | 0 | { |
208 | 0 | int i = 0; |
209 | |
|
210 | 0 | if (dirs != NULL) |
211 | 0 | { |
212 | 0 | while (dirs[i] != NULL) |
213 | 0 | free (dirs[i++]); |
214 | |
|
215 | 0 | free ((char *) dirs); |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets |
220 | | to PREFIX starting with the directory portion of PROGNAME and a relative |
221 | | pathname of the difference between BIN_PREFIX and PREFIX. |
222 | | |
223 | | For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is |
224 | | /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this |
225 | | function will return /red/green/blue/../../omega/. |
226 | | |
227 | | If no relative prefix can be found, return NULL. */ |
228 | | |
229 | | static char * |
230 | | make_relative_prefix_1 (const char *progname, const char *bin_prefix, |
231 | | const char *prefix, const int resolve_links) |
232 | 0 | { |
233 | 0 | char **prog_dirs = NULL, **bin_dirs = NULL, **prefix_dirs = NULL; |
234 | 0 | int prog_num, bin_num, prefix_num; |
235 | 0 | int i, n, common; |
236 | 0 | int needed_len; |
237 | 0 | char *ret = NULL, *ptr, *full_progname; |
238 | 0 | char *alloc_ptr = NULL; |
239 | |
|
240 | 0 | if (progname == NULL || bin_prefix == NULL || prefix == NULL) |
241 | 0 | return NULL; |
242 | | |
243 | | /* If there is no full pathname, try to find the program by checking in each |
244 | | of the directories specified in the PATH environment variable. */ |
245 | 0 | if (lbasename (progname) == progname) |
246 | 0 | { |
247 | 0 | char *temp; |
248 | |
|
249 | 0 | temp = getenv ("PATH"); |
250 | 0 | if (temp) |
251 | 0 | { |
252 | 0 | char *startp, *endp, *nstore; |
253 | 0 | size_t prefixlen = strlen (temp) + 1; |
254 | 0 | size_t len; |
255 | 0 | if (prefixlen < 2) |
256 | 0 | prefixlen = 2; |
257 | |
|
258 | 0 | len = prefixlen + strlen (progname) + 1; |
259 | | #ifdef HAVE_HOST_EXECUTABLE_SUFFIX |
260 | | len += strlen (HOST_EXECUTABLE_SUFFIX); |
261 | | #endif |
262 | 0 | if (len < MAX_ALLOCA_SIZE) |
263 | 0 | nstore = (char *) alloca (len); |
264 | 0 | else |
265 | 0 | alloc_ptr = nstore = (char *) malloc (len); |
266 | |
|
267 | 0 | startp = endp = temp; |
268 | 0 | while (1) |
269 | 0 | { |
270 | 0 | if (*endp == PATH_SEPARATOR || *endp == 0) |
271 | 0 | { |
272 | 0 | if (endp == startp) |
273 | 0 | { |
274 | 0 | nstore[0] = '.'; |
275 | 0 | nstore[1] = DIR_SEPARATOR; |
276 | 0 | nstore[2] = '\0'; |
277 | 0 | } |
278 | 0 | else |
279 | 0 | { |
280 | 0 | memcpy (nstore, startp, endp - startp); |
281 | 0 | if (! IS_DIR_SEPARATOR (endp[-1])) |
282 | 0 | { |
283 | 0 | nstore[endp - startp] = DIR_SEPARATOR; |
284 | 0 | nstore[endp - startp + 1] = 0; |
285 | 0 | } |
286 | 0 | else |
287 | 0 | nstore[endp - startp] = 0; |
288 | 0 | } |
289 | 0 | strcat (nstore, progname); |
290 | 0 | if (! access (nstore, X_OK) |
291 | | #ifdef HAVE_HOST_EXECUTABLE_SUFFIX |
292 | | || ! access (strcat (nstore, HOST_EXECUTABLE_SUFFIX), X_OK) |
293 | | #endif |
294 | 0 | ) |
295 | 0 | { |
296 | 0 | #if defined (HAVE_SYS_STAT_H) && defined (S_ISREG) |
297 | 0 | struct stat st; |
298 | 0 | if (stat (nstore, &st) >= 0 && S_ISREG (st.st_mode)) |
299 | 0 | #endif |
300 | 0 | { |
301 | 0 | progname = nstore; |
302 | 0 | break; |
303 | 0 | } |
304 | 0 | } |
305 | | |
306 | 0 | if (*endp == 0) |
307 | 0 | break; |
308 | 0 | endp = startp = endp + 1; |
309 | 0 | } |
310 | 0 | else |
311 | 0 | endp++; |
312 | 0 | } |
313 | 0 | } |
314 | 0 | } |
315 | |
|
316 | 0 | if (resolve_links) |
317 | 0 | full_progname = lrealpath (progname); |
318 | 0 | else |
319 | 0 | full_progname = strdup (progname); |
320 | 0 | if (full_progname == NULL) |
321 | 0 | goto bailout; |
322 | | |
323 | 0 | prog_dirs = split_directories (full_progname, &prog_num); |
324 | 0 | free (full_progname); |
325 | 0 | if (prog_dirs == NULL) |
326 | 0 | goto bailout; |
327 | | |
328 | 0 | bin_dirs = split_directories (bin_prefix, &bin_num); |
329 | 0 | if (bin_dirs == NULL) |
330 | 0 | goto bailout; |
331 | | |
332 | | /* Remove the program name from comparison of directory names. */ |
333 | 0 | prog_num--; |
334 | | |
335 | | /* If we are still installed in the standard location, we don't need to |
336 | | specify relative directories. Also, if argv[0] still doesn't contain |
337 | | any directory specifiers after the search above, then there is not much |
338 | | we can do. */ |
339 | 0 | if (prog_num == bin_num) |
340 | 0 | { |
341 | 0 | for (i = 0; i < bin_num; i++) |
342 | 0 | { |
343 | 0 | if (strcmp (prog_dirs[i], bin_dirs[i]) != 0) |
344 | 0 | break; |
345 | 0 | } |
346 | |
|
347 | 0 | if (prog_num <= 0 || i == bin_num) |
348 | 0 | goto bailout; |
349 | 0 | } |
350 | | |
351 | 0 | prefix_dirs = split_directories (prefix, &prefix_num); |
352 | 0 | if (prefix_dirs == NULL) |
353 | 0 | goto bailout; |
354 | | |
355 | | /* Find how many directories are in common between bin_prefix & prefix. */ |
356 | 0 | n = (prefix_num < bin_num) ? prefix_num : bin_num; |
357 | 0 | for (common = 0; common < n; common++) |
358 | 0 | { |
359 | 0 | if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0) |
360 | 0 | break; |
361 | 0 | } |
362 | | |
363 | | /* If there are no common directories, there can be no relative prefix. */ |
364 | 0 | if (common == 0) |
365 | 0 | goto bailout; |
366 | | |
367 | | /* Two passes: first figure out the size of the result string, and |
368 | | then construct it. */ |
369 | 0 | needed_len = 0; |
370 | 0 | for (i = 0; i < prog_num; i++) |
371 | 0 | needed_len += strlen (prog_dirs[i]); |
372 | 0 | needed_len += sizeof (DIR_UP) * (bin_num - common); |
373 | 0 | for (i = common; i < prefix_num; i++) |
374 | 0 | needed_len += strlen (prefix_dirs[i]); |
375 | 0 | needed_len += 1; /* Trailing NUL. */ |
376 | |
|
377 | 0 | ret = (char *) malloc (needed_len); |
378 | 0 | if (ret == NULL) |
379 | 0 | goto bailout; |
380 | | |
381 | | /* Build up the pathnames in argv[0]. */ |
382 | 0 | *ret = '\0'; |
383 | 0 | for (i = 0; i < prog_num; i++) |
384 | 0 | strcat (ret, prog_dirs[i]); |
385 | | |
386 | | /* Now build up the ..'s. */ |
387 | 0 | ptr = ret + strlen(ret); |
388 | 0 | for (i = common; i < bin_num; i++) |
389 | 0 | { |
390 | 0 | strcpy (ptr, DIR_UP); |
391 | 0 | ptr += sizeof (DIR_UP) - 1; |
392 | 0 | *(ptr++) = DIR_SEPARATOR; |
393 | 0 | } |
394 | 0 | *ptr = '\0'; |
395 | | |
396 | | /* Put in directories to move over to prefix. */ |
397 | 0 | for (i = common; i < prefix_num; i++) |
398 | 0 | strcat (ret, prefix_dirs[i]); |
399 | |
|
400 | 0 | bailout: |
401 | 0 | free_split_directories (prog_dirs); |
402 | 0 | free_split_directories (bin_dirs); |
403 | 0 | free_split_directories (prefix_dirs); |
404 | 0 | free (alloc_ptr); |
405 | |
|
406 | 0 | return ret; |
407 | 0 | } |
408 | | |
409 | | |
410 | | /* Do the full job, including symlink resolution. |
411 | | This path will find files installed in the same place as the |
412 | | program even when a soft link has been made to the program |
413 | | from somwhere else. */ |
414 | | |
415 | | char * |
416 | | make_relative_prefix (const char *progname, const char *bin_prefix, |
417 | | const char *prefix) |
418 | 0 | { |
419 | 0 | return make_relative_prefix_1 (progname, bin_prefix, prefix, 1); |
420 | 0 | } |
421 | | |
422 | | /* Make the relative pathname without attempting to resolve any links. |
423 | | '..' etc may also be left in the pathname. |
424 | | This will find the files the user meant the program to find if the |
425 | | installation is patched together with soft links. */ |
426 | | |
427 | | char * |
428 | | make_relative_prefix_ignore_links (const char *progname, |
429 | | const char *bin_prefix, |
430 | | const char *prefix) |
431 | 0 | { |
432 | 0 | return make_relative_prefix_1 (progname, bin_prefix, prefix, 0); |
433 | 0 | } |
434 | | |