merge from gcc
[deliverable/binutils-gdb.git] / libiberty / make-relative-prefix.c
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
5 This file is part of libiberty.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-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
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}.
29
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/}.
33
34 The return value is normally allocated via @code{malloc}. If no relative prefix
35 can 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
70 # define HOST_EXECUTABLE_SUFFIX ".exe"
71 # ifndef DIR_SEPARATOR_2
72 # define DIR_SEPARATOR_2 '\\'
73 # endif
74 # define PATH_SEPARATOR ';'
75 #else
76 # define PATH_SEPARATOR ':'
77 #endif
78
79 #ifndef DIR_SEPARATOR_2
80 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
81 #else
82 # define IS_DIR_SEPARATOR(ch) \
83 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
84 #endif
85
86 #define DIR_UP ".."
87
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 **));
91
92 static char *
93 save_string (s, len)
94 const char *s;
95 int len;
96 {
97 char *result = malloc (len + 1);
98
99 memcpy (result, s, len);
100 result[len] = 0;
101 return result;
102 }
103
104 /* Split a filename into component directories. */
105
106 static char **
107 split_directories (name, ptr_num_dirs)
108 const char *name;
109 int *ptr_num_dirs;
110 {
111 int num_dirs = 0;
112 char **dirs;
113 const char *p, *q;
114 int ch;
115
116 /* Count the number of directories. Special case MSDOS disk names as part
117 of the initial directory. */
118 p = name;
119 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
120 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
121 {
122 p += 3;
123 num_dirs++;
124 }
125 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
126
127 while ((ch = *p++) != '\0')
128 {
129 if (IS_DIR_SEPARATOR (ch))
130 {
131 num_dirs++;
132 while (IS_DIR_SEPARATOR (*p))
133 p++;
134 }
135 }
136
137 dirs = (char **) malloc (sizeof (char *) * (num_dirs + 2));
138 if (dirs == NULL)
139 return NULL;
140
141 /* Now copy the directory parts. */
142 num_dirs = 0;
143 p = name;
144 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
145 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
146 {
147 dirs[num_dirs++] = save_string (p, 3);
148 if (dirs[num_dirs - 1] == NULL)
149 {
150 free (dirs);
151 return NULL;
152 }
153 p += 3;
154 }
155 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
156
157 q = p;
158 while ((ch = *p++) != '\0')
159 {
160 if (IS_DIR_SEPARATOR (ch))
161 {
162 while (IS_DIR_SEPARATOR (*p))
163 p++;
164
165 dirs[num_dirs++] = save_string (q, p - q);
166 if (dirs[num_dirs - 1] == NULL)
167 {
168 dirs[num_dirs] = NULL;
169 free_split_directories (dirs);
170 return NULL;
171 }
172 q = p;
173 }
174 }
175
176 if (p - 1 - q > 0)
177 dirs[num_dirs++] = save_string (q, p - 1 - q);
178 dirs[num_dirs] = NULL;
179
180 if (dirs[num_dirs - 1] == NULL)
181 {
182 free_split_directories (dirs);
183 return NULL;
184 }
185
186 if (ptr_num_dirs)
187 *ptr_num_dirs = num_dirs;
188 return dirs;
189 }
190
191 /* Release storage held by split directories. */
192
193 static void
194 free_split_directories (dirs)
195 char **dirs;
196 {
197 int i = 0;
198
199 while (dirs[i] != NULL)
200 free (dirs[i++]);
201
202 free ((char *) dirs);
203 }
204
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.
208
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/.
212
213 If no relative prefix can be found, return NULL. */
214
215 char *
216 make_relative_prefix (progname, bin_prefix, prefix)
217 const char *progname;
218 const char *bin_prefix;
219 const char *prefix;
220 {
221 char **prog_dirs, **bin_dirs, **prefix_dirs;
222 int prog_num, bin_num, prefix_num;
223 int i, n, common;
224 int needed_len;
225 char *ret, *ptr;
226
227 if (progname == NULL || bin_prefix == NULL || prefix == NULL)
228 return NULL;
229
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)
233 return NULL;
234
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. */
237 if (prog_num == 1)
238 {
239 char *temp;
240
241 temp = getenv ("PATH");
242 if (temp)
243 {
244 char *startp, *endp, *nstore;
245 size_t prefixlen = strlen (temp) + 1;
246 if (prefixlen < 2)
247 prefixlen = 2;
248
249 nstore = (char *) alloca (prefixlen + strlen (progname) + 1);
250
251 startp = endp = temp;
252 while (1)
253 {
254 if (*endp == PATH_SEPARATOR || *endp == 0)
255 {
256 if (endp == startp)
257 {
258 nstore[0] = '.';
259 nstore[1] = DIR_SEPARATOR;
260 nstore[2] = '\0';
261 }
262 else
263 {
264 strncpy (nstore, startp, endp - startp);
265 if (! IS_DIR_SEPARATOR (endp[-1]))
266 {
267 nstore[endp - startp] = DIR_SEPARATOR;
268 nstore[endp - startp + 1] = 0;
269 }
270 else
271 nstore[endp - startp] = 0;
272 }
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)
277 #endif
278 )
279 {
280 free_split_directories (prog_dirs);
281 progname = nstore;
282 prog_dirs = split_directories (progname, &prog_num);
283 if (prog_dirs == NULL)
284 {
285 free_split_directories (bin_dirs);
286 return NULL;
287 }
288 break;
289 }
290
291 if (*endp == 0)
292 break;
293 endp = startp = endp + 1;
294 }
295 else
296 endp++;
297 }
298 }
299 }
300
301 /* Remove the program name from comparison of directory names. */
302 prog_num--;
303
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
307 we can do. */
308 if (prog_num == bin_num)
309 {
310 for (i = 0; i < bin_num; i++)
311 {
312 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
313 break;
314 }
315
316 if (prog_num <= 0 || i == bin_num)
317 {
318 free_split_directories (prog_dirs);
319 free_split_directories (bin_dirs);
320 prog_dirs = bin_dirs = (char **) 0;
321 return NULL;
322 }
323 }
324
325 prefix_dirs = split_directories (prefix, &prefix_num);
326 if (prefix_dirs == NULL)
327 {
328 free_split_directories (prog_dirs);
329 free_split_directories (bin_dirs);
330 return NULL;
331 }
332
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++)
336 {
337 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
338 break;
339 }
340
341 /* If there are no common directories, there can be no relative prefix. */
342 if (common == 0)
343 {
344 free_split_directories (prog_dirs);
345 free_split_directories (bin_dirs);
346 free_split_directories (prefix_dirs);
347 return NULL;
348 }
349
350 /* Two passes: first figure out the size of the result string, and
351 then construct it. */
352 needed_len = 0;
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. */
359
360 ret = (char *) malloc (needed_len);
361 if (ret == NULL)
362 return NULL;
363
364 /* Build up the pathnames in argv[0]. */
365 for (i = 0; i < prog_num; i++)
366 strcat (ret, prog_dirs[i]);
367
368 /* Now build up the ..'s. */
369 ptr = ret + strlen(ret);
370 for (i = common; i < bin_num; i++)
371 {
372 strcpy (ptr, DIR_UP);
373 ptr += sizeof (DIR_UP) - 1;
374 *(ptr++) = DIR_SEPARATOR;
375 }
376 *ptr = '\0';
377
378 /* Put in directories to move over to prefix. */
379 for (i = common; i < prefix_num; i++)
380 strcat (ret, prefix_dirs[i]);
381
382 free_split_directories (prog_dirs);
383 free_split_directories (bin_dirs);
384 free_split_directories (prefix_dirs);
385
386 return ret;
387 }
This page took 0.040445 seconds and 5 git commands to generate.