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