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