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