* hppa.h (ldwa, ldda): Add ordered opcodes.
[binutils.git] / libiberty / make-relative-prefix.c
blobc208cdce0243461891d7f3c1f761a1fa11d88f18
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
10 version.
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.
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. */
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}.
37 @end deftypefn
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
45 #ifdef HAVE_STDLIB_H
46 #include <stdlib.h>
47 #endif
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
52 #include <string.h>
54 #include "ansidecl.h"
55 #include "libiberty.h"
57 #ifndef R_OK
58 #define R_OK 4
59 #define W_OK 2
60 #define X_OK 1
61 #endif
63 #ifndef DIR_SEPARATOR
64 # define DIR_SEPARATOR '/'
65 #endif
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 '\\'
74 # endif
75 # define PATH_SEPARATOR ';'
76 #else
77 # define PATH_SEPARATOR ':'
78 #endif
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
87 #define DIR_UP ".."
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 **));
93 static char *
94 save_string (s, len)
95 const char *s;
96 int len;
98 char *result = malloc (len + 1);
100 memcpy (result, s, len);
101 result[len] = 0;
102 return result;
105 /* Split a filename into component directories. */
107 static char **
108 split_directories (name, ptr_num_dirs)
109 const char *name;
110 int *ptr_num_dirs;
112 int num_dirs = 0;
113 char **dirs;
114 const char *p, *q;
115 int ch;
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]))
123 p += 3;
124 num_dirs++;
126 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
128 while ((ch = *p++) != '\0')
130 if (IS_DIR_SEPARATOR (ch))
132 num_dirs++;
133 while (IS_DIR_SEPARATOR (*p))
134 p++;
138 dirs = (char **) malloc (sizeof (char *) * (num_dirs + 2));
139 if (dirs == NULL)
140 return NULL;
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]))
148 dirs[num_dirs++] = save_string (p, 3);
149 if (dirs[num_dirs - 1] == NULL)
151 free (dirs);
152 return NULL;
154 p += 3;
156 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
158 q = p;
159 while ((ch = *p++) != '\0')
161 if (IS_DIR_SEPARATOR (ch))
163 while (IS_DIR_SEPARATOR (*p))
164 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);
171 return NULL;
173 q = p;
177 if (p - 1 - q > 0)
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);
184 return NULL;
187 if (ptr_num_dirs)
188 *ptr_num_dirs = num_dirs;
189 return dirs;
192 /* Release storage held by split directories. */
194 static void
195 free_split_directories (dirs)
196 char **dirs;
198 int i = 0;
200 while (dirs[i] != NULL)
201 free (dirs[i++]);
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. */
216 char *
217 make_relative_prefix (progname, bin_prefix, prefix)
218 const char *progname;
219 const char *bin_prefix;
220 const char *prefix;
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;
228 if (progname == NULL || bin_prefix == NULL || prefix == NULL)
229 return 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)
234 return 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. */
238 if (prog_num == 1)
240 char *temp;
242 temp = getenv ("PATH");
243 if (temp)
245 char *startp, *endp, *nstore;
246 size_t prefixlen = strlen (temp) + 1;
247 if (prefixlen < 2)
248 prefixlen = 2;
250 nstore = (char *) alloca (prefixlen + strlen (progname) + 1);
252 startp = endp = temp;
253 while (1)
255 if (*endp == PATH_SEPARATOR || *endp == 0)
257 if (endp == startp)
259 nstore[0] = '.';
260 nstore[1] = DIR_SEPARATOR;
261 nstore[2] = '\0';
263 else
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;
271 else
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)
278 #endif
281 free_split_directories (prog_dirs);
282 progname = nstore;
283 prog_dirs = split_directories (progname, &prog_num);
284 if (prog_dirs == NULL)
286 free_split_directories (bin_dirs);
287 return NULL;
289 break;
292 if (*endp == 0)
293 break;
294 endp = startp = endp + 1;
296 else
297 endp++;
302 /* Remove the program name from comparison of directory names. */
303 prog_num--;
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)
311 for (i = 0; i < bin_num; i++)
313 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
314 break;
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;
322 return NULL;
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);
331 return NULL;
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)
339 break;
342 /* If there are no common directories, there can be no relative prefix. */
343 if (common == 0)
345 free_split_directories (prog_dirs);
346 free_split_directories (bin_dirs);
347 free_split_directories (prefix_dirs);
348 return NULL;
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. */
361 ret = (char *) malloc (needed_len);
362 if (ret == NULL)
363 return NULL;
365 /* Build up the pathnames in argv[0]. */
366 *ret = '\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;
378 *ptr = '\0';
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);
388 return ret;