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 HAVE_HOST_EXECUTABLE_SUFFIX
71 # define HOST_EXECUTABLE_SUFFIX ".exe"
72 # ifndef DIR_SEPARATOR_2
73 # define DIR_SEPARATOR_2 '\\'
75 # define PATH_SEPARATOR ';'
77 # define PATH_SEPARATOR ':'
80 #ifndef DIR_SEPARATOR_2
81 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
83 # define IS_DIR_SEPARATOR(ch) \
84 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
89 static char *save_string
PARAMS ((const char *, int));
90 static char **split_directories
PARAMS ((const char *, int *));
91 static void free_split_directories
PARAMS ((char **));
98 char *result
= malloc (len
+ 1);
100 memcpy (result
, s
, len
);
105 /* Split a filename into component directories. */
108 split_directories (name
, ptr_num_dirs
)
117 /* Count the number of directories. Special case MSDOS disk names as part
118 of the initial directory. */
120 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
121 if (name
[1] == ':' && IS_DIR_SEPARATOR (name
[2]))
126 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
128 while ((ch
= *p
++) != '\0')
130 if (IS_DIR_SEPARATOR (ch
))
133 while (IS_DIR_SEPARATOR (*p
))
138 dirs
= (char **) malloc (sizeof (char *) * (num_dirs
+ 2));
142 /* Now copy the directory parts. */
145 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
146 if (name
[1] == ':' && IS_DIR_SEPARATOR (name
[2]))
148 dirs
[num_dirs
++] = save_string (p
, 3);
149 if (dirs
[num_dirs
- 1] == NULL
)
156 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
159 while ((ch
= *p
++) != '\0')
161 if (IS_DIR_SEPARATOR (ch
))
163 while (IS_DIR_SEPARATOR (*p
))
166 dirs
[num_dirs
++] = save_string (q
, p
- q
);
167 if (dirs
[num_dirs
- 1] == NULL
)
169 dirs
[num_dirs
] = NULL
;
170 free_split_directories (dirs
);
178 dirs
[num_dirs
++] = save_string (q
, p
- 1 - q
);
179 dirs
[num_dirs
] = NULL
;
181 if (dirs
[num_dirs
- 1] == NULL
)
183 free_split_directories (dirs
);
188 *ptr_num_dirs
= num_dirs
;
192 /* Release storage held by split directories. */
195 free_split_directories (dirs
)
200 while (dirs
[i
] != NULL
)
203 free ((char *) dirs
);
206 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
207 to PREFIX starting with the directory portion of PROGNAME and a relative
208 pathname of the difference between BIN_PREFIX and PREFIX.
210 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
211 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
212 function will return /red/green/blue/../../omega/.
214 If no relative prefix can be found, return NULL. */
217 make_relative_prefix (progname
, bin_prefix
, prefix
)
218 const char *progname
;
219 const char *bin_prefix
;
222 char **prog_dirs
, **bin_dirs
, **prefix_dirs
;
223 int prog_num
, bin_num
, prefix_num
;
228 if (progname
== NULL
|| bin_prefix
== NULL
|| prefix
== NULL
)
231 prog_dirs
= split_directories (progname
, &prog_num
);
232 bin_dirs
= split_directories (bin_prefix
, &bin_num
);
233 if (bin_dirs
== NULL
|| prog_dirs
== NULL
)
236 /* If there is no full pathname, try to find the program by checking in each
237 of the directories specified in the PATH environment variable. */
242 temp
= getenv ("PATH");
245 char *startp
, *endp
, *nstore
;
246 size_t prefixlen
= strlen (temp
) + 1;
250 nstore
= (char *) alloca (prefixlen
+ strlen (progname
) + 1);
252 startp
= endp
= temp
;
255 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
260 nstore
[1] = DIR_SEPARATOR
;
265 strncpy (nstore
, startp
, endp
- startp
);
266 if (! IS_DIR_SEPARATOR (endp
[-1]))
268 nstore
[endp
- startp
] = DIR_SEPARATOR
;
269 nstore
[endp
- startp
+ 1] = 0;
272 nstore
[endp
- startp
] = 0;
274 strcat (nstore
, progname
);
275 if (! access (nstore
, X_OK
)
276 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
277 || ! access (strcat (nstore
, HOST_EXECUTABLE_SUFFIX
), X_OK
)
281 free_split_directories (prog_dirs
);
283 prog_dirs
= split_directories (progname
, &prog_num
);
284 if (prog_dirs
== NULL
)
286 free_split_directories (bin_dirs
);
294 endp
= startp
= endp
+ 1;
302 /* Remove the program name from comparison of directory names. */
305 /* If we are still installed in the standard location, we don't need to
306 specify relative directories. Also, if argv[0] still doesn't contain
307 any directory specifiers after the search above, then there is not much
309 if (prog_num
== bin_num
)
311 for (i
= 0; i
< bin_num
; i
++)
313 if (strcmp (prog_dirs
[i
], bin_dirs
[i
]) != 0)
317 if (prog_num
<= 0 || i
== bin_num
)
319 free_split_directories (prog_dirs
);
320 free_split_directories (bin_dirs
);
321 prog_dirs
= bin_dirs
= (char **) 0;
326 prefix_dirs
= split_directories (prefix
, &prefix_num
);
327 if (prefix_dirs
== NULL
)
329 free_split_directories (prog_dirs
);
330 free_split_directories (bin_dirs
);
334 /* Find how many directories are in common between bin_prefix & prefix. */
335 n
= (prefix_num
< bin_num
) ? prefix_num
: bin_num
;
336 for (common
= 0; common
< n
; common
++)
338 if (strcmp (bin_dirs
[common
], prefix_dirs
[common
]) != 0)
342 /* If there are no common directories, there can be no relative prefix. */
345 free_split_directories (prog_dirs
);
346 free_split_directories (bin_dirs
);
347 free_split_directories (prefix_dirs
);
351 /* Two passes: first figure out the size of the result string, and
352 then construct it. */
354 for (i
= 0; i
< prog_num
; i
++)
355 needed_len
+= strlen (prog_dirs
[i
]);
356 needed_len
+= sizeof (DIR_UP
) * (bin_num
- common
);
357 for (i
= common
; i
< prefix_num
; i
++)
358 needed_len
+= strlen (prefix_dirs
[i
]);
359 needed_len
+= 1; /* Trailing NUL. */
361 ret
= (char *) malloc (needed_len
);
365 /* Build up the pathnames in argv[0]. */
367 for (i
= 0; i
< prog_num
; i
++)
368 strcat (ret
, prog_dirs
[i
]);
370 /* Now build up the ..'s. */
371 ptr
= ret
+ strlen(ret
);
372 for (i
= common
; i
< bin_num
; i
++)
374 strcpy (ptr
, DIR_UP
);
375 ptr
+= sizeof (DIR_UP
) - 1;
376 *(ptr
++) = DIR_SEPARATOR
;
380 /* Put in directories to move over to prefix. */
381 for (i
= common
; i
< prefix_num
; i
++)
382 strcat (ret
, prefix_dirs
[i
]);
384 free_split_directories (prog_dirs
);
385 free_split_directories (bin_dirs
);
386 free_split_directories (prefix_dirs
);