1 /*****************************************************************************/
5 /* Handling of search paths */
9 /* (C) 2000-2013, 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 /*****************************************************************************/
42 /* Microsoft compiler */
51 #include "searchpath.h"
57 /*****************************************************************************/
59 /*****************************************************************************/
63 static char* CleanupPath (const char* Path)
64 /* Prepare and return a clean copy of Path */
69 /* Get the length of the path */
72 /* Check for a trailing path separator and remove it */
73 if (Len > 0 && (Path[Len-1] == '\\' || Path[Len-1] == '/')) {
77 /* Allocate memory for the new string */
78 NewPath = (char*) xmalloc (Len + 1);
80 /* Copy the path and terminate it, then return the copy */
81 memcpy (NewPath, Path, Len);
88 static void Add (SearchPaths* P, const char* New)
89 /* Cleanup a new search path and add it to the list */
91 /* Add a clean copy of the path to the collection */
92 CollAppend (P, CleanupPath (New));
97 SearchPaths* NewSearchPath (void)
98 /* Create a new, empty search path list */
100 return NewCollection ();
105 void AddSearchPath (SearchPaths* P, const char* NewPath)
106 /* Add a new search path to the end of an existing list */
108 /* Allow a NULL path */
116 void AddSearchPathFromEnv (SearchPaths* P, const char* EnvVar)
117 /* Add a search path from an environment variable to the end of an existing
121 AddSearchPath (P, getenv (EnvVar));
126 void AddSubSearchPathFromEnv (SearchPaths* P, const char* EnvVar, const char* SubDir)
127 /* Add a search path from an environment variable, adding a subdirectory to
128 ** the environment variable value.
131 StrBuf Dir = AUTO_STRBUF_INITIALIZER;
133 const char* EnvVal = getenv (EnvVar);
139 /* Copy the environment variable to the buffer */
140 SB_CopyStr (&Dir, EnvVal);
142 /* Add a path separator if necessary */
143 if (SB_NotEmpty (&Dir)) {
144 if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
145 SB_AppendChar (&Dir, '/');
149 /* Add the subdirectory and terminate the string */
150 SB_AppendStr (&Dir, SubDir);
153 /* Add the search path */
154 AddSearchPath (P, SB_GetConstBuf (&Dir));
156 /* Free the temp buffer */
162 void AddSubSearchPathFromWinBin (SearchPaths* P, const char* SubDir)
165 ** Add a search path from the running binary, adding a subdirectory to
166 ** the parent directory of the directory containing the binary.
173 if (GetModuleFileName (NULL, Dir, _MAX_PATH) == 0) {
177 /* Remove binary name */
178 Ptr = strrchr (Dir, '\\');
184 /* Check for 'bin' directory */
185 Ptr = strrchr (Dir, '\\');
189 if (strcmp (Ptr++, "\\bin") != 0) {
194 strcpy (Ptr, SubDir);
196 /* Add the search path */
197 AddSearchPath (P, Dir);
208 int PushSearchPath (SearchPaths* P, const char* NewPath)
209 /* Add a new search path to the head of an existing search path list, provided
210 ** that it's not already there. If the path is already at the first position,
211 ** return zero, otherwise return a non zero value.
214 /* Generate a clean copy of NewPath */
215 char* Path = CleanupPath (NewPath);
217 /* If we have paths, check if Path is already at position zero */
218 if (CollCount (P) > 0 && strcmp (CollConstAt (P, 0), Path) == 0) {
219 /* Match. Delete the copy and return to the caller */
224 /* Insert a clean copy of the path at position 0, return success */
225 CollInsert (P, Path, 0);
231 void PopSearchPath (SearchPaths* P)
232 /* Remove a search path from the head of an existing search path list */
234 /* Remove the path at position 0 */
235 xfree (CollAt (P, 0));
241 char* GetSearchPath (SearchPaths* P, unsigned Index)
242 /* Return the search path at the given index, if the index is valid, return an
243 ** empty string otherwise.
246 if (Index < CollCount (P))
247 return CollAtUnchecked (P, Index);
253 char* SearchFile (const SearchPaths* P, const char* File)
254 /* Search for a file in a list of directories. Return a pointer to a malloced
255 ** area that contains the complete path, if found, return 0 otherwise.
259 StrBuf PathName = AUTO_STRBUF_INITIALIZER;
261 /* Start the search */
263 for (I = 0; I < CollCount (P); ++I) {
265 /* Copy the next path element into the buffer */
266 SB_CopyStr (&PathName, CollConstAt (P, I));
268 /* Add a path separator and the filename */
269 if (SB_NotEmpty (&PathName)) {
270 SB_AppendChar (&PathName, '/');
272 SB_AppendStr (&PathName, File);
273 SB_Terminate (&PathName);
275 /* Check if this file exists */
276 if (access (SB_GetBuf (&PathName), 0) == 0) {
277 /* The file exists, we're done */
278 Name = xstrdup (SB_GetBuf (&PathName));
283 /* Cleanup and return the result of the search */