fcc25612008243a733bf9a702601025da56298a1
[platform/upstream/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
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "filenames.h"
24 #include "file-find.h"
25
26 static bool debug = false;
27
28 void
29 find_file_set_debug(bool debug_state)
30 {
31   debug = debug_state;
32 }
33
34 char *
35 find_a_file (struct path_prefix *pprefix, const char *name)
36 {
37   char *temp;
38   struct prefix_list *pl;
39   int len = pprefix->max_len + strlen (name) + 1;
40
41   if (debug)
42     fprintf (stderr, "Looking for '%s'\n", name);
43
44 #ifdef HOST_EXECUTABLE_SUFFIX
45   len += strlen (HOST_EXECUTABLE_SUFFIX);
46 #endif
47
48   temp = XNEWVEC (char, len);
49
50   /* Determine the filename to execute (special case for absolute paths).  */
51
52   if (IS_ABSOLUTE_PATH (name))
53     {
54       if (access (name, X_OK) == 0)
55         {
56           strcpy (temp, name);
57
58           if (debug)
59             fprintf (stderr, "  - found: absolute path\n");
60
61           return temp;
62         }
63
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);
69
70         if (access (temp, X_OK) == 0)
71           return temp;
72 #endif
73
74       if (debug)
75         fprintf (stderr, "  - failed to locate using absolute path\n");
76     }
77   else
78     for (pl = pprefix->plist; pl; pl = pl->next)
79       {
80         struct stat st;
81
82         strcpy (temp, pl->prefix);
83         strcat (temp, name);
84
85         if (stat (temp, &st) >= 0
86             && ! S_ISDIR (st.st_mode)
87             && access (temp, X_OK) == 0)
88           return temp;
89
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);
94
95         if (stat (temp, &st) >= 0
96             && ! S_ISDIR (st.st_mode)
97             && access (temp, X_OK) == 0)
98           return temp;
99 #endif
100       }
101
102   if (debug && pprefix->plist == NULL)
103     fprintf (stderr, "  - failed: no entries in prefix list\n");
104
105   free (temp);
106   return 0;
107 }
108
109 /* Add an entry for PREFIX to prefix list PPREFIX.  */
110
111 void
112 add_prefix (struct path_prefix *pprefix, const char *prefix)
113 {
114   struct prefix_list *pl, **prev;
115   int len;
116
117   if (pprefix->plist)
118     {
119       for (pl = pprefix->plist; pl->next; pl = pl->next)
120         ;
121       prev = &pl->next;
122     }
123   else
124     prev = &pprefix->plist;
125
126   /* Keep track of the longest prefix.  */
127
128   len = strlen (prefix);
129   if (len > pprefix->max_len)
130     pprefix->max_len = len;
131
132   pl = XNEW (struct prefix_list);
133   pl->prefix = xstrdup (prefix);
134
135   if (*prev)
136     pl->next = *prev;
137   else
138     pl->next = (struct prefix_list *) 0;
139   *prev = pl;
140 }
141
142 /* Take the value of the environment variable ENV, break it into a path, and
143    add of the entries to PPREFIX.  */
144
145 void
146 prefix_from_env (const char *env, struct path_prefix *pprefix)
147 {
148   const char *p;
149   p = getenv (env);
150
151   if (p)
152     prefix_from_string (p, pprefix);
153 }
154
155 void
156 prefix_from_string (const char *p, struct path_prefix *pprefix)
157 {
158   const char *startp, *endp;
159   char *nstore = XNEWVEC (char, strlen (p) + 3);
160
161   if (debug)
162     fprintf (stderr, "Convert string '%s' into prefixes, separator = '%c'\n", p, PATH_SEPARATOR);
163
164   startp = endp = p;
165   while (1)
166     {
167       if (*endp == PATH_SEPARATOR || *endp == 0)
168         {
169           strncpy (nstore, startp, endp-startp);
170           if (endp == startp)
171             {
172               strcpy (nstore, "./");
173             }
174           else if (! IS_DIR_SEPARATOR (endp[-1]))
175             {
176               nstore[endp-startp] = DIR_SEPARATOR;
177               nstore[endp-startp+1] = 0;
178             }
179           else
180             nstore[endp-startp] = 0;
181
182           if (debug)
183             fprintf (stderr, "  - add prefix: %s\n", nstore);
184
185           add_prefix (pprefix, nstore);
186           if (*endp == 0)
187             break;
188           endp = startp = endp + 1;
189         }
190       else
191         endp++;
192     }
193   free (nstore);
194 }