make-relative-prefix.c 11 KB

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