make-relative-prefix.c 11 KB

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