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