annotate gcc/file-find.c @ 16:04ced10e8804

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