]> git.sur5r.net Git - cc65/blob - src/common/searchpath.c
2f536388bca3ecd9c66d6c4273b06978aa34d4dd
[cc65] / src / common / searchpath.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                               searchpath.h                                */
4 /*                                                                           */
5 /*                    Search path path handling for ld65                     */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #if defined(_MSC_VER)
39 /* Microsoft compiler */
40 #  include <io.h>
41 #else
42 /* Anyone else */
43 #  include <unistd.h>
44 #endif
45
46 /* common */
47 #include "searchpath.h"
48 #include "strbuf.h"
49 #include "xmalloc.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 static char* SearchPaths[MAX_SEARCH_PATHS];
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 static char* Add (char* Orig, const char* New)
70 /* Create a new path from Orig and New, delete Orig, return the result */
71 {
72     unsigned OrigLen, NewLen;
73     char* NewPath;
74
75     /* Get the length of the original string */
76     OrigLen = Orig? strlen (Orig) : 0;
77
78     /* Get the length of the new path */
79     NewLen = strlen (New);
80
81     /* Check for a trailing path separator and remove it */
82     if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
83         --NewLen;
84     }
85
86     /* Allocate memory for the new string */
87     NewPath = (char*) xmalloc (OrigLen + NewLen + 2);
88
89     /* Copy the strings */
90     memcpy (NewPath, Orig, OrigLen);
91     memcpy (NewPath+OrigLen, New, NewLen);
92     NewPath [OrigLen+NewLen+0] = ';';
93     NewPath [OrigLen+NewLen+1] = '\0';
94
95     /* Delete the original path */
96     xfree (Orig);
97
98     /* Return the new path */
99     return NewPath;
100 }
101
102
103
104 static char* Find (const char* Path, const char* File)
105 /* Search for a file in a list of directories. If found, return the complete
106  * name including the path in a malloced data area, if not found, return 0.
107  */
108 {
109     const char* P;
110     StrBuf PathName = AUTO_STRBUF_INITIALIZER;
111
112     /* Initialize variables */
113     P = Path;
114
115     /* Handle a NULL pointer as replacement for an empty string */
116     if (P == 0) {
117         P = "";
118     }
119
120     /* Start the search */
121     while (*P) {
122         /* Clear the string buffer */
123         SB_Clear (&PathName);
124
125         /* Copy the next path element into the buffer */
126         while (*P != '\0' && *P != ';') {
127             SB_AppendChar (&PathName, *P++);
128         }
129
130         /* Add a path separator and the filename */
131         if (SB_NotEmpty (&PathName)) {
132             SB_AppendChar (&PathName, '/');
133         }
134         SB_AppendStr (&PathName, File);
135         SB_Terminate (&PathName);
136
137         /* Check if this file exists */
138         if (access (SB_GetBuf (&PathName), 0) == 0) {
139             /* The file exists, return its name */
140             char* Name = xstrdup (SB_GetBuf (&PathName));
141             SB_Done (&PathName);
142             return Name;
143         }
144
145         /* Skip a list separator if we have one */
146         if (*P == ';') {
147             ++P;
148         }
149     }
150
151     /* Not found */
152     SB_Done (&PathName);
153     return 0;
154 }
155
156
157
158 void AddSearchPath (const char* NewPath, unsigned Where)
159 /* Add a new search path to the existing one */
160 {
161     /* Allow a NULL path */
162     if (NewPath) {
163         unsigned I;
164         for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
165             unsigned Mask = (0x01U << I);
166             if (Where & Mask) {
167                 SearchPaths[I] = Add (SearchPaths[I], NewPath);
168             }
169         }
170     }
171 }
172
173
174
175 void AddSearchPathFromEnv (const char* EnvVar, unsigned Where)
176 /* Add a search path from an environment variable */
177 {
178     AddSearchPath (getenv (EnvVar), Where);
179 }
180
181
182
183 void AddSubSearchPathFromEnv (const char* EnvVar, const char* SubDir, unsigned Where)
184 /* Add a search path from an environment variable, adding a subdirectory to
185  * the environment variable value.
186  */
187 {
188     StrBuf Dir = AUTO_STRBUF_INITIALIZER;
189
190     const char* EnvVal = getenv (EnvVar);
191     if (EnvVal == 0) {
192         /* Not found */
193         return;
194     }
195
196     /* Copy the environment variable to the buffer */
197     SB_CopyStr (&Dir, EnvVal);
198
199     /* Add a path separator if necessary */
200     if (SB_NotEmpty (&Dir)) {
201         if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
202             SB_AppendChar (&Dir, '/');
203         }
204     }
205
206     /* Add the subdirectory */
207     SB_AppendStr (&Dir, SubDir);
208
209     /* Terminate the string */
210     SB_Terminate (&Dir);
211
212     /* Add the search path */
213     AddSearchPath (SB_GetConstBuf (&Dir), Where);
214
215     /* Free the temp buffer */
216     SB_Done (&Dir);
217 }
218
219
220
221 void ForgetAllSearchPaths (unsigned Where)
222 /* Forget all search paths in the given lists. */
223 {
224     unsigned I;
225     for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
226         unsigned Mask = (0x01U << I);
227         if (Where & Mask) {
228             xfree (SearchPaths[I]);
229             SearchPaths[I] = 0;
230         }
231     }
232 }
233
234
235
236 char* SearchFile (const char* Name, unsigned Where)
237 /* Search for a file in a list of directories. Return a pointer to a malloced
238  * area that contains the complete path, if found, return 0 otherwise.
239  */
240 {
241     unsigned I;
242     for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
243         unsigned Mask = (0x01U << I);
244         if (Where & Mask) {
245             char* Path = Find (SearchPaths[I], Name);
246             if (Path) {
247                 /* Found the file */
248                 return Path;
249             }
250         }
251     }
252     return 0;
253 }
254
255
256