]> git.sur5r.net Git - cc65/blob - src/common/searchpath.c
Fixed a bug
[cc65] / src / common / searchpath.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                               searchpath.h                                */
4 /*                                                                           */
5 /*                    Search path path handling for ld65                     */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 <stdlib.h>
38 #include <string.h>
39 #if defined(_MSC_VER)
40 /* Microsoft compiler */
41 #  include <io.h>
42 #else
43 /* Anyone else */
44 #  include <unistd.h>
45 #endif
46
47 /* common */
48 #include "searchpath.h"
49 #include "xmalloc.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 static char* SearchPaths[MAX_SEARCH_PATHS];
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 static char* Add (char* Orig, const char* New)
70 /* Create a new path from Orig and New, delete Orig, return the result */
71 {
72     unsigned OrigLen, NewLen;
73     char* NewPath;
74
75     /* Get the length of the original string */
76     OrigLen = Orig? strlen (Orig) : 0;
77
78     /* Get the length of the new path */
79     NewLen = strlen (New);
80
81     /* Check for a trailing path separator and remove it */
82     if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
83         --NewLen;
84     }
85
86     /* Allocate memory for the new string */
87     NewPath = (char*) xmalloc (OrigLen + NewLen + 2);
88
89     /* Copy the strings */
90     memcpy (NewPath, Orig, OrigLen);
91     memcpy (NewPath+OrigLen, New, NewLen);
92     NewPath [OrigLen+NewLen+0] = ';';
93     NewPath [OrigLen+NewLen+1] = '\0';
94
95     /* Delete the original path */
96     xfree (Orig);
97
98     /* Return the new path */
99     return NewPath;
100 }
101
102
103
104 static char* Find (const char* Path, const char* File)
105 /* Search for a file in a list of directories. If found, return the complete
106  * name including the path in a malloced data area, if not found, return 0.
107  */
108 {
109     const char* P;
110     int Max;
111     char PathName [FILENAME_MAX];
112
113     /* Initialize variables */
114     Max = sizeof (PathName) - strlen (File) - 2;
115     if (Max < 0) {
116         return 0;
117     }
118     P = Path;
119
120     /* Handle a NULL pointer as replacement for an empty string */
121     if (P == 0) {
122         P = "";
123     }
124
125     /* Start the search */
126     while (*P) {
127         /* Copy the next path element into the buffer */
128         int Count = 0;
129         while (*P != '\0' && *P != ';' && Count < Max) {
130             PathName [Count++] = *P++;
131         }
132
133         /* Add a path separator and the filename */
134         if (Count) {
135             PathName [Count++] = '/';
136         }
137         strcpy (PathName + Count, File);
138
139         /* Check if this file exists */
140         if (access (PathName, 0) == 0) {
141             /* The file exists */
142             return xstrdup (PathName);
143         }
144
145         /* Skip a list separator if we have one */
146         if (*P == ';') {
147             ++P;
148         }
149     }
150
151     /* Not found */
152     return 0;
153 }
154
155
156
157 void AddSearchPath (const char* NewPath, unsigned Where)
158 /* Add a new search path to the existing one */
159 {
160     /* Allow a NULL path */
161     if (NewPath) {
162         unsigned I;
163         for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
164             unsigned Mask = (0x01U << I);
165             if (Where & Mask) {
166                 SearchPaths[I] = Add (SearchPaths[I], NewPath);
167             }
168         }
169     }
170 }
171
172
173
174 void AddSearchPathFromEnv (const char* EnvVar, unsigned Where)
175 /* Add a search from an environment variable */
176 {
177     AddSearchPath (getenv (EnvVar), Where);
178 }
179
180
181
182 char* SearchFile (const char* Name, unsigned Where)
183 /* Search for a file in a list of directories. Return a pointer to a malloced
184  * area that contains the complete path, if found, return 0 otherwise.
185  */
186 {
187     unsigned I;
188     for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
189         unsigned Mask = (0x01U << I);
190         if (Where & Mask) {
191             char* Path = Find (SearchPaths[I], Name);
192             if (Path) {
193                 /* Found the file */
194                 return Path;
195             }
196         }
197     }
198     return 0;
199 }
200
201
202