1 | /* Path conversion for Windows pathnames.
|
---|
2 | Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
|
---|
3 | 2007 Free Software Foundation, Inc.
|
---|
4 | This file is part of GNU Make.
|
---|
5 |
|
---|
6 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
7 | terms of the GNU General Public License as published by the Free Software
|
---|
8 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
9 | version.
|
---|
10 |
|
---|
11 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
13 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License along with
|
---|
16 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
17 |
|
---|
18 | #include <Windows.h> /* bird */
|
---|
19 | #include <stdio.h> /* bird */
|
---|
20 | #include <string.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include "make.h"
|
---|
23 | #include "pathstuff.h"
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * Convert delimiter separated vpath to Canonical format.
|
---|
27 | */
|
---|
28 | char *
|
---|
29 | convert_vpath_to_windows32(char *Path, char to_delim)
|
---|
30 | {
|
---|
31 | char *etok; /* token separator for old Path */
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * Convert all spaces to delimiters. Note that pathnames which
|
---|
35 | * contain blanks get trounced here. Use 8.3 format as a workaround.
|
---|
36 | */
|
---|
37 | for (etok = Path; etok && *etok; etok++)
|
---|
38 | if (isblank ((unsigned char) *etok))
|
---|
39 | *etok = to_delim;
|
---|
40 |
|
---|
41 | return (convert_Path_to_windows32(Path, to_delim));
|
---|
42 | }
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Convert delimiter separated path to Canonical format.
|
---|
46 | */
|
---|
47 | char *
|
---|
48 | convert_Path_to_windows32(char *Path, char to_delim)
|
---|
49 | {
|
---|
50 | char *etok; /* token separator for old Path */
|
---|
51 | char *p; /* points to element of old Path */
|
---|
52 |
|
---|
53 | /* is this a multi-element Path ? */
|
---|
54 | for (p = Path, etok = strpbrk(p, ":;");
|
---|
55 | etok;
|
---|
56 | etok = strpbrk(p, ":;"))
|
---|
57 | if ((etok - p) == 1) {
|
---|
58 | if (*(etok - 1) == ';' ||
|
---|
59 | *(etok - 1) == ':') {
|
---|
60 | etok[-1] = to_delim;
|
---|
61 | etok[0] = to_delim;
|
---|
62 | p = ++etok;
|
---|
63 | continue; /* ignore empty bucket */
|
---|
64 | } else if (!isalpha ((unsigned char) *p)) {
|
---|
65 | /* found one to count, handle things like '.' */
|
---|
66 | *etok = to_delim;
|
---|
67 | p = ++etok;
|
---|
68 | } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
|
---|
69 | /* found one to count, handle drive letter */
|
---|
70 | *etok = to_delim;
|
---|
71 | p = ++etok;
|
---|
72 | } else
|
---|
73 | /* all finished, force abort */
|
---|
74 | p += strlen(p);
|
---|
75 | } else if (*p == '"') { /* a quoted directory */
|
---|
76 | for (p++; *p && *p != '"'; p++) /* skip quoted part */
|
---|
77 | ;
|
---|
78 | etok = strpbrk(p, ":;"); /* find next delimiter */
|
---|
79 | *etok = to_delim;
|
---|
80 | p = ++etok;
|
---|
81 | } else {
|
---|
82 | /* found another one, no drive letter */
|
---|
83 | *etok = to_delim;
|
---|
84 | p = ++etok;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return Path;
|
---|
88 | }
|
---|
89 |
|
---|
90 | #if 1 /* bird */
|
---|
91 | extern void nt_fullpath(const char *pszPath, char *pszFull, size_t cchFull);
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Convert to forward slashes. Resolve to full pathname optionally
|
---|
96 | */
|
---|
97 | char *
|
---|
98 | w32ify(const char *filename, int resolve)
|
---|
99 | {
|
---|
100 | static char w32_path[FILENAME_MAX];
|
---|
101 | char *p;
|
---|
102 |
|
---|
103 | #if 1 /* bird */
|
---|
104 | if (resolve) {
|
---|
105 | nt_fullpath(filename, w32_path, sizeof(w32_path));
|
---|
106 | } else {
|
---|
107 | w32_path[0] = '\0';
|
---|
108 | strncat(w32_path, filename, sizeof(w32_path));
|
---|
109 | }
|
---|
110 | #else /* !bird */
|
---|
111 | if (resolve) {
|
---|
112 | _fullpath(w32_path, filename, sizeof (w32_path));
|
---|
113 | } else
|
---|
114 | strncpy(w32_path, filename, sizeof (w32_path));
|
---|
115 | #endif /* !bird */
|
---|
116 |
|
---|
117 | for (p = w32_path; p && *p; p++)
|
---|
118 | if (*p == '\\')
|
---|
119 | *p = '/';
|
---|
120 |
|
---|
121 | return w32_path;
|
---|
122 | }
|
---|
123 |
|
---|
124 | char *
|
---|
125 | getcwd_fs(char* buf, int len)
|
---|
126 | {
|
---|
127 | char *p = getcwd(buf, len);
|
---|
128 |
|
---|
129 | if (p) {
|
---|
130 | char *q = w32ify(buf, 0);
|
---|
131 | #if 1 /* bird */
|
---|
132 | buf[0] = '\0';
|
---|
133 | strncat(buf, q, len);
|
---|
134 | #else /* !bird */
|
---|
135 | strncpy(buf, q, len);
|
---|
136 | #endif /* !bird */
|
---|
137 | }
|
---|
138 |
|
---|
139 | return p;
|
---|
140 | }
|
---|
141 |
|
---|
142 | #undef stat
|
---|
143 | /*
|
---|
144 | * Workaround for directory names with trailing slashes.
|
---|
145 | * Added by bird reasons stated.
|
---|
146 | */
|
---|
147 | int
|
---|
148 | my_stat(const char *path, struct stat *st)
|
---|
149 | {
|
---|
150 | int rc = stat(path, st);
|
---|
151 | if ( rc != 0
|
---|
152 | && errno == ENOENT
|
---|
153 | && *path != '\0')
|
---|
154 | {
|
---|
155 | char *slash = strchr(path, '\0') - 1;
|
---|
156 | if (*slash == '/' || *slash == '\\')
|
---|
157 | {
|
---|
158 | size_t len_path = slash - path + 1;
|
---|
159 | char *tmp = alloca(len_path + 4);
|
---|
160 | memcpy(tmp, path, len_path);
|
---|
161 | tmp[len_path] = '.';
|
---|
162 | tmp[len_path + 1] = '\0';
|
---|
163 | errno = 0;
|
---|
164 | rc = stat(tmp, st);
|
---|
165 | if ( rc == 0
|
---|
166 | && !S_ISDIR(st->st_mode))
|
---|
167 | {
|
---|
168 | errno = ENOTDIR;
|
---|
169 | rc = -1;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | #ifdef KMK_PRF
|
---|
174 | {
|
---|
175 | int err = errno;
|
---|
176 | fprintf(stderr, "stat(%s,) -> %d/%d\n", path, rc, errno);
|
---|
177 | errno = err;
|
---|
178 | }
|
---|
179 | #endif
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 |
|
---|
183 | #ifdef unused
|
---|
184 | /*
|
---|
185 | * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
|
---|
186 | * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
|
---|
187 | * _NutPathToNutc() fails to convert, just return the path we were handed
|
---|
188 | * and assume the caller will know what to do with it (It was probably
|
---|
189 | * a mistake to try and convert it anyway due to some of the bizarre things
|
---|
190 | * that might look like pathnames in makefiles).
|
---|
191 | */
|
---|
192 | char *
|
---|
193 | convert_path_to_nutc(char *path)
|
---|
194 | {
|
---|
195 | int count; /* count of path elements */
|
---|
196 | char *nutc_path; /* new NutC path */
|
---|
197 | int nutc_path_len; /* length of buffer to allocate for new path */
|
---|
198 | char *pathp; /* pointer to nutc_path used to build it */
|
---|
199 | char *etok; /* token separator for old path */
|
---|
200 | char *p; /* points to element of old path */
|
---|
201 | char sep; /* what flavor of separator used in old path */
|
---|
202 | char *rval;
|
---|
203 |
|
---|
204 | /* is this a multi-element path ? */
|
---|
205 | for (p = path, etok = strpbrk(p, ":;"), count = 0;
|
---|
206 | etok;
|
---|
207 | etok = strpbrk(p, ":;"))
|
---|
208 | if ((etok - p) == 1) {
|
---|
209 | if (*(etok - 1) == ';' ||
|
---|
210 | *(etok - 1) == ':') {
|
---|
211 | p = ++etok;
|
---|
212 | continue; /* ignore empty bucket */
|
---|
213 | } else if (etok = strpbrk(etok+1, ":;"))
|
---|
214 | /* found one to count, handle drive letter */
|
---|
215 | p = ++etok, count++;
|
---|
216 | else
|
---|
217 | /* all finished, force abort */
|
---|
218 | p += strlen(p);
|
---|
219 | } else
|
---|
220 | /* found another one, no drive letter */
|
---|
221 | p = ++etok, count++;
|
---|
222 |
|
---|
223 | if (count) {
|
---|
224 | count++; /* x1;x2;x3 <- need to count x3 */
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * Hazard a guess on how big the buffer needs to be.
|
---|
228 | * We have to convert things like c:/foo to /c=/foo.
|
---|
229 | */
|
---|
230 | nutc_path_len = strlen(path) + (count*2) + 1;
|
---|
231 | nutc_path = xmalloc(nutc_path_len);
|
---|
232 | pathp = nutc_path;
|
---|
233 | *pathp = '\0';
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * Loop through PATH and convert one elemnt of the path at at
|
---|
237 | * a time. Single file pathnames will fail this and fall
|
---|
238 | * to the logic below loop.
|
---|
239 | */
|
---|
240 | for (p = path, etok = strpbrk(p, ":;");
|
---|
241 | etok;
|
---|
242 | etok = strpbrk(p, ":;")) {
|
---|
243 |
|
---|
244 | /* don't trip up on device specifiers or empty path slots */
|
---|
245 | if ((etok - p) == 1)
|
---|
246 | if (*(etok - 1) == ';' ||
|
---|
247 | *(etok - 1) == ':') {
|
---|
248 | p = ++etok;
|
---|
249 | continue;
|
---|
250 | } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
|
---|
251 | break; /* thing found was a WINDOWS32 pathname */
|
---|
252 |
|
---|
253 | /* save separator */
|
---|
254 | sep = *etok;
|
---|
255 |
|
---|
256 | /* terminate the current path element -- temporarily */
|
---|
257 | *etok = '\0';
|
---|
258 |
|
---|
259 | #ifdef __NUTC__
|
---|
260 | /* convert to NutC format */
|
---|
261 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
---|
262 | free(nutc_path);
|
---|
263 | rval = savestring(path, strlen(path));
|
---|
264 | return rval;
|
---|
265 | }
|
---|
266 | #else
|
---|
267 | *pathp++ = '/';
|
---|
268 | *pathp++ = p[0];
|
---|
269 | *pathp++ = '=';
|
---|
270 | *pathp++ = '/';
|
---|
271 | strcpy(pathp, &p[2]);
|
---|
272 | #endif
|
---|
273 |
|
---|
274 | pathp += strlen(pathp);
|
---|
275 | *pathp++ = ':'; /* use Unix style path separtor for new path */
|
---|
276 | *pathp = '\0'; /* make sure we are null terminaed */
|
---|
277 |
|
---|
278 | /* restore path separator */
|
---|
279 | *etok = sep;
|
---|
280 |
|
---|
281 | /* point p to first char of next path element */
|
---|
282 | p = ++etok;
|
---|
283 |
|
---|
284 | }
|
---|
285 | } else {
|
---|
286 | nutc_path_len = strlen(path) + 3;
|
---|
287 | nutc_path = xmalloc(nutc_path_len);
|
---|
288 | pathp = nutc_path;
|
---|
289 | *pathp = '\0';
|
---|
290 | p = path;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * OK, here we handle the last element in PATH (e.g. c of a;b;c)
|
---|
295 | * or the path was a single filename and will be converted
|
---|
296 | * here. Note, testing p here assures that we don't trip up
|
---|
297 | * on paths like a;b; which have trailing delimiter followed by
|
---|
298 | * nothing.
|
---|
299 | */
|
---|
300 | if (*p != '\0') {
|
---|
301 | #ifdef __NUTC__
|
---|
302 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
---|
303 | free(nutc_path);
|
---|
304 | rval = savestring(path, strlen(path));
|
---|
305 | return rval;
|
---|
306 | }
|
---|
307 | #else
|
---|
308 | *pathp++ = '/';
|
---|
309 | *pathp++ = p[0];
|
---|
310 | *pathp++ = '=';
|
---|
311 | *pathp++ = '/';
|
---|
312 | strcpy(pathp, &p[2]);
|
---|
313 | #endif
|
---|
314 | } else
|
---|
315 | *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
|
---|
316 |
|
---|
317 | rval = savestring(nutc_path, strlen(nutc_path));
|
---|
318 | free(nutc_path);
|
---|
319 | return rval;
|
---|
320 | }
|
---|
321 |
|
---|
322 | #endif
|
---|