1 /* Relative (relocatable) prefix support.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 This file is part of libiberty.
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
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
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
23 @deftypefn Extension {const char*} make_relative_prefix (const char *@var{progname}, @
24 const char *@var{bin_prefix}, const char *@var{prefix})
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}.
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.
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/}.
43 The return value is normally allocated via @code{malloc}. If no
44 relative prefix can be found, return @code{NULL}.
60 #ifdef HAVE_SYS_STAT_H
67 #include "libiberty.h"
76 # define DIR_SEPARATOR '/'
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 '\\'
87 # define PATH_SEPARATOR ';'
89 # define PATH_SEPARATOR ':'
92 #ifndef DIR_SEPARATOR_2
93 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
95 # define IS_DIR_SEPARATOR(ch) \
96 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
101 static char *save_string (const char *, int);
102 static char **split_directories (const char *, int *);
103 static void free_split_directories (char **);
106 save_string (const char *s
, int len
)
108 char *result
= (char *) malloc (len
+ 1);
110 memcpy (result
, s
, len
);
115 /* Split a filename into component directories. */
118 split_directories (const char *name
, int *ptr_num_dirs
)
128 /* Count the number of directories. Special case MSDOS disk names as part
129 of the initial directory. */
131 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
132 if (name
[1] == ':' && IS_DIR_SEPARATOR (name
[2]))
137 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
139 while ((ch
= *p
++) != '\0')
141 if (IS_DIR_SEPARATOR (ch
))
144 while (IS_DIR_SEPARATOR (*p
))
149 dirs
= (char **) malloc (sizeof (char *) * (num_dirs
+ 2));
153 /* Now copy the directory parts. */
156 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
157 if (name
[1] == ':' && IS_DIR_SEPARATOR (name
[2]))
159 dirs
[num_dirs
++] = save_string (p
, 3);
160 if (dirs
[num_dirs
- 1] == NULL
)
167 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
170 while ((ch
= *p
++) != '\0')
172 if (IS_DIR_SEPARATOR (ch
))
174 while (IS_DIR_SEPARATOR (*p
))
177 dirs
[num_dirs
++] = save_string (q
, p
- q
);
178 if (dirs
[num_dirs
- 1] == NULL
)
180 dirs
[num_dirs
] = NULL
;
181 free_split_directories (dirs
);
189 dirs
[num_dirs
++] = save_string (q
, p
- 1 - q
);
190 dirs
[num_dirs
] = NULL
;
192 if (dirs
[num_dirs
- 1] == NULL
)
194 free_split_directories (dirs
);
199 *ptr_num_dirs
= num_dirs
;
203 /* Release storage held by split directories. */
206 free_split_directories (char **dirs
)
212 while (dirs
[i
] != NULL
)
215 free ((char *) dirs
);
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.
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/.
227 If no relative prefix can be found, return NULL. */
230 make_relative_prefix_1 (const char *progname
, const char *bin_prefix
,
231 const char *prefix
, const int resolve_links
)
233 char **prog_dirs
= NULL
, **bin_dirs
= NULL
, **prefix_dirs
= NULL
;
234 int prog_num
, bin_num
, prefix_num
;
237 char *ret
= NULL
, *ptr
, *full_progname
;
238 char *alloc_ptr
= NULL
;
240 if (progname
== NULL
|| bin_prefix
== NULL
|| prefix
== NULL
)
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 if (lbasename (progname
) == progname
)
249 temp
= getenv ("PATH");
252 char *startp
, *endp
, *nstore
;
253 size_t prefixlen
= strlen (temp
) + 1;
258 len
= prefixlen
+ strlen (progname
) + 1;
259 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
260 len
+= strlen (HOST_EXECUTABLE_SUFFIX
);
262 if (len
< MAX_ALLOCA_SIZE
)
263 nstore
= (char *) alloca (len
);
265 alloc_ptr
= nstore
= (char *) malloc (len
);
267 startp
= endp
= temp
;
270 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
275 nstore
[1] = DIR_SEPARATOR
;
280 memcpy (nstore
, startp
, endp
- startp
);
281 if (! IS_DIR_SEPARATOR (endp
[-1]))
283 nstore
[endp
- startp
] = DIR_SEPARATOR
;
284 nstore
[endp
- startp
+ 1] = 0;
287 nstore
[endp
- startp
] = 0;
289 strcat (nstore
, progname
);
290 if (! access (nstore
, X_OK
)
291 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
292 || ! access (strcat (nstore
, HOST_EXECUTABLE_SUFFIX
), X_OK
)
296 #if defined (HAVE_SYS_STAT_H) && defined (S_ISREG)
298 if (stat (nstore
, &st
) >= 0 && S_ISREG (st
.st_mode
))
308 endp
= startp
= endp
+ 1;
317 full_progname
= lrealpath (progname
);
319 full_progname
= strdup (progname
);
320 if (full_progname
== NULL
)
323 prog_dirs
= split_directories (full_progname
, &prog_num
);
324 free (full_progname
);
325 if (prog_dirs
== NULL
)
328 bin_dirs
= split_directories (bin_prefix
, &bin_num
);
329 if (bin_dirs
== NULL
)
332 /* Remove the program name from comparison of directory names. */
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
339 if (prog_num
== bin_num
)
341 for (i
= 0; i
< bin_num
; i
++)
343 if (strcmp (prog_dirs
[i
], bin_dirs
[i
]) != 0)
347 if (prog_num
<= 0 || i
== bin_num
)
351 prefix_dirs
= split_directories (prefix
, &prefix_num
);
352 if (prefix_dirs
== NULL
)
355 /* Find how many directories are in common between bin_prefix & prefix. */
356 n
= (prefix_num
< bin_num
) ? prefix_num
: bin_num
;
357 for (common
= 0; common
< n
; common
++)
359 if (strcmp (bin_dirs
[common
], prefix_dirs
[common
]) != 0)
363 /* If there are no common directories, there can be no relative prefix. */
367 /* Two passes: first figure out the size of the result string, and
368 then construct it. */
370 for (i
= 0; i
< prog_num
; i
++)
371 needed_len
+= strlen (prog_dirs
[i
]);
372 needed_len
+= sizeof (DIR_UP
) * (bin_num
- common
);
373 for (i
= common
; i
< prefix_num
; i
++)
374 needed_len
+= strlen (prefix_dirs
[i
]);
375 needed_len
+= 1; /* Trailing NUL. */
377 ret
= (char *) malloc (needed_len
);
381 /* Build up the pathnames in argv[0]. */
383 for (i
= 0; i
< prog_num
; i
++)
384 strcat (ret
, prog_dirs
[i
]);
386 /* Now build up the ..'s. */
387 ptr
= ret
+ strlen(ret
);
388 for (i
= common
; i
< bin_num
; i
++)
390 strcpy (ptr
, DIR_UP
);
391 ptr
+= sizeof (DIR_UP
) - 1;
392 *(ptr
++) = DIR_SEPARATOR
;
396 /* Put in directories to move over to prefix. */
397 for (i
= common
; i
< prefix_num
; i
++)
398 strcat (ret
, prefix_dirs
[i
]);
401 free_split_directories (prog_dirs
);
402 free_split_directories (bin_dirs
);
403 free_split_directories (prefix_dirs
);
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. */
416 make_relative_prefix (const char *progname
, const char *bin_prefix
,
419 return make_relative_prefix_1 (progname
, bin_prefix
, prefix
, 1);
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. */
428 make_relative_prefix_ignore_links (const char *progname
,
429 const char *bin_prefix
,
432 return make_relative_prefix_1 (progname
, bin_prefix
, prefix
, 0);