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