2 * include.c - Include file handling for cc65
4 * Ullrich von Bassewitz, 18.08.1998
12 /* Microsoft compiler */
25 /*****************************************************************************/
27 /*****************************************************************************/
31 static char* SysIncludePath = 0;
32 static char* UserIncludePath = 0;
36 /*****************************************************************************/
38 /*****************************************************************************/
42 static char* Add (char* Orig, const char* New)
43 /* Create a new path from Orig and New, delete Orig, return the result */
45 unsigned OrigLen, NewLen;
48 /* Get the length of the original string */
49 OrigLen = Orig? strlen (Orig) : 0;
51 /* Get the length of the new path */
52 NewLen = strlen (New);
54 /* Check for a trailing path separator and remove it */
55 if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
59 /* Allocate memory for the new string */
60 NewPath = xmalloc (OrigLen + NewLen + 2);
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';
68 /* Delete the original path */
71 /* Return the new path */
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.
84 char PathName [FILENAME_MAX];
86 /* Initialize variables */
87 Max = sizeof (PathName) - strlen (File) - 2;
93 /* Handle a NULL pointer as replacement for an empty string */
98 /* Start the search */
100 /* Copy the next path element into the buffer */
102 while (*P != '\0' && *P != ';' && Count < Max) {
103 PathName [Count++] = *P++;
106 /* Add a path separator and the filename */
108 PathName [Count++] = '/';
110 strcpy (PathName + Count, File);
112 /* Check if this file exists */
113 if (access (PathName, R_OK) == 0) {
114 /* The file exists */
115 return xstrdup (PathName);
118 /* Skip a list separator if we have one */
130 void AddIncludePath (const char* NewPath, unsigned Where)
131 /* Add a new include path to the existing one */
133 /* Allow a NULL path */
135 if (Where & INC_SYS) {
136 SysIncludePath = Add (SysIncludePath, NewPath);
138 if (Where & INC_USER) {
139 UserIncludePath = Add (UserIncludePath, NewPath);
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.
151 if (Where & INC_SYS) {
152 /* Search in the system include directories */
153 return Find (SysIncludePath, Name);
155 if (Where & INC_USER) {
156 /* Search in the user include directories */
157 return Find (UserIncludePath, Name);