missing Changelog
[official-gcc.git] / gcc / file-find.c
blobfcc25612008243a733bf9a702601025da56298a1
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
10 version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "filenames.h"
24 #include "file-find.h"
26 static bool debug = false;
28 void
29 find_file_set_debug(bool debug_state)
31 debug = debug_state;
34 char *
35 find_a_file (struct path_prefix *pprefix, const char *name)
37 char *temp;
38 struct prefix_list *pl;
39 int len = pprefix->max_len + strlen (name) + 1;
41 if (debug)
42 fprintf (stderr, "Looking for '%s'\n", name);
44 #ifdef HOST_EXECUTABLE_SUFFIX
45 len += strlen (HOST_EXECUTABLE_SUFFIX);
46 #endif
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)
56 strcpy (temp, name);
58 if (debug)
59 fprintf (stderr, " - found: absolute path\n");
61 return temp;
64 #ifdef HOST_EXECUTABLE_SUFFIX
65 /* Some systems have a suffix for executable files.
66 So try appending that. */
67 strcpy (temp, name);
68 strcat (temp, HOST_EXECUTABLE_SUFFIX);
70 if (access (temp, X_OK) == 0)
71 return temp;
72 #endif
74 if (debug)
75 fprintf (stderr, " - failed to locate using absolute path\n");
77 else
78 for (pl = pprefix->plist; pl; pl = pl->next)
80 struct stat st;
82 strcpy (temp, pl->prefix);
83 strcat (temp, name);
85 if (stat (temp, &st) >= 0
86 && ! S_ISDIR (st.st_mode)
87 && access (temp, X_OK) == 0)
88 return temp;
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)
98 return temp;
99 #endif
102 if (debug && pprefix->plist == NULL)
103 fprintf (stderr, " - failed: no entries in prefix list\n");
105 free (temp);
106 return 0;
109 /* Add an entry for PREFIX to prefix list PPREFIX. */
111 void
112 add_prefix (struct path_prefix *pprefix, const char *prefix)
114 struct prefix_list *pl, **prev;
115 int len;
117 if (pprefix->plist)
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 /* Take the value of the environment variable ENV, break it into a path, and
143 add of the entries to PPREFIX. */
145 void
146 prefix_from_env (const char *env, struct path_prefix *pprefix)
148 const char *p;
149 p = getenv (env);
151 if (p)
152 prefix_from_string (p, pprefix);
155 void
156 prefix_from_string (const char *p, struct path_prefix *pprefix)
158 const char *startp, *endp;
159 char *nstore = XNEWVEC (char, strlen (p) + 3);
161 if (debug)
162 fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
164 startp = endp = p;
165 while (1)
167 if (*endp == PATH_SEPARATOR || *endp == 0)
169 strncpy (nstore, startp, endp-startp);
170 if (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;
179 else
180 nstore[endp-startp] = 0;
182 if (debug)
183 fprintf (stderr, " - add prefix: %s\n", nstore);
185 add_prefix (pprefix, nstore);
186 if (*endp == 0)
187 break;
188 endp = startp = endp + 1;
190 else
191 endp++;
193 free (nstore);