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.
6 This file is part of GCC.
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
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
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/>. */
24 #include "filenames.h"
25 #include "file-find.h"
27 static bool debug
= false;
30 find_file_set_debug(bool debug_state
)
36 find_a_file (struct path_prefix
*pprefix
, const char *name
)
39 struct prefix_list
*pl
;
40 int len
= pprefix
->max_len
+ strlen (name
) + 1;
43 fprintf (stderr
, "Looking for '%s'\n", name
);
45 #ifdef HOST_EXECUTABLE_SUFFIX
46 len
+= strlen (HOST_EXECUTABLE_SUFFIX
);
49 temp
= XNEWVEC (char, len
);
51 /* Determine the filename to execute (special case for absolute paths). */
53 if (IS_ABSOLUTE_PATH (name
))
55 if (access (name
, X_OK
) == 0)
60 fprintf (stderr
, " - found: absolute path\n");
65 #ifdef HOST_EXECUTABLE_SUFFIX
66 /* Some systems have a suffix for executable files.
67 So try appending that. */
69 strcat (temp
, HOST_EXECUTABLE_SUFFIX
);
71 if (access (temp
, X_OK
) == 0)
76 fprintf (stderr
, " - failed to locate using absolute path\n");
79 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
83 strcpy (temp
, pl
->prefix
);
86 if (stat (temp
, &st
) >= 0
87 && ! S_ISDIR (st
.st_mode
)
88 && access (temp
, X_OK
) == 0)
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
);
96 if (stat (temp
, &st
) >= 0
97 && ! S_ISDIR (st
.st_mode
)
98 && access (temp
, X_OK
) == 0)
103 if (debug
&& pprefix
->plist
== NULL
)
104 fprintf (stderr
, " - failed: no entries in prefix list\n");
110 /* Add an entry for PREFIX to prefix list PPREFIX. */
113 add_prefix (struct path_prefix
*pprefix
, const char *prefix
)
115 struct prefix_list
*pl
, **prev
;
120 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
125 prev
= &pprefix
->plist
;
127 /* Keep track of the longest prefix. */
129 len
= strlen (prefix
);
130 if (len
> pprefix
->max_len
)
131 pprefix
->max_len
= len
;
133 pl
= XNEW (struct prefix_list
);
134 pl
->prefix
= xstrdup (prefix
);
139 pl
->next
= (struct prefix_list
*) 0;
143 /* Take the value of the environment variable ENV, break it into a path, and
144 add of the entries to PPREFIX. */
147 prefix_from_env (const char *env
, struct path_prefix
*pprefix
)
153 prefix_from_string (p
, pprefix
);
157 prefix_from_string (const char *p
, struct path_prefix
*pprefix
)
159 const char *startp
, *endp
;
160 char *nstore
= XNEWVEC (char, strlen (p
) + 3);
163 fprintf (stderr
, "Convert string '%s' into prefixes, separator = '%c'\n", p
, PATH_SEPARATOR
);
168 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
170 strncpy (nstore
, startp
, endp
-startp
);
173 strcpy (nstore
, "./");
175 else if (! IS_DIR_SEPARATOR (endp
[-1]))
177 nstore
[endp
-startp
] = DIR_SEPARATOR
;
178 nstore
[endp
-startp
+1] = 0;
181 nstore
[endp
-startp
] = 0;
184 fprintf (stderr
, " - add prefix: %s\n", nstore
);
186 add_prefix (pprefix
, nstore
);
189 endp
= startp
= endp
+ 1;