1 /* Relative (relocatable) prefix support.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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, 59 Temple Place - Suite 330, 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 strings @var{progname}, @var{bin_prefix}, @var{prefix}, return a string
27 that gets to @var{prefix} starting with the directory portion of @var{progname} and
28 a relative pathname of the difference between @var{bin_prefix} and @var{prefix}.
30 For example, if @var{bin_prefix} is @code{/alpha/beta/gamma/gcc/delta}, @var{prefix}
31 is @code{/alpha/beta/gamma/omega/}, and @var{progname} is @code{/red/green/blue/gcc},
32 then this function will return @code{/red/green/blue/../../omega/}.
34 The return value is normally allocated via @code{malloc}. If no relative prefix
35 can be found, return @code{NULL}.
55 #include "libiberty.h"
64 # define DIR_SEPARATOR '/'
67 #if defined (_WIN32) || defined (__MSDOS__) \
68 || defined (__DJGPP__) || defined (__OS2__)
69 # define HAVE_DOS_BASED_FILE_SYSTEM
70 # define HOST_EXECUTABLE_SUFFIX ".exe"
71 # ifndef DIR_SEPARATOR_2
72 # define DIR_SEPARATOR_2 '\\'
74 # define PATH_SEPARATOR ';'
76 # define PATH_SEPARATOR ':'
79 #ifndef DIR_SEPARATOR_2
80 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
82 # define IS_DIR_SEPARATOR(ch) \
83 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
88 static char *save_string PARAMS ((const char *, int));
89 static char **split_directories PARAMS ((const char *, int *));
90 static void free_split_directories PARAMS ((char **));
97 char *result = malloc (len + 1);
99 memcpy (result, s, len);
104 /* Split a filename into component directories. */
107 split_directories (name, ptr_num_dirs)
116 /* Count the number of directories. Special case MSDOS disk names as part
117 of the initial directory. */
119 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
120 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
125 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
127 while ((ch = *p++) != '\0')
129 if (IS_DIR_SEPARATOR (ch))
132 while (IS_DIR_SEPARATOR (*p))
137 dirs = (char **) malloc (sizeof (char *) * (num_dirs + 2));
141 /* Now copy the directory parts. */
144 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
145 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
147 dirs[num_dirs++] = save_string (p, 3);
148 if (dirs[num_dirs - 1] == NULL)
155 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
158 while ((ch = *p++) != '\0')
160 if (IS_DIR_SEPARATOR (ch))
162 while (IS_DIR_SEPARATOR (*p))
165 dirs[num_dirs++] = save_string (q, p - q);
166 if (dirs[num_dirs - 1] == NULL)
168 dirs[num_dirs] = NULL;
169 free_split_directories (dirs);
177 dirs[num_dirs++] = save_string (q, p - 1 - q);
178 dirs[num_dirs] = NULL;
180 if (dirs[num_dirs - 1] == NULL)
182 free_split_directories (dirs);
187 *ptr_num_dirs = num_dirs;
191 /* Release storage held by split directories. */
194 free_split_directories (dirs)
199 while (dirs[i] != NULL)
202 free ((char *) dirs);
205 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
206 to PREFIX starting with the directory portion of PROGNAME and a relative
207 pathname of the difference between BIN_PREFIX and PREFIX.
209 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
210 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
211 function will return /red/green/blue/../../omega/.
213 If no relative prefix can be found, return NULL. */
216 make_relative_prefix (progname, bin_prefix, prefix)
217 const char *progname;
218 const char *bin_prefix;
221 char **prog_dirs, **bin_dirs, **prefix_dirs;
222 int prog_num, bin_num, prefix_num;
227 if (progname == NULL || bin_prefix == NULL || prefix == NULL)
230 prog_dirs = split_directories (progname, &prog_num);
231 bin_dirs = split_directories (bin_prefix, &bin_num);
232 if (bin_dirs == NULL || prog_dirs == NULL)
235 /* If there is no full pathname, try to find the program by checking in each
236 of the directories specified in the PATH environment variable. */
241 temp = getenv ("PATH");
244 char *startp, *endp, *nstore;
245 size_t prefixlen = strlen (temp) + 1;
249 nstore = (char *) alloca (prefixlen + strlen (progname) + 1);
251 startp = endp = temp;
254 if (*endp == PATH_SEPARATOR || *endp == 0)
259 nstore[1] = DIR_SEPARATOR;
264 strncpy (nstore, startp, endp - startp);
265 if (! IS_DIR_SEPARATOR (endp[-1]))
267 nstore[endp - startp] = DIR_SEPARATOR;
268 nstore[endp - startp + 1] = 0;
271 nstore[endp - startp] = 0;
273 strcat (nstore, progname);
274 if (! access (nstore, X_OK)
275 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
276 || ! access (strcat (nstore, HOST_EXECUTABLE_SUFFIX), X_OK)
280 free_split_directories (prog_dirs);
282 prog_dirs = split_directories (progname, &prog_num);
283 if (prog_dirs == NULL)
285 free_split_directories (bin_dirs);
293 endp = startp = endp + 1;
301 /* Remove the program name from comparison of directory names. */
304 /* If we are still installed in the standard location, we don't need to
305 specify relative directories. Also, if argv[0] still doesn't contain
306 any directory specifiers after the search above, then there is not much
308 if (prog_num == bin_num)
310 for (i = 0; i < bin_num; i++)
312 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
316 if (prog_num <= 0 || i == bin_num)
318 free_split_directories (prog_dirs);
319 free_split_directories (bin_dirs);
320 prog_dirs = bin_dirs = (char **) 0;
325 prefix_dirs = split_directories (prefix, &prefix_num);
326 if (prefix_dirs == NULL)
328 free_split_directories (prog_dirs);
329 free_split_directories (bin_dirs);
333 /* Find how many directories are in common between bin_prefix & prefix. */
334 n = (prefix_num < bin_num) ? prefix_num : bin_num;
335 for (common = 0; common < n; common++)
337 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
341 /* If there are no common directories, there can be no relative prefix. */
344 free_split_directories (prog_dirs);
345 free_split_directories (bin_dirs);
346 free_split_directories (prefix_dirs);
350 /* Two passes: first figure out the size of the result string, and
351 then construct it. */
353 for (i = 0; i < prog_num; i++)
354 needed_len += strlen (prog_dirs[i]);
355 needed_len += sizeof (DIR_UP) * (bin_num - common);
356 for (i = common; i < prefix_num; i++)
357 needed_len += strlen (prefix_dirs[i]);
358 needed_len += 1; /* Trailing NUL. */
360 ret = (char *) malloc (needed_len);
364 /* Build up the pathnames in argv[0]. */
365 for (i = 0; i < prog_num; i++)
366 strcat (ret, prog_dirs[i]);
368 /* Now build up the ..'s. */
369 ptr = ret + strlen(ret);
370 for (i = common; i < bin_num; i++)
372 strcpy (ptr, DIR_UP);
373 ptr += sizeof (DIR_UP) - 1;
374 *(ptr++) = DIR_SEPARATOR;
378 /* Put in directories to move over to prefix. */
379 for (i = common; i < prefix_num; i++)
380 strcat (ret, prefix_dirs[i]);
382 free_split_directories (prog_dirs);
383 free_split_directories (bin_dirs);
384 free_split_directories (prefix_dirs);