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