2002-12-06 Andrew Cagney <ac131313@redhat.com>
[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,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
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
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
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
26Given three strings @var{progname}, @var{bin_prefix}, @var{prefix}, return a string
27that gets to @var{prefix} starting with the directory portion of @var{progname} and
28a relative pathname of the difference between @var{bin_prefix} and @var{prefix}.
29
30For example, if @var{bin_prefix} is @code{/alpha/beta/gamma/gcc/delta}, @var{prefix}
31is @code{/alpha/beta/gamma/omega/}, and @var{progname} is @code{/red/green/blue/gcc},
32then this function will return @code{/red/green/blue/../../omega/}.
33
34The return value is normally allocated via @code{malloc}. If no relative prefix
35can be found, return @code{NULL}.
36
37@end deftypefn
38
39*/
40
41#ifdef HAVE_CONFIG_H
42#include "config.h"
43#endif
44
45#ifdef HAVE_STDLIB_H
46#include <stdlib.h>
47#endif
48#ifdef HAVE_UNISTD_H
49#include <unistd.h>
50#endif
51
52#include <string.h>
53
54#include "ansidecl.h"
55#include "libiberty.h"
56
57#ifndef R_OK
58#define R_OK 4
59#define W_OK 2
60#define X_OK 1
61#endif
62
63#ifndef DIR_SEPARATOR
64# define DIR_SEPARATOR '/'
65#endif
66
67#if defined (_WIN32) || defined (__MSDOS__) \
68 || defined (__DJGPP__) || defined (__OS2__)
69# define HAVE_DOS_BASED_FILE_SYSTEM
b51c1553 70# define HAVE_HOST_EXECUTABLE_SUFFIX
2a80c0a4
DD
71# define HOST_EXECUTABLE_SUFFIX ".exe"
72# ifndef DIR_SEPARATOR_2
73# define DIR_SEPARATOR_2 '\\'
74# endif
75# define PATH_SEPARATOR ';'
76#else
77# define PATH_SEPARATOR ':'
78#endif
79
80#ifndef DIR_SEPARATOR_2
81# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
82#else
83# define IS_DIR_SEPARATOR(ch) \
84 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
85#endif
86
87#define DIR_UP ".."
88
89static char *save_string PARAMS ((const char *, int));
90static char **split_directories PARAMS ((const char *, int *));
91static void free_split_directories PARAMS ((char **));
92
93static char *
94save_string (s, len)
95 const char *s;
96 int len;
97{
98 char *result = malloc (len + 1);
99
100 memcpy (result, s, len);
101 result[len] = 0;
102 return result;
103}
104
105/* Split a filename into component directories. */
106
107static char **
108split_directories (name, ptr_num_dirs)
109 const char *name;
110 int *ptr_num_dirs;
111{
112 int num_dirs = 0;
113 char **dirs;
114 const char *p, *q;
115 int ch;
116
117 /* Count the number of directories. Special case MSDOS disk names as part
118 of the initial directory. */
119 p = name;
120#ifdef HAVE_DOS_BASED_FILE_SYSTEM
121 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
122 {
123 p += 3;
124 num_dirs++;
125 }
126#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
127
128 while ((ch = *p++) != '\0')
129 {
130 if (IS_DIR_SEPARATOR (ch))
131 {
132 num_dirs++;
133 while (IS_DIR_SEPARATOR (*p))
134 p++;
135 }
136 }
137
138 dirs = (char **) malloc (sizeof (char *) * (num_dirs + 2));
139 if (dirs == NULL)
140 return NULL;
141
142 /* Now copy the directory parts. */
143 num_dirs = 0;
144 p = name;
145#ifdef HAVE_DOS_BASED_FILE_SYSTEM
146 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
147 {
148 dirs[num_dirs++] = save_string (p, 3);
149 if (dirs[num_dirs - 1] == NULL)
150 {
151 free (dirs);
152 return NULL;
153 }
154 p += 3;
155 }
156#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
157
158 q = p;
159 while ((ch = *p++) != '\0')
160 {
161 if (IS_DIR_SEPARATOR (ch))
162 {
163 while (IS_DIR_SEPARATOR (*p))
164 p++;
165
166 dirs[num_dirs++] = save_string (q, p - q);
167 if (dirs[num_dirs - 1] == NULL)
168 {
169 dirs[num_dirs] = NULL;
170 free_split_directories (dirs);
171 return NULL;
172 }
173 q = p;
174 }
175 }
176
177 if (p - 1 - q > 0)
178 dirs[num_dirs++] = save_string (q, p - 1 - q);
179 dirs[num_dirs] = NULL;
180
181 if (dirs[num_dirs - 1] == NULL)
182 {
183 free_split_directories (dirs);
184 return NULL;
185 }
186
187 if (ptr_num_dirs)
188 *ptr_num_dirs = num_dirs;
189 return dirs;
190}
191
192/* Release storage held by split directories. */
193
194static void
195free_split_directories (dirs)
196 char **dirs;
197{
198 int i = 0;
199
200 while (dirs[i] != NULL)
201 free (dirs[i++]);
202
203 free ((char *) dirs);
204}
205
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.
209
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/.
213
214 If no relative prefix can be found, return NULL. */
215
216char *
217make_relative_prefix (progname, bin_prefix, prefix)
218 const char *progname;
219 const char *bin_prefix;
220 const char *prefix;
221{
222 char **prog_dirs, **bin_dirs, **prefix_dirs;
223 int prog_num, bin_num, prefix_num;
224 int i, n, common;
225 int needed_len;
226 char *ret, *ptr;
227
228 if (progname == NULL || bin_prefix == NULL || prefix == NULL)
229 return NULL;
230
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)
234 return NULL;
235
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. */
238 if (prog_num == 1)
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 {
281 free_split_directories (prog_dirs);
282 progname = nstore;
283 prog_dirs = split_directories (progname, &prog_num);
284 if (prog_dirs == NULL)
285 {
286 free_split_directories (bin_dirs);
287 return NULL;
288 }
289 break;
290 }
291
292 if (*endp == 0)
293 break;
294 endp = startp = endp + 1;
295 }
296 else
297 endp++;
298 }
299 }
300 }
301
302 /* Remove the program name from comparison of directory names. */
303 prog_num--;
304
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
308 we can do. */
309 if (prog_num == bin_num)
310 {
311 for (i = 0; i < bin_num; i++)
312 {
313 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
314 break;
315 }
316
317 if (prog_num <= 0 || i == bin_num)
318 {
319 free_split_directories (prog_dirs);
320 free_split_directories (bin_dirs);
321 prog_dirs = bin_dirs = (char **) 0;
322 return NULL;
323 }
324 }
325
326 prefix_dirs = split_directories (prefix, &prefix_num);
327 if (prefix_dirs == NULL)
328 {
329 free_split_directories (prog_dirs);
330 free_split_directories (bin_dirs);
331 return NULL;
332 }
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)
344 {
345 free_split_directories (prog_dirs);
346 free_split_directories (bin_dirs);
347 free_split_directories (prefix_dirs);
348 return NULL;
349 }
350
351 /* Two passes: first figure out the size of the result string, and
352 then construct it. */
353 needed_len = 0;
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. */
360
361 ret = (char *) malloc (needed_len);
362 if (ret == NULL)
363 return NULL;
364
365 /* Build up the pathnames in argv[0]. */
756954c3 366 *ret = '\0';
2a80c0a4
DD
367 for (i = 0; i < prog_num; i++)
368 strcat (ret, prog_dirs[i]);
369
370 /* Now build up the ..'s. */
371 ptr = ret + strlen(ret);
372 for (i = common; i < bin_num; i++)
373 {
374 strcpy (ptr, DIR_UP);
375 ptr += sizeof (DIR_UP) - 1;
376 *(ptr++) = DIR_SEPARATOR;
377 }
378 *ptr = '\0';
379
380 /* Put in directories to move over to prefix. */
381 for (i = common; i < prefix_num; i++)
382 strcat (ret, prefix_dirs[i]);
383
384 free_split_directories (prog_dirs);
385 free_split_directories (bin_dirs);
386 free_split_directories (prefix_dirs);
387
388 return ret;
389}
This page took 0.049576 seconds and 4 git commands to generate.