1 /* Relative (relocatable) prefix support.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2006 Free Software Foundation, Inc.
5 This file is part of libiberty.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 @deftypefn Extension {const char*} make_relative_prefix (const char *@var{progname}, 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}.
64 #include "libiberty.h"
73 # define DIR_SEPARATOR '/'
76 #if defined (_WIN32) || defined (__MSDOS__) \
77 || defined (__DJGPP__) || defined (__OS2__)
78 # define HAVE_DOS_BASED_FILE_SYSTEM
79 # define HAVE_HOST_EXECUTABLE_SUFFIX
80 # define HOST_EXECUTABLE_SUFFIX ".exe"
81 # ifndef DIR_SEPARATOR_2
82 # define DIR_SEPARATOR_2 '\\'
84 # define PATH_SEPARATOR ';'
86 # define PATH_SEPARATOR ':'
89 #ifndef DIR_SEPARATOR_2
90 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
92 # define IS_DIR_SEPARATOR(ch) \
93 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
98 static char *save_string (const char *, int);
99 static char **split_directories (const char *, int *);
100 static void free_split_directories (char **);
103 save_string (const char *s
, int len
)
105 char *result
= (char *) malloc (len
+ 1);
107 memcpy (result
, s
, len
);
112 /* Split a filename into component directories. */
115 split_directories (const char *name
, int *ptr_num_dirs
)
122 /* Count the number of directories. Special case MSDOS disk names as part
123 of the initial directory. */
125 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
126 if (name
[1] == ':' && IS_DIR_SEPARATOR (name
[2]))
131 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
133 while ((ch
= *p
++) != '\0')
135 if (IS_DIR_SEPARATOR (ch
))
138 while (IS_DIR_SEPARATOR (*p
))
143 dirs
= (char **) malloc (sizeof (char *) * (num_dirs
+ 2));
147 /* Now copy the directory parts. */
150 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
151 if (name
[1] == ':' && IS_DIR_SEPARATOR (name
[2]))
153 dirs
[num_dirs
++] = save_string (p
, 3);
154 if (dirs
[num_dirs
- 1] == NULL
)
161 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
164 while ((ch
= *p
++) != '\0')
166 if (IS_DIR_SEPARATOR (ch
))
168 while (IS_DIR_SEPARATOR (*p
))
171 dirs
[num_dirs
++] = save_string (q
, p
- q
);
172 if (dirs
[num_dirs
- 1] == NULL
)
174 dirs
[num_dirs
] = NULL
;
175 free_split_directories (dirs
);
183 dirs
[num_dirs
++] = save_string (q
, p
- 1 - q
);
184 dirs
[num_dirs
] = NULL
;
186 if (dirs
[num_dirs
- 1] == NULL
)
188 free_split_directories (dirs
);
193 *ptr_num_dirs
= num_dirs
;
197 /* Release storage held by split directories. */
200 free_split_directories (char **dirs
)
204 while (dirs
[i
] != NULL
)
207 free ((char *) dirs
);
210 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
211 to PREFIX starting with the directory portion of PROGNAME and a relative
212 pathname of the difference between BIN_PREFIX and PREFIX.
214 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
215 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
216 function will return /red/green/blue/../../omega/.
218 If no relative prefix can be found, return NULL. */
221 make_relative_prefix_1 (const char *progname
, const char *bin_prefix
,
222 const char *prefix
, const int resolve_links
)
224 char **prog_dirs
, **bin_dirs
, **prefix_dirs
;
225 int prog_num
, bin_num
, prefix_num
;
228 char *ret
, *ptr
, *full_progname
= NULL
;
230 if (progname
== NULL
|| bin_prefix
== NULL
|| prefix
== NULL
)
233 /* If there is no full pathname, try to find the program by checking in each
234 of the directories specified in the PATH environment variable. */
235 if (lbasename (progname
) == progname
)
239 temp
= getenv ("PATH");
242 char *startp
, *endp
, *nstore
;
243 size_t prefixlen
= strlen (temp
) + 1;
247 nstore
= (char *) alloca (prefixlen
+ strlen (progname
) + 1);
249 startp
= endp
= temp
;
252 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
257 nstore
[1] = DIR_SEPARATOR
;
262 strncpy (nstore
, startp
, endp
- startp
);
263 if (! IS_DIR_SEPARATOR (endp
[-1]))
265 nstore
[endp
- startp
] = DIR_SEPARATOR
;
266 nstore
[endp
- startp
+ 1] = 0;
269 nstore
[endp
- startp
] = 0;
271 strcat (nstore
, progname
);
272 if (! access (nstore
, X_OK
)
273 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
274 || ! access (strcat (nstore
, HOST_EXECUTABLE_SUFFIX
), X_OK
)
284 endp
= startp
= endp
+ 1;
294 full_progname
= lrealpath (progname
);
295 if (full_progname
== NULL
)
299 full_progname
= strdup(progname
);
301 prog_dirs
= split_directories (full_progname
, &prog_num
);
302 bin_dirs
= split_directories (bin_prefix
, &bin_num
);
303 free (full_progname
);
304 if (bin_dirs
== NULL
|| prog_dirs
== NULL
)
307 /* Remove the program name from comparison of directory names. */
310 /* If we are still installed in the standard location, we don't need to
311 specify relative directories. Also, if argv[0] still doesn't contain
312 any directory specifiers after the search above, then there is not much
314 if (prog_num
== bin_num
)
316 for (i
= 0; i
< bin_num
; i
++)
318 if (strcmp (prog_dirs
[i
], bin_dirs
[i
]) != 0)
322 if (prog_num
<= 0 || i
== bin_num
)
324 free_split_directories (prog_dirs
);
325 free_split_directories (bin_dirs
);
326 prog_dirs
= bin_dirs
= (char **) 0;
331 prefix_dirs
= split_directories (prefix
, &prefix_num
);
332 if (prefix_dirs
== NULL
)
334 free_split_directories (prog_dirs
);
335 free_split_directories (bin_dirs
);
339 /* Find how many directories are in common between bin_prefix & prefix. */
340 n
= (prefix_num
< bin_num
) ? prefix_num
: bin_num
;
341 for (common
= 0; common
< n
; common
++)
343 if (strcmp (bin_dirs
[common
], prefix_dirs
[common
]) != 0)
347 /* If there are no common directories, there can be no relative prefix. */
350 free_split_directories (prog_dirs
);
351 free_split_directories (bin_dirs
);
352 free_split_directories (prefix_dirs
);
356 /* Two passes: first figure out the size of the result string, and
357 then construct it. */
359 for (i
= 0; i
< prog_num
; i
++)
360 needed_len
+= strlen (prog_dirs
[i
]);
361 needed_len
+= sizeof (DIR_UP
) * (bin_num
- common
);
362 for (i
= common
; i
< prefix_num
; i
++)
363 needed_len
+= strlen (prefix_dirs
[i
]);
364 needed_len
+= 1; /* Trailing NUL. */
366 ret
= (char *) malloc (needed_len
);
370 /* Build up the pathnames in argv[0]. */
372 for (i
= 0; i
< prog_num
; i
++)
373 strcat (ret
, prog_dirs
[i
]);
375 /* Now build up the ..'s. */
376 ptr
= ret
+ strlen(ret
);
377 for (i
= common
; i
< bin_num
; i
++)
379 strcpy (ptr
, DIR_UP
);
380 ptr
+= sizeof (DIR_UP
) - 1;
381 *(ptr
++) = DIR_SEPARATOR
;
385 /* Put in directories to move over to prefix. */
386 for (i
= common
; i
< prefix_num
; i
++)
387 strcat (ret
, prefix_dirs
[i
]);
389 free_split_directories (prog_dirs
);
390 free_split_directories (bin_dirs
);
391 free_split_directories (prefix_dirs
);
397 /* Do the full job, including symlink resolution.
398 This path will find files installed in the same place as the
399 program even when a soft link has been made to the program
400 from somwhere else. */
403 make_relative_prefix (const char *progname
, const char *bin_prefix
,
406 return make_relative_prefix_1 (progname
, bin_prefix
, prefix
, 1);
409 /* Make the relative pathname without attempting to resolve any links.
410 '..' etc may also be left in the pathname.
411 This will find the files the user meant the program to find if the
412 installation is patched together with soft links. */
415 make_relative_prefix_ignore_links (const char *progname
,
416 const char *bin_prefix
,
419 return make_relative_prefix_1 (progname
, bin_prefix
, prefix
, 0);