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 */
44 # pragma warning(disable : 4996)
52 #include "searchpath.h"
58 /*****************************************************************************/
60 /*****************************************************************************/
64 static char* CleanupPath (const char* Path)
65 /* Prepare and return a clean copy of Path */
70 /* Get the length of the path */
73 /* Check for a trailing path separator and remove it */
74 if (Len > 0 && (Path[Len-1] == '\\' || Path[Len-1] == '/')) {
78 /* Allocate memory for the new string */
79 NewPath = (char*) xmalloc (Len + 1);
81 /* Copy the path and terminate it, then return the copy */
82 memcpy (NewPath, Path, Len);
89 static void Add (SearchPaths* P, const char* New)
90 /* Cleanup a new search path and add it to the list */
92 /* Add a clean copy of the path to the collection */
93 CollAppend (P, CleanupPath (New));
98 SearchPaths* NewSearchPath (void)
99 /* Create a new, empty search path list */
101 return NewCollection ();
106 void AddSearchPath (SearchPaths* P, const char* NewPath)
107 /* Add a new search path to the end of an existing list */
109 /* Allow a NULL path */
117 void AddSearchPathFromEnv (SearchPaths* P, const char* EnvVar)
118 /* Add a search path from an environment variable to the end of an existing
122 AddSearchPath (P, getenv (EnvVar));
127 void AddSubSearchPathFromEnv (SearchPaths* P, const char* EnvVar, const char* SubDir)
128 /* Add a search path from an environment variable, adding a subdirectory to
129 * the environment variable value.
132 StrBuf Dir = AUTO_STRBUF_INITIALIZER;
134 const char* EnvVal = getenv (EnvVar);
140 /* Copy the environment variable to the buffer */
141 SB_CopyStr (&Dir, EnvVal);
143 /* Add a path separator if necessary */
144 if (SB_NotEmpty (&Dir)) {
145 if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
146 SB_AppendChar (&Dir, '/');
150 /* Add the subdirectory and terminate the string */
151 SB_AppendStr (&Dir, SubDir);
154 /* Add the search path */
155 AddSearchPath (P, SB_GetConstBuf (&Dir));
157 /* Free the temp buffer */
163 void AddSubSearchPathFromWinBin (SearchPaths* P, const char* SubDir)
166 * Add a search path from the running binary, adding a subdirectory to
167 * the parent directory of the directory containing the binary.
174 if (GetModuleFileName (NULL, Dir, _MAX_PATH) == 0) {
178 /* Remove binary name */
179 Ptr = strrchr (Dir, '\\');
185 /* Check for 'bin' directory */
186 Ptr = strrchr (Dir, '\\');
190 if (strcmp (Ptr++, "\\bin") != 0) {
195 strcpy (Ptr, SubDir);
197 /* Add the search path */
198 AddSearchPath (P, Dir);
209 int PushSearchPath (SearchPaths* P, const char* NewPath)
210 /* Add a new search path to the head of an existing search path list, provided
211 * that it's not already there. If the path is already at the first position,
212 * return zero, otherwise return a non zero value.
215 /* Generate a clean copy of NewPath */
216 char* Path = CleanupPath (NewPath);
218 /* If we have paths, check if Path is already at position zero */
219 if (CollCount (P) > 0 && strcmp (CollConstAt (P, 0), Path) == 0) {
220 /* Match. Delete the copy and return to the caller */
225 /* Insert a clean copy of the path at position 0, return success */
226 CollInsert (P, Path, 0);
232 void PopSearchPath (SearchPaths* P)
233 /* Remove a search path from the head of an existing search path list */
235 /* Remove the path at position 0 */
236 xfree (CollAt (P, 0));
242 char* SearchFile (const SearchPaths* P, const char* File)
243 /* Search for a file in a list of directories. Return a pointer to a malloced
244 * area that contains the complete path, if found, return 0 otherwise.
248 StrBuf PathName = AUTO_STRBUF_INITIALIZER;
250 /* Start the search */
252 for (I = 0; I < CollCount (P); ++I) {
254 /* Copy the next path element into the buffer */
255 SB_CopyStr (&PathName, CollConstAt (P, I));
257 /* Add a path separator and the filename */
258 if (SB_NotEmpty (&PathName)) {
259 SB_AppendChar (&PathName, '/');
261 SB_AppendStr (&PathName, File);
262 SB_Terminate (&PathName);
264 /* Check if this file exists */
265 if (access (SB_GetBuf (&PathName), 0) == 0) {
266 /* The file exists, we're done */
267 Name = xstrdup (SB_GetBuf (&PathName));
272 /* Cleanup and return the result of the search */