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