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