2 * include.c - Include file handling for cc65
4 * Ullrich von Bassewitz, 18.08.1998
18 /*****************************************************************************/
20 /*****************************************************************************/
24 static char* SysIncludePath = 0;
25 static char* UserIncludePath = 0;
29 /*****************************************************************************/
31 /*****************************************************************************/
35 static char* Add (char* Orig, char* New)
36 /* Create a new path from Orig and New, delete Orig, return the result */
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';
47 /* Calculate the length of the combined paths */
49 Len = strlen (Orig) + NewLen;
54 /* Allocate memory for the new string */
55 NewPath = xmalloc (Len + 2);
57 /* Copy the strings */
59 strcpy (NewPath, Orig);
63 strcat (NewPath, New);
64 strcat (NewPath, ";");
66 /* Delete the original path */
69 /* Return the new path */
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.
83 char PathName [FILENAME_MAX];
85 /* Initialize variables */
86 Max = sizeof (PathName) - strlen (File) - 2;
92 /* Handle a NULL pointer as replacement for an empty string */
97 /* Start the search */
99 /* Copy the next path element into the buffer */
101 while (*P != '\0' && *P != ';' && Count < Max) {
102 PathName [Count++] = *P++;
105 /* Add a path separator and the filename */
107 PathName [Count++] = '/';
109 strcpy (PathName + Count, File);
111 /* Check if this file exists */
112 if (access (PathName, R_OK) == 0) {
113 /* The file exists */
114 return xstrdup (PathName);
117 /* Skip a list separator if we have one */
129 void AddIncludePath (char* NewPath, unsigned Where)
130 /* Add a new include path to the existing one */
132 /* Allow a NULL path */
134 if (Where & INC_SYS) {
135 SysIncludePath = Add (SysIncludePath, NewPath);
137 if (Where & INC_USER) {
138 UserIncludePath = Add (UserIncludePath, NewPath);
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.
150 if (Where & INC_SYS) {
151 /* Search in the system include directories */
152 return Find (SysIncludePath, Name);
154 if (Where & INC_USER) {
155 /* Search in the user include directories */
156 return Find (UserIncludePath, Name);