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