1 /*****************************************************************************/
5 /* Include path handling for the ca65 macro assembler */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
45 /*****************************************************************************/
47 /*****************************************************************************/
51 static char* IncludePath = 0;
55 /*****************************************************************************/
57 /*****************************************************************************/
61 static char* Add (char* Orig, const char* New)
62 /* Create a new path from Orig and New, delete Orig, return the result */
64 unsigned OrigLen, NewLen;
67 /* Get the length of the original string */
68 OrigLen = Orig? strlen (Orig) : 0;
70 /* Get the length of the new path */
71 NewLen = strlen (New);
73 /* Check for a trailing path separator and remove it */
74 if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
78 /* Allocate memory for the new string */
79 NewPath = Xmalloc (OrigLen + NewLen + 2);
81 /* Copy the strings */
82 memcpy (NewPath, Orig, OrigLen);
83 memcpy (NewPath+OrigLen, New, NewLen);
84 NewPath [OrigLen+NewLen+0] = ';';
85 NewPath [OrigLen+NewLen+1] = '\0';
87 /* Delete the original path */
90 /* Return the new path */
96 static char* Find (const char* Path, const char* File)
97 /* Search for a file in a list of directories. If found, return the complete
98 * name including the path in a malloced data area, if not found, return 0.
104 char PathName [FILENAME_MAX];
106 /* Initialize variables */
107 Max = sizeof (PathName) - strlen (File) - 2;
113 /* Handle a NULL pointer as replacement for an empty string */
118 /* Start the search */
120 /* Copy the next path element into the buffer */
122 while (*P != '\0' && *P != ';' && Count < Max) {
123 PathName [Count++] = *P++;
126 /* Add a path separator and the filename */
128 PathName [Count++] = '/';
130 strcpy (PathName + Count, File);
132 /* Check if this file exists */
133 if (access (PathName, R_OK) == 0) {
134 /* The file exists */
135 return StrDup (PathName);
138 /* Skip a list separator if we have one */
150 void AddIncludePath (const char* NewPath)
151 /* Add a new include path to the existing one */
153 /* Allow a NULL path */
155 IncludePath = Add (IncludePath, NewPath);
161 char* FindInclude (const char* Name)
162 /* Find an include file. Return a pointer to a malloced area that contains
163 * the complete path, if found, return 0 otherwise.
166 /* Search in the include directories */
167 return Find (IncludePath, Name);