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