file-find.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Utility functions for finding files relative to GCC binaries.
  2. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  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 3, 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 COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "filenames.h"
  18. #include "file-find.h"
  19. static bool debug = false;
  20. void
  21. find_file_set_debug (bool debug_state)
  22. {
  23. debug = debug_state;
  24. }
  25. char *
  26. find_a_file (struct path_prefix *pprefix, const char *name, int mode)
  27. {
  28. char *temp;
  29. struct prefix_list *pl;
  30. int len = pprefix->max_len + strlen (name) + 1;
  31. if (debug)
  32. fprintf (stderr, "Looking for '%s'\n", name);
  33. #ifdef HOST_EXECUTABLE_SUFFIX
  34. len += strlen (HOST_EXECUTABLE_SUFFIX);
  35. #endif
  36. temp = XNEWVEC (char, len);
  37. /* Determine the filename to execute (special case for absolute paths). */
  38. if (IS_ABSOLUTE_PATH (name))
  39. {
  40. if (access (name, mode) == 0)
  41. {
  42. strcpy (temp, name);
  43. if (debug)
  44. fprintf (stderr, " - found: absolute path\n");
  45. return temp;
  46. }
  47. #ifdef HOST_EXECUTABLE_SUFFIX
  48. /* Some systems have a suffix for executable files.
  49. So try appending that. */
  50. strcpy (temp, name);
  51. strcat (temp, HOST_EXECUTABLE_SUFFIX);
  52. if (access (temp, mode) == 0)
  53. return temp;
  54. #endif
  55. if (debug)
  56. fprintf (stderr, " - failed to locate using absolute path\n");
  57. }
  58. else
  59. for (pl = pprefix->plist; pl; pl = pl->next)
  60. {
  61. struct stat st;
  62. strcpy (temp, pl->prefix);
  63. strcat (temp, name);
  64. if (stat (temp, &st) >= 0
  65. && ! S_ISDIR (st.st_mode)
  66. && access (temp, mode) == 0)
  67. return temp;
  68. #ifdef HOST_EXECUTABLE_SUFFIX
  69. /* Some systems have a suffix for executable files.
  70. So try appending that. */
  71. strcat (temp, HOST_EXECUTABLE_SUFFIX);
  72. if (stat (temp, &st) >= 0
  73. && ! S_ISDIR (st.st_mode)
  74. && access (temp, mode) == 0)
  75. return temp;
  76. #endif
  77. }
  78. if (debug && pprefix->plist == NULL)
  79. fprintf (stderr, " - failed: no entries in prefix list\n");
  80. free (temp);
  81. return 0;
  82. }
  83. /* Add an entry for PREFIX to prefix list PREFIX.
  84. Add at beginning if FIRST is true. */
  85. void
  86. do_add_prefix (struct path_prefix *pprefix, const char *prefix, bool first)
  87. {
  88. struct prefix_list *pl, **prev;
  89. int len;
  90. if (pprefix->plist && !first)
  91. {
  92. for (pl = pprefix->plist; pl->next; pl = pl->next)
  93. ;
  94. prev = &pl->next;
  95. }
  96. else
  97. prev = &pprefix->plist;
  98. /* Keep track of the longest prefix. */
  99. len = strlen (prefix);
  100. if (len > pprefix->max_len)
  101. pprefix->max_len = len;
  102. pl = XNEW (struct prefix_list);
  103. pl->prefix = xstrdup (prefix);
  104. if (*prev)
  105. pl->next = *prev;
  106. else
  107. pl->next = (struct prefix_list *) 0;
  108. *prev = pl;
  109. }
  110. /* Add an entry for PREFIX at the end of prefix list PREFIX. */
  111. void
  112. add_prefix (struct path_prefix *pprefix, const char *prefix)
  113. {
  114. do_add_prefix (pprefix, prefix, false);
  115. }
  116. /* Add an entry for PREFIX at the begin of prefix list PREFIX. */
  117. void
  118. add_prefix_begin (struct path_prefix *pprefix, const char *prefix)
  119. {
  120. do_add_prefix (pprefix, prefix, true);
  121. }
  122. /* Take the value of the environment variable ENV, break it into a path, and
  123. add of the entries to PPREFIX. */
  124. void
  125. prefix_from_env (const char *env, struct path_prefix *pprefix)
  126. {
  127. const char *p;
  128. p = getenv (env);
  129. if (p)
  130. prefix_from_string (p, pprefix);
  131. }
  132. void
  133. prefix_from_string (const char *p, struct path_prefix *pprefix)
  134. {
  135. const char *startp, *endp;
  136. char *nstore = XNEWVEC (char, strlen (p) + 3);
  137. if (debug)
  138. fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
  139. startp = endp = p;
  140. while (1)
  141. {
  142. if (*endp == PATH_SEPARATOR || *endp == 0)
  143. {
  144. strncpy (nstore, startp, endp-startp);
  145. if (endp == startp)
  146. {
  147. strcpy (nstore, "./");
  148. }
  149. else if (! IS_DIR_SEPARATOR (endp[-1]))
  150. {
  151. nstore[endp-startp] = DIR_SEPARATOR;
  152. nstore[endp-startp+1] = 0;
  153. }
  154. else
  155. nstore[endp-startp] = 0;
  156. if (debug)
  157. fprintf (stderr, " - add prefix: %s\n", nstore);
  158. add_prefix (pprefix, nstore);
  159. if (*endp == 0)
  160. break;
  161. endp = startp = endp + 1;
  162. }
  163. else
  164. endp++;
  165. }
  166. free (nstore);
  167. }