1 /*****************************************************************************/
5 /* Search path path handling for ld65 */
9 /* (C) 2000-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
39 /* Microsoft compiler */
47 #include "searchpath.h"
53 /*****************************************************************************/
55 /*****************************************************************************/
59 static char* SearchPaths[MAX_SEARCH_PATHS];
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static char* Add (char* Orig, const char* New)
70 /* Create a new path from Orig and New, delete Orig, return the result */
72 unsigned OrigLen, NewLen;
75 /* Get the length of the original string */
76 OrigLen = Orig? strlen (Orig) : 0;
78 /* Get the length of the new path */
79 NewLen = strlen (New);
81 /* Check for a trailing path separator and remove it */
82 if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
86 /* Allocate memory for the new string */
87 NewPath = (char*) xmalloc (OrigLen + NewLen + 2);
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';
95 /* Delete the original path */
98 /* Return the new path */
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.
110 StrBuf PathName = AUTO_STRBUF_INITIALIZER;
112 /* Initialize variables */
115 /* Handle a NULL pointer as replacement for an empty string */
120 /* Start the search */
122 /* Clear the string buffer */
123 SB_Clear (&PathName);
125 /* Copy the next path element into the buffer */
126 while (*P != '\0' && *P != ';') {
127 SB_AppendChar (&PathName, *P++);
130 /* Add a path separator and the filename */
131 if (SB_NotEmpty (&PathName)) {
132 SB_AppendChar (&PathName, '/');
134 SB_AppendStr (&PathName, File);
135 SB_Terminate (&PathName);
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));
145 /* Skip a list separator if we have one */
158 void AddSearchPath (const char* NewPath, unsigned Where)
159 /* Add a new search path to the existing one */
161 /* Allow a NULL path */
164 for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
165 unsigned Mask = (0x01U << I);
167 SearchPaths[I] = Add (SearchPaths[I], NewPath);
175 void AddSearchPathFromEnv (const char* EnvVar, unsigned Where)
176 /* Add a search from an environment variable */
178 AddSearchPath (getenv (EnvVar), Where);
183 void ForgetAllSearchPaths (unsigned Where)
184 /* Forget all search paths in the given lists. */
187 for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
188 unsigned Mask = (0x01U << I);
190 xfree (SearchPaths[I]);
198 char* SearchFile (const char* Name, unsigned Where)
199 /* Search for a file in a list of directories. Return a pointer to a malloced
200 * area that contains the complete path, if found, return 0 otherwise.
204 for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
205 unsigned Mask = (0x01U << I);
207 char* Path = Find (SearchPaths[I], Name);