1 /* Utility functions for finding files relative to GCC binaries.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "filenames.h"
24 #include "file-find.h"
26 static bool debug = false;
29 find_file_set_debug(bool debug_state)
35 find_a_file (struct path_prefix *pprefix, const char *name)
38 struct prefix_list *pl;
39 int len = pprefix->max_len + strlen (name) + 1;
42 fprintf (stderr, "Looking for '%s'\n", name);
44 #ifdef HOST_EXECUTABLE_SUFFIX
45 len += strlen (HOST_EXECUTABLE_SUFFIX);
48 temp = XNEWVEC (char, len);
50 /* Determine the filename to execute (special case for absolute paths). */
52 if (IS_ABSOLUTE_PATH (name))
54 if (access (name, X_OK) == 0)
59 fprintf (stderr, " - found: absolute path\n");
64 #ifdef HOST_EXECUTABLE_SUFFIX
65 /* Some systems have a suffix for executable files.
66 So try appending that. */
68 strcat (temp, HOST_EXECUTABLE_SUFFIX);
70 if (access (temp, X_OK) == 0)
75 fprintf (stderr, " - failed to locate using absolute path\n");
78 for (pl = pprefix->plist; pl; pl = pl->next)
82 strcpy (temp, pl->prefix);
85 if (stat (temp, &st) >= 0
86 && ! S_ISDIR (st.st_mode)
87 && access (temp, X_OK) == 0)
90 #ifdef HOST_EXECUTABLE_SUFFIX
91 /* Some systems have a suffix for executable files.
92 So try appending that. */
93 strcat (temp, HOST_EXECUTABLE_SUFFIX);
95 if (stat (temp, &st) >= 0
96 && ! S_ISDIR (st.st_mode)
97 && access (temp, X_OK) == 0)
102 if (debug && pprefix->plist == NULL)
103 fprintf (stderr, " - failed: no entries in prefix list\n");
109 /* Add an entry for PREFIX to prefix list PPREFIX. */
112 add_prefix (struct path_prefix *pprefix, const char *prefix)
114 struct prefix_list *pl, **prev;
119 for (pl = pprefix->plist; pl->next; pl = pl->next)
124 prev = &pprefix->plist;
126 /* Keep track of the longest prefix. */
128 len = strlen (prefix);
129 if (len > pprefix->max_len)
130 pprefix->max_len = len;
132 pl = XNEW (struct prefix_list);
133 pl->prefix = xstrdup (prefix);
138 pl->next = (struct prefix_list *) 0;
142 /* Take the value of the environment variable ENV, break it into a path, and
143 add of the entries to PPREFIX. */
146 prefix_from_env (const char *env, struct path_prefix *pprefix)
152 prefix_from_string (p, pprefix);
156 prefix_from_string (const char *p, struct path_prefix *pprefix)
158 const char *startp, *endp;
159 char *nstore = XNEWVEC (char, strlen (p) + 3);
162 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
167 if (*endp == PATH_SEPARATOR || *endp == 0)
169 strncpy (nstore, startp, endp-startp);
172 strcpy (nstore, "./");
174 else if (! IS_DIR_SEPARATOR (endp[-1]))
176 nstore[endp-startp] = DIR_SEPARATOR;
177 nstore[endp-startp+1] = 0;
180 nstore[endp-startp] = 0;
183 fprintf (stderr, " - add prefix: %s\n", nstore);
185 add_prefix (pprefix, nstore);
188 endp = startp = endp + 1;