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