* Lots of bug fixes and cleanup; new i18n files, etc.
[make.git] / w32 / pathstuff.c
blobd8f38453964cedaa1a893ab2639e0812bc2aacc1
1 #include <string.h>
2 #include <stdlib.h>
3 #include "make.h"
4 #include "pathstuff.h"
6 /*
7 * Convert delimiter separated vpath to Canonical format.
8 */
9 char *
10 convert_vpath_to_windows32(char *Path, char to_delim)
12 char *etok; /* token separator for old Path */
15 * Convert all spaces to delimiters. Note that pathnames which
16 * contain blanks get trounced here. Use 8.3 format as a workaround.
18 for (etok = Path; etok && *etok; etok++)
19 if (isblank ((unsigned char) *etok))
20 *etok = to_delim;
22 return (convert_Path_to_windows32(Path, to_delim));
26 * Convert delimiter separated path to Canonical format.
28 char *
29 convert_Path_to_windows32(char *Path, char to_delim)
31 char *etok; /* token separator for old Path */
32 char *p; /* points to element of old Path */
34 /* is this a multi-element Path ? */
35 for (p = Path, etok = strpbrk(p, ":;");
36 etok;
37 etok = strpbrk(p, ":;"))
38 if ((etok - p) == 1) {
39 if (*(etok - 1) == ';' ||
40 *(etok - 1) == ':') {
41 etok[-1] = to_delim;
42 etok[0] = to_delim;
43 p = ++etok;
44 continue; /* ignore empty bucket */
45 } else if (!isalpha ((unsigned char) *p)) {
46 /* found one to count, handle things like '.' */
47 *etok = to_delim;
48 p = ++etok;
49 } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
50 /* found one to count, handle drive letter */
51 *etok = to_delim;
52 p = ++etok;
53 } else
54 /* all finished, force abort */
55 p += strlen(p);
56 } else {
57 /* found another one, no drive letter */
58 *etok = to_delim;
59 p = ++etok;
62 return Path;
66 * Convert to forward slashes. Resolve to full pathname optionally
68 char *
69 w32ify(char *filename, int resolve)
71 static char w32_path[FILENAME_MAX];
72 char *p;
74 if (resolve)
75 _fullpath(w32_path, filename, sizeof (w32_path));
76 else
77 strncpy(w32_path, filename, sizeof (w32_path));
79 for (p = w32_path; p && *p; p++)
80 if (*p == '\\')
81 *p = '/';
83 return w32_path;
86 char *
87 getcwd_fs(char* buf, int len)
89 char *p;
91 if (p = getcwd(buf, len)) {
92 char *q = w32ify(buf, 0);
93 strncpy(buf, q, len);
96 return p;
99 #ifdef unused
101 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
102 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
103 * _NutPathToNutc() fails to convert, just return the path we were handed
104 * and assume the caller will know what to do with it (It was probably
105 * a mistake to try and convert it anyway due to some of the bizarre things
106 * that might look like pathnames in makefiles).
108 char *
109 convert_path_to_nutc(char *path)
111 int count; /* count of path elements */
112 char *nutc_path; /* new NutC path */
113 int nutc_path_len; /* length of buffer to allocate for new path */
114 char *pathp; /* pointer to nutc_path used to build it */
115 char *etok; /* token separator for old path */
116 char *p; /* points to element of old path */
117 char sep; /* what flavor of separator used in old path */
118 char *rval;
120 /* is this a multi-element path ? */
121 for (p = path, etok = strpbrk(p, ":;"), count = 0;
122 etok;
123 etok = strpbrk(p, ":;"))
124 if ((etok - p) == 1) {
125 if (*(etok - 1) == ';' ||
126 *(etok - 1) == ':') {
127 p = ++etok;
128 continue; /* ignore empty bucket */
129 } else if (etok = strpbrk(etok+1, ":;"))
130 /* found one to count, handle drive letter */
131 p = ++etok, count++;
132 else
133 /* all finished, force abort */
134 p += strlen(p);
135 } else
136 /* found another one, no drive letter */
137 p = ++etok, count++;
139 if (count) {
140 count++; /* x1;x2;x3 <- need to count x3 */
143 * Hazard a guess on how big the buffer needs to be.
144 * We have to convert things like c:/foo to /c=/foo.
146 nutc_path_len = strlen(path) + (count*2) + 1;
147 nutc_path = xmalloc(nutc_path_len);
148 pathp = nutc_path;
149 *pathp = '\0';
152 * Loop through PATH and convert one elemnt of the path at at
153 * a time. Single file pathnames will fail this and fall
154 * to the logic below loop.
156 for (p = path, etok = strpbrk(p, ":;");
157 etok;
158 etok = strpbrk(p, ":;")) {
160 /* don't trip up on device specifiers or empty path slots */
161 if ((etok - p) == 1)
162 if (*(etok - 1) == ';' ||
163 *(etok - 1) == ':') {
164 p = ++etok;
165 continue;
166 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
167 break; /* thing found was a WINDOWS32 pathname */
169 /* save separator */
170 sep = *etok;
172 /* terminate the current path element -- temporarily */
173 *etok = '\0';
175 #ifdef __NUTC__
176 /* convert to NutC format */
177 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
178 free(nutc_path);
179 rval = savestring(path, strlen(path));
180 return rval;
182 #else
183 *pathp++ = '/';
184 *pathp++ = p[0];
185 *pathp++ = '=';
186 *pathp++ = '/';
187 strcpy(pathp, &p[2]);
188 #endif
190 pathp += strlen(pathp);
191 *pathp++ = ':'; /* use Unix style path separtor for new path */
192 *pathp = '\0'; /* make sure we are null terminaed */
194 /* restore path separator */
195 *etok = sep;
197 /* point p to first char of next path element */
198 p = ++etok;
201 } else {
202 nutc_path_len = strlen(path) + 3;
203 nutc_path = xmalloc(nutc_path_len);
204 pathp = nutc_path;
205 *pathp = '\0';
206 p = path;
210 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
211 * or the path was a single filename and will be converted
212 * here. Note, testing p here assures that we don't trip up
213 * on paths like a;b; which have trailing delimiter followed by
214 * nothing.
216 if (*p != '\0') {
217 #ifdef __NUTC__
218 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
219 free(nutc_path);
220 rval = savestring(path, strlen(path));
221 return rval;
223 #else
224 *pathp++ = '/';
225 *pathp++ = p[0];
226 *pathp++ = '=';
227 *pathp++ = '/';
228 strcpy(pathp, &p[2]);
229 #endif
230 } else
231 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
233 rval = savestring(nutc_path, strlen(nutc_path));
234 free(nutc_path);
235 return rval;
238 #endif