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 (resolve) {
|
---|
104 | #if 1 /* bird */
|
---|
105 | nt_fullpath(filename, w32_path, sizeof(w32_path));
|
---|
106 | #else /* !bird */
|
---|
107 | _fullpath(w32_path, filename, sizeof (w32_path));
|
---|
108 | #endif /* !bird */
|
---|
109 | } else
|
---|
110 | strncpy(w32_path, filename, sizeof (w32_path));
|
---|
111 |
|
---|
112 | for (p = w32_path; p && *p; p++)
|
---|
113 | if (*p == '\\')
|
---|
114 | *p = '/';
|
---|
115 |
|
---|
116 | return w32_path;
|
---|
117 | }
|
---|
118 |
|
---|
119 | char *
|
---|
120 | getcwd_fs(char* buf, int len)
|
---|
121 | {
|
---|
122 | char *p = getcwd(buf, len);
|
---|
123 |
|
---|
124 | if (p) {
|
---|
125 | char *q = w32ify(buf, 0);
|
---|
126 | strncpy(buf, q, len);
|
---|
127 | }
|
---|
128 |
|
---|
129 | return p;
|
---|
130 | }
|
---|
131 |
|
---|
132 | #undef stat
|
---|
133 | /*
|
---|
134 | * Workaround for directory names with trailing slashes.
|
---|
135 | * Added by bird reasons stated.
|
---|
136 | */
|
---|
137 | int
|
---|
138 | my_stat(const char *path, struct stat *st)
|
---|
139 | {
|
---|
140 | int rc = stat(path, st);
|
---|
141 | if ( rc != 0
|
---|
142 | && errno == ENOENT
|
---|
143 | && *path != '\0')
|
---|
144 | {
|
---|
145 | char *slash = strchr(path, '\0') - 1;
|
---|
146 | if (*slash == '/' || *slash == '\\')
|
---|
147 | {
|
---|
148 | size_t len_path = slash - path + 1;
|
---|
149 | char *tmp = alloca(len_path + 4);
|
---|
150 | memcpy(tmp, path, len_path);
|
---|
151 | tmp[len_path] = '.';
|
---|
152 | tmp[len_path + 1] = '\0';
|
---|
153 | errno = 0;
|
---|
154 | rc = stat(tmp, st);
|
---|
155 | if ( rc == 0
|
---|
156 | && !S_ISDIR(st->st_mode))
|
---|
157 | {
|
---|
158 | errno = ENOTDIR;
|
---|
159 | rc = -1;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | #ifdef KMK_PRF
|
---|
164 | {
|
---|
165 | int err = errno;
|
---|
166 | fprintf(stderr, "stat(%s,) -> %d/%d\n", path, rc, errno);
|
---|
167 | errno = err;
|
---|
168 | }
|
---|
169 | #endif
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 | #ifdef unused
|
---|
174 | /*
|
---|
175 | * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
|
---|
176 | * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
|
---|
177 | * _NutPathToNutc() fails to convert, just return the path we were handed
|
---|
178 | * and assume the caller will know what to do with it (It was probably
|
---|
179 | * a mistake to try and convert it anyway due to some of the bizarre things
|
---|
180 | * that might look like pathnames in makefiles).
|
---|
181 | */
|
---|
182 | char *
|
---|
183 | convert_path_to_nutc(char *path)
|
---|
184 | {
|
---|
185 | int count; /* count of path elements */
|
---|
186 | char *nutc_path; /* new NutC path */
|
---|
187 | int nutc_path_len; /* length of buffer to allocate for new path */
|
---|
188 | char *pathp; /* pointer to nutc_path used to build it */
|
---|
189 | char *etok; /* token separator for old path */
|
---|
190 | char *p; /* points to element of old path */
|
---|
191 | char sep; /* what flavor of separator used in old path */
|
---|
192 | char *rval;
|
---|
193 |
|
---|
194 | /* is this a multi-element path ? */
|
---|
195 | for (p = path, etok = strpbrk(p, ":;"), count = 0;
|
---|
196 | etok;
|
---|
197 | etok = strpbrk(p, ":;"))
|
---|
198 | if ((etok - p) == 1) {
|
---|
199 | if (*(etok - 1) == ';' ||
|
---|
200 | *(etok - 1) == ':') {
|
---|
201 | p = ++etok;
|
---|
202 | continue; /* ignore empty bucket */
|
---|
203 | } else if (etok = strpbrk(etok+1, ":;"))
|
---|
204 | /* found one to count, handle drive letter */
|
---|
205 | p = ++etok, count++;
|
---|
206 | else
|
---|
207 | /* all finished, force abort */
|
---|
208 | p += strlen(p);
|
---|
209 | } else
|
---|
210 | /* found another one, no drive letter */
|
---|
211 | p = ++etok, count++;
|
---|
212 |
|
---|
213 | if (count) {
|
---|
214 | count++; /* x1;x2;x3 <- need to count x3 */
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Hazard a guess on how big the buffer needs to be.
|
---|
218 | * We have to convert things like c:/foo to /c=/foo.
|
---|
219 | */
|
---|
220 | nutc_path_len = strlen(path) + (count*2) + 1;
|
---|
221 | nutc_path = xmalloc(nutc_path_len);
|
---|
222 | pathp = nutc_path;
|
---|
223 | *pathp = '\0';
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Loop through PATH and convert one elemnt of the path at at
|
---|
227 | * a time. Single file pathnames will fail this and fall
|
---|
228 | * to the logic below loop.
|
---|
229 | */
|
---|
230 | for (p = path, etok = strpbrk(p, ":;");
|
---|
231 | etok;
|
---|
232 | etok = strpbrk(p, ":;")) {
|
---|
233 |
|
---|
234 | /* don't trip up on device specifiers or empty path slots */
|
---|
235 | if ((etok - p) == 1)
|
---|
236 | if (*(etok - 1) == ';' ||
|
---|
237 | *(etok - 1) == ':') {
|
---|
238 | p = ++etok;
|
---|
239 | continue;
|
---|
240 | } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
|
---|
241 | break; /* thing found was a WINDOWS32 pathname */
|
---|
242 |
|
---|
243 | /* save separator */
|
---|
244 | sep = *etok;
|
---|
245 |
|
---|
246 | /* terminate the current path element -- temporarily */
|
---|
247 | *etok = '\0';
|
---|
248 |
|
---|
249 | #ifdef __NUTC__
|
---|
250 | /* convert to NutC format */
|
---|
251 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
---|
252 | free(nutc_path);
|
---|
253 | rval = savestring(path, strlen(path));
|
---|
254 | return rval;
|
---|
255 | }
|
---|
256 | #else
|
---|
257 | *pathp++ = '/';
|
---|
258 | *pathp++ = p[0];
|
---|
259 | *pathp++ = '=';
|
---|
260 | *pathp++ = '/';
|
---|
261 | strcpy(pathp, &p[2]);
|
---|
262 | #endif
|
---|
263 |
|
---|
264 | pathp += strlen(pathp);
|
---|
265 | *pathp++ = ':'; /* use Unix style path separtor for new path */
|
---|
266 | *pathp = '\0'; /* make sure we are null terminaed */
|
---|
267 |
|
---|
268 | /* restore path separator */
|
---|
269 | *etok = sep;
|
---|
270 |
|
---|
271 | /* point p to first char of next path element */
|
---|
272 | p = ++etok;
|
---|
273 |
|
---|
274 | }
|
---|
275 | } else {
|
---|
276 | nutc_path_len = strlen(path) + 3;
|
---|
277 | nutc_path = xmalloc(nutc_path_len);
|
---|
278 | pathp = nutc_path;
|
---|
279 | *pathp = '\0';
|
---|
280 | p = path;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * OK, here we handle the last element in PATH (e.g. c of a;b;c)
|
---|
285 | * or the path was a single filename and will be converted
|
---|
286 | * here. Note, testing p here assures that we don't trip up
|
---|
287 | * on paths like a;b; which have trailing delimiter followed by
|
---|
288 | * nothing.
|
---|
289 | */
|
---|
290 | if (*p != '\0') {
|
---|
291 | #ifdef __NUTC__
|
---|
292 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
---|
293 | free(nutc_path);
|
---|
294 | rval = savestring(path, strlen(path));
|
---|
295 | return rval;
|
---|
296 | }
|
---|
297 | #else
|
---|
298 | *pathp++ = '/';
|
---|
299 | *pathp++ = p[0];
|
---|
300 | *pathp++ = '=';
|
---|
301 | *pathp++ = '/';
|
---|
302 | strcpy(pathp, &p[2]);
|
---|
303 | #endif
|
---|
304 | } else
|
---|
305 | *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
|
---|
306 |
|
---|
307 | rval = savestring(nutc_path, strlen(nutc_path));
|
---|
308 | free(nutc_path);
|
---|
309 | return rval;
|
---|
310 | }
|
---|
311 |
|
---|
312 | #endif
|
---|