]> git.sur5r.net Git - cc65/blob - src/cc65/include.c
Rename a function
[cc65] / src / cc65 / include.c
1 /*
2  * include.c - Include file handling for cc65
3  *
4  * Ullrich von Bassewitz, 18.08.1998
5  */
6
7
8
9 #include <stdio.h>
10 #include <string.h>
11 #if defined(_MSC_VER)
12 /* Microsoft compiler */
13 #  include <io.h>
14 #  define R_OK  4
15 #else
16 /* Anyone else */
17 #  include <unistd.h>
18 #endif
19
20 #include "mem.h"
21 #include "include.h"
22
23
24
25 /*****************************************************************************/
26 /*                                   data                                    */
27 /*****************************************************************************/
28
29
30
31 static char* SysIncludePath  = 0;
32 static char* UserIncludePath = 0;
33
34
35
36 /*****************************************************************************/
37 /*                                   code                                    */
38 /*****************************************************************************/
39
40
41
42 static char* Add (char* Orig, const char* New)
43 /* Create a new path from Orig and New, delete Orig, return the result */
44 {
45     unsigned OrigLen, NewLen;
46     char* NewPath;
47
48     /* Get the length of the original string */
49     OrigLen = Orig? strlen (Orig) : 0;
50
51     /* Get the length of the new path */
52     NewLen = strlen (New);
53
54     /* Check for a trailing path separator and remove it */
55     if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
56         --NewLen;
57     }
58
59     /* Allocate memory for the new string */
60     NewPath = xmalloc (OrigLen + NewLen + 2);
61
62     /* Copy the strings */
63     memcpy (NewPath, Orig, OrigLen);
64     memcpy (NewPath+OrigLen, New, NewLen);
65     NewPath [OrigLen+NewLen+0] = ';';
66     NewPath [OrigLen+NewLen+1] = '\0';
67
68     /* Delete the original path */
69     xfree (Orig);
70
71     /* Return the new path */
72     return NewPath;
73 }
74
75
76
77 static char* Find (const char* Path, const char* File)
78 /* Search for a file in a list of directories. If found, return the complete
79  * name including the path in a malloced data area, if not found, return 0.
80  */
81 {
82     const char* P;
83     int Max;
84     char PathName [FILENAME_MAX];
85
86     /* Initialize variables */
87     Max = sizeof (PathName) - strlen (File) - 2;
88     if (Max < 0) {
89         return 0;
90     }
91     P = Path;
92
93     /* Handle a NULL pointer as replacement for an empty string */
94     if (P == 0) {
95         P = "";
96     }
97
98     /* Start the search */
99     while (*P) {
100         /* Copy the next path element into the buffer */
101         int Count = 0;
102         while (*P != '\0' && *P != ';' && Count < Max) {
103             PathName [Count++] = *P++;
104         }
105
106         /* Add a path separator and the filename */
107         if (Count) {
108             PathName [Count++] = '/';
109         }
110         strcpy (PathName + Count, File);
111
112         /* Check if this file exists */
113         if (access (PathName, R_OK) == 0) {
114             /* The file exists */
115             return xstrdup (PathName);
116         }
117
118         /* Skip a list separator if we have one */
119         if (*P == ';') {
120             ++P;
121         }
122     }
123
124     /* Not found */
125     return 0;
126 }
127
128
129
130 void AddIncludePath (const char* NewPath, unsigned Where)
131 /* Add a new include path to the existing one */
132 {
133     /* Allow a NULL path */
134     if (NewPath) {
135         if (Where & INC_SYS) {
136             SysIncludePath = Add (SysIncludePath, NewPath);
137         }
138         if (Where & INC_USER) {
139             UserIncludePath = Add (UserIncludePath, NewPath);
140         }
141     }
142 }
143
144
145
146 char* FindInclude (const char* Name, unsigned Where)
147 /* Find an include file. Return a pointer to a malloced area that contains
148  * the complete path, if found, return 0 otherwise.
149  */
150 {
151     if (Where & INC_SYS) {
152         /* Search in the system include directories */
153         return Find (SysIncludePath, Name);
154     }
155     if (Where & INC_USER) {
156         /* Search in the user include directories */
157         return Find (UserIncludePath, Name);
158     }
159     return 0;
160 }
161
162
163
164