1 /* Utility functions for finding files relative to GCC binaries.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
22 #include "filenames.h"
23 #include "file-find.h"
25 static bool debug = false;
28 find_file_set_debug(bool debug_state)
34 find_a_file (struct path_prefix *pprefix, const char *name, int mode)
37 struct prefix_list *pl;
38 int len = pprefix->max_len + strlen (name) + 1;
41 fprintf (stderr, "Looking for '%s'\n", name);
43 #ifdef HOST_EXECUTABLE_SUFFIX
44 len += strlen (HOST_EXECUTABLE_SUFFIX);
47 temp = XNEWVEC (char, len);
49 /* Determine the filename to execute (special case for absolute paths). */
51 if (IS_ABSOLUTE_PATH (name))
53 if (access (name, mode) == 0)
58 fprintf (stderr, " - found: absolute path\n");
63 #ifdef HOST_EXECUTABLE_SUFFIX
64 /* Some systems have a suffix for executable files.
65 So try appending that. */
67 strcat (temp, HOST_EXECUTABLE_SUFFIX);
69 if (access (temp, mode) == 0)
74 fprintf (stderr, " - failed to locate using absolute path\n");
77 for (pl = pprefix->plist; pl; pl = pl->next)
81 strcpy (temp, pl->prefix);
84 if (stat (temp, &st) >= 0
85 && ! S_ISDIR (st.st_mode)
86 && access (temp, mode) == 0)
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);
94 if (stat (temp, &st) >= 0
95 && ! S_ISDIR (st.st_mode)
96 && access (temp, mode) == 0)
101 if (debug && pprefix->plist == NULL)
102 fprintf (stderr, " - failed: no entries in prefix list\n");
108 /* Add an entry for PREFIX to prefix list PPREFIX. */
111 add_prefix (struct path_prefix *pprefix, const char *prefix)
113 struct prefix_list *pl, **prev;
118 for (pl = pprefix->plist; pl->next; pl = pl->next)
123 prev = &pprefix->plist;
125 /* Keep track of the longest prefix. */
127 len = strlen (prefix);
128 if (len > pprefix->max_len)
129 pprefix->max_len = len;
131 pl = XNEW (struct prefix_list);
132 pl->prefix = xstrdup (prefix);
137 pl->next = (struct prefix_list *) 0;
141 /* Take the value of the environment variable ENV, break it into a path, and
142 add of the entries to PPREFIX. */
145 prefix_from_env (const char *env, struct path_prefix *pprefix)
151 prefix_from_string (p, pprefix);
155 prefix_from_string (const char *p, struct path_prefix *pprefix)
157 const char *startp, *endp;
158 char *nstore = XNEWVEC (char, strlen (p) + 3);
161 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
166 if (*endp == PATH_SEPARATOR || *endp == 0)
168 strncpy (nstore, startp, endp-startp);
171 strcpy (nstore, "./");
173 else if (! IS_DIR_SEPARATOR (endp[-1]))
175 nstore[endp-startp] = DIR_SEPARATOR;
176 nstore[endp-startp+1] = 0;
179 nstore[endp-startp] = 0;
182 fprintf (stderr, " - add prefix: %s\n", nstore);
184 add_prefix (pprefix, nstore);
187 endp = startp = endp + 1;