]> git.sur5r.net Git - cc65/blob - src/cc65/incpath.c
* Added several type casts to increase C++ compatibility.
[cc65] / src / cc65 / incpath.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 incpath.c                                 */
4 /*                                                                           */
5 /*                      Include path handling for cc65                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 <stdio.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 "xmalloc.h"
48
49 /* cc65 */
50 #include "incpath.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 static char* SysIncludePath  = 0;
61 static char* UserIncludePath = 0;
62
63
64
65 /*****************************************************************************/
66 /*                                   Code                                    */
67 /*****************************************************************************/
68
69
70
71 static char* Add (char* Orig, const char* New)
72 /* Create a new path from Orig and New, delete Orig, return the result */
73 {
74     unsigned OrigLen, NewLen;
75     char* NewPath;
76
77     /* Get the length of the original string */
78     OrigLen = Orig? strlen (Orig) : 0;
79
80     /* Get the length of the new path */
81     NewLen = strlen (New);
82
83     /* Check for a trailing path separator and remove it */
84     if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
85         --NewLen;
86     }
87
88     /* Allocate memory for the new string */
89     NewPath = (char*) xmalloc (OrigLen + NewLen + 2);
90
91     /* Copy the strings */
92     memcpy (NewPath, Orig, OrigLen);
93     memcpy (NewPath+OrigLen, New, NewLen);
94     NewPath [OrigLen+NewLen+0] = ';';
95     NewPath [OrigLen+NewLen+1] = '\0';
96
97     /* Delete the original path */
98     xfree (Orig);
99
100     /* Return the new path */
101     return NewPath;
102 }
103
104
105
106 static char* Find (const char* Path, const char* File)
107 /* Search for a file in a list of directories. If found, return the complete
108  * name including the path in a malloced data area, if not found, return 0.
109  */
110 {
111     const char* P;
112     int Max;
113     char PathName [FILENAME_MAX];
114
115     /* Initialize variables */
116     Max = sizeof (PathName) - strlen (File) - 2;
117     if (Max < 0) {
118         return 0;
119     }
120     P = Path;
121
122     /* Handle a NULL pointer as replacement for an empty string */
123     if (P == 0) {
124         P = "";
125     }
126
127     /* Start the search */
128     while (*P) {
129         /* Copy the next path element into the buffer */
130         int Count = 0;
131         while (*P != '\0' && *P != ';' && Count < Max) {
132             PathName [Count++] = *P++;
133         }
134
135         /* Add a path separator and the filename */
136         if (Count) {
137             PathName [Count++] = '/';
138         }
139         strcpy (PathName + Count, File);
140
141         /* Check if this file exists */
142         if (access (PathName, 0) == 0) {
143             /* The file exists */
144             return xstrdup (PathName);
145         }
146
147         /* Skip a list separator if we have one */
148         if (*P == ';') {
149             ++P;
150         }
151     }
152
153     /* Not found */
154     return 0;
155 }
156
157
158
159 void AddIncludePath (const char* NewPath, unsigned Where)
160 /* Add a new include path to the existing one */
161 {
162     /* Allow a NULL path */
163     if (NewPath) {
164         if (Where & INC_SYS) {
165             SysIncludePath = Add (SysIncludePath, NewPath);
166         }
167         if (Where & INC_USER) {
168             UserIncludePath = Add (UserIncludePath, NewPath);
169         }
170     }
171 }
172
173
174
175 char* FindInclude (const char* Name, unsigned Where)
176 /* Find an include file. Return a pointer to a malloced area that contains
177  * the complete path, if found, return 0 otherwise.
178  */
179 {
180     if (Where & INC_SYS) {
181         /* Search in the system include directories */
182         return Find (SysIncludePath, Name);
183     }
184     if (Where & INC_USER) {
185         /* Search in the user include directories */
186         return Find (UserIncludePath, Name);
187     }
188     return 0;
189 }
190
191
192