PR rtl-optimization/82913
[official-gcc.git] / gcc / file-find.c
blobb5a1fe8494e88b694df15c9b806c49dfaa938720
1 /* Utility functions for finding files relative to GCC binaries.
2 Copyright (C) 1992-2017 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
9 version.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "filenames.h"
23 #include "file-find.h"
25 static bool debug = false;
27 void
28 find_file_set_debug (bool debug_state)
30 debug = debug_state;
33 char *
34 find_a_file (struct path_prefix *pprefix, const char *name, int mode)
36 char *temp;
37 struct prefix_list *pl;
38 int len = pprefix->max_len + strlen (name) + 1;
40 if (debug)
41 fprintf (stderr, "Looking for '%s'\n", name);
43 #ifdef HOST_EXECUTABLE_SUFFIX
44 len += strlen (HOST_EXECUTABLE_SUFFIX);
45 #endif
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)
55 strcpy (temp, name);
57 if (debug)
58 fprintf (stderr, " - found: absolute path\n");
60 return temp;
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);
69 if (access (temp, mode) == 0)
70 return temp;
71 #endif
73 if (debug)
74 fprintf (stderr, " - failed to locate using absolute path\n");
76 else
77 for (pl = pprefix->plist; pl; pl = pl->next)
79 struct stat st;
81 strcpy (temp, pl->prefix);
82 strcat (temp, name);
84 if (stat (temp, &st) >= 0
85 && ! S_ISDIR (st.st_mode)
86 && access (temp, mode) == 0)
87 return temp;
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)
97 return temp;
98 #endif
101 if (debug && pprefix->plist == NULL)
102 fprintf (stderr, " - failed: no entries in prefix list\n");
104 free (temp);
105 return 0;
108 /* Add an entry for PREFIX to prefix list PREFIX.
109 Add at beginning if FIRST is true. */
111 void
112 do_add_prefix (struct path_prefix *pprefix, const char *prefix, bool first)
114 struct prefix_list *pl, **prev;
115 int len;
117 if (pprefix->plist && !first)
119 for (pl = pprefix->plist; pl->next; pl = pl->next)
121 prev = &pl->next;
123 else
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);
135 if (*prev)
136 pl->next = *prev;
137 else
138 pl->next = (struct prefix_list *) 0;
139 *prev = pl;
142 /* Add an entry for PREFIX at the end of prefix list PREFIX. */
144 void
145 add_prefix (struct path_prefix *pprefix, const char *prefix)
147 do_add_prefix (pprefix, prefix, false);
150 /* Add an entry for PREFIX at the begin of prefix list PREFIX. */
152 void
153 add_prefix_begin (struct path_prefix *pprefix, const char *prefix)
155 do_add_prefix (pprefix, prefix, true);
158 /* Take the value of the environment variable ENV, break it into a path, and
159 add of the entries to PPREFIX. */
161 void
162 prefix_from_env (const char *env, struct path_prefix *pprefix)
164 const char *p;
165 p = getenv (env);
167 if (p)
168 prefix_from_string (p, pprefix);
171 void
172 prefix_from_string (const char *p, struct path_prefix *pprefix)
174 const char *startp, *endp;
175 char *nstore = XNEWVEC (char, strlen (p) + 3);
177 if (debug)
178 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
180 startp = endp = p;
181 while (1)
183 if (*endp == PATH_SEPARATOR || *endp == 0)
185 strncpy (nstore, startp, endp-startp);
186 if (endp == startp)
188 strcpy (nstore, "./");
190 else if (! IS_DIR_SEPARATOR (endp[-1]))
192 nstore[endp-startp] = DIR_SEPARATOR;
193 nstore[endp-startp+1] = 0;
195 else
196 nstore[endp-startp] = 0;
198 if (debug)
199 fprintf (stderr, " - add prefix: %s\n", nstore);
201 add_prefix (pprefix, nstore);
202 if (*endp == 0)
203 break;
204 endp = startp = endp + 1;
206 else
207 endp++;
209 free (nstore);