source: trunk/src/gmake/w32/pathstuff.c@ 508

Last change on this file since 508 was 508, checked in by bird, 19 years ago

workaround for directory statting (overrides the stat symbol).

  • Property svn:eol-style set to native
File size: 11.3 KB
Line 
1/* Path conversion for Windows pathnames.
2Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
32006 Free Software Foundation, Inc.
4This file is part of GNU Make.
5
6GNU Make is free software; you can redistribute it and/or modify it under the
7terms of the GNU General Public License as published by the Free Software
8Foundation; either version 2, or (at your option) any later version.
9
10GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15GNU Make; see the file COPYING. If not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
17
18#include <Windows.h>
19#include <string.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include "make.h"
23#include "pathstuff.h"
24
25
26/*
27 * Convert delimiter separated vpath to Canonical format.
28 */
29char *
30convert_vpath_to_windows32(char *Path, char to_delim)
31{
32 char *etok; /* token separator for old Path */
33
34 /*
35 * Convert all spaces to delimiters. Note that pathnames which
36 * contain blanks get trounced here. Use 8.3 format as a workaround.
37 */
38 for (etok = Path; etok && *etok; etok++)
39 if (isblank ((unsigned char) *etok))
40 *etok = to_delim;
41
42 return (convert_Path_to_windows32(Path, to_delim));
43}
44
45/*
46 * Convert delimiter separated path to Canonical format.
47 */
48char *
49convert_Path_to_windows32(char *Path, char to_delim)
50{
51 char *etok; /* token separator for old Path */
52 char *p; /* points to element of old Path */
53
54 /* is this a multi-element Path ? */
55 for (p = Path, etok = strpbrk(p, ":;");
56 etok;
57 etok = strpbrk(p, ":;"))
58 if ((etok - p) == 1) {
59 if (*(etok - 1) == ';' ||
60 *(etok - 1) == ':') {
61 etok[-1] = to_delim;
62 etok[0] = to_delim;
63 p = ++etok;
64 continue; /* ignore empty bucket */
65 } else if (!isalpha ((unsigned char) *p)) {
66 /* found one to count, handle things like '.' */
67 *etok = to_delim;
68 p = ++etok;
69 } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
70 /* found one to count, handle drive letter */
71 *etok = to_delim;
72 p = ++etok;
73 } else
74 /* all finished, force abort */
75 p += strlen(p);
76 } else {
77 /* found another one, no drive letter */
78 *etok = to_delim;
79 p = ++etok;
80 }
81
82 return Path;
83}
84
85/*
86 * Corrects the case of a path.
87 * Expects a fullpath!
88 */
89void w32_fixcase(char *pszPath)
90{
91#define my_assert(expr) \
92 do { \
93 if (!(expr)) { \
94 printf("my_assert: %s, file %s, line %d\npszPath=%s\npsz=%s\n", \
95 #expr, __FILE__, __LINE__, pszPath, psz); \
96 __asm { __asm int 3 } \
97 exit(1); \
98 } \
99 } while (0)
100
101 char *psz = pszPath;
102 if (*psz == '/' || *psz == '\\')
103 {
104 if (psz[1] == '/' || psz[1] == '\\')
105 {
106 /* UNC */
107 my_assert(psz[1] == '/' || psz[1] == '\\');
108 my_assert(psz[2] != '/' && psz[2] != '\\');
109
110 /* skip server name */
111 psz += 2;
112 while (*psz != '\\' && *psz != '/')
113 {
114 if (!*psz)
115 return;
116 *psz++ = toupper(*psz);
117 }
118
119 /* skip the share name */
120 psz++;
121 my_assert(*psz != '/' && *psz != '\\');
122 while (*psz != '\\' && *psz != '/')
123 {
124 if (!*psz)
125 return;
126 *psz++ = toupper(*psz);
127 }
128 my_assert(*psz == '/' || *psz == '\\');
129 psz++;
130 }
131 else
132 {
133 /* Unix spec */
134 psz++;
135 }
136 }
137 else
138 {
139 /* Drive letter */
140 my_assert(psz[1] == ':');
141 *psz = toupper(*psz);
142 my_assert(psz[0] >= 'A' && psz[0] <= 'Z');
143 my_assert(psz[2] == '/' || psz[2] == '\\');
144 psz += 3;
145 }
146
147 /*
148 * Pointing to the first char after the unc or drive specifier.
149 */
150 while (*psz)
151 {
152 WIN32_FIND_DATA FindFileData;
153 HANDLE hDir;
154 char chSaved0;
155 char chSaved1;
156 char *pszEnd;
157
158
159 /* find the end of the component. */
160 pszEnd = psz;
161 while (*pszEnd && *pszEnd != '/' && *pszEnd != '\\')
162 pszEnd++;
163
164 /* replace the end with "?\0" */
165 chSaved0 = pszEnd[0];
166 chSaved1 = pszEnd[1];
167 pszEnd[0] = '?';
168 pszEnd[1] = '\0';
169
170 /* find the right filename. */
171 hDir = FindFirstFile(pszPath, &FindFileData);
172 pszEnd[1] = chSaved1;
173 if (!hDir)
174 {
175 pszEnd[0] = chSaved0;
176 return;
177 }
178 pszEnd[0] = '\0';
179 while (stricmp(FindFileData.cFileName, psz))
180 {
181 if (!FindNextFile(hDir, &FindFileData))
182 {
183 pszEnd[0] = chSaved0;
184 return;
185 }
186 }
187 strcpy(psz, FindFileData.cFileName);
188 pszEnd[0] = chSaved0;
189
190 /* advance to the next component */
191 if (!chSaved0)
192 return;
193 psz = pszEnd + 1;
194 my_assert(*psz != '/' && *psz != '\\');
195 }
196#undef my_assert
197}
198
199/*
200 * Convert to forward slashes. Resolve to full pathname optionally
201 */
202char *
203w32ify(char *filename, int resolve)
204{
205 static char w32_path[FILENAME_MAX];
206 char *p;
207
208 if (resolve)
209 _fullpath(w32_path, filename, sizeof (w32_path));
210 else
211 strncpy(w32_path, filename, sizeof (w32_path));
212
213 w32_fixcase(w32_path);
214
215 for (p = w32_path; p && *p; p++)
216 if (*p == '\\')
217 *p = '/';
218
219 return w32_path;
220}
221
222char *
223getcwd_fs(char* buf, int len)
224{
225 char *p = getcwd(buf, len);
226
227 if (p) {
228 char *q = w32ify(buf, 0);
229 strncpy(buf, q, len);
230 }
231
232 return p;
233}
234
235/* Workaround for directory names with trailing slashes. */
236int
237stat(const char *path, struct stat *st)
238{
239 int rc = _stat(path, st);
240 if ( rc != 0
241 && errno == ENOENT
242 && *path != '\0')
243 {
244 char *slash = strchr(path, '\0') - 1;
245 if (*slash == '/' || *slash == '\\')
246 {
247 size_t len_path = slash - path + 1;
248 char *tmp = alloca(len_path + 4);
249 memcpy(tmp, path, len_path);
250 tmp[len_path] = '.';
251 tmp[len_path + 1] = '\0';
252 errno = 0;
253 rc = _stat(tmp, st);
254 if ( rc == 0
255 && !S_ISDIR(st->st_mode))
256 {
257 errno = ENOTDIR;
258 rc = -1;
259 }
260 }
261 }
262 return rc;
263}
264
265#ifdef unused
266/*
267 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
268 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
269 * _NutPathToNutc() fails to convert, just return the path we were handed
270 * and assume the caller will know what to do with it (It was probably
271 * a mistake to try and convert it anyway due to some of the bizarre things
272 * that might look like pathnames in makefiles).
273 */
274char *
275convert_path_to_nutc(char *path)
276{
277 int count; /* count of path elements */
278 char *nutc_path; /* new NutC path */
279 int nutc_path_len; /* length of buffer to allocate for new path */
280 char *pathp; /* pointer to nutc_path used to build it */
281 char *etok; /* token separator for old path */
282 char *p; /* points to element of old path */
283 char sep; /* what flavor of separator used in old path */
284 char *rval;
285
286 /* is this a multi-element path ? */
287 for (p = path, etok = strpbrk(p, ":;"), count = 0;
288 etok;
289 etok = strpbrk(p, ":;"))
290 if ((etok - p) == 1) {
291 if (*(etok - 1) == ';' ||
292 *(etok - 1) == ':') {
293 p = ++etok;
294 continue; /* ignore empty bucket */
295 } else if (etok = strpbrk(etok+1, ":;"))
296 /* found one to count, handle drive letter */
297 p = ++etok, count++;
298 else
299 /* all finished, force abort */
300 p += strlen(p);
301 } else
302 /* found another one, no drive letter */
303 p = ++etok, count++;
304
305 if (count) {
306 count++; /* x1;x2;x3 <- need to count x3 */
307
308 /*
309 * Hazard a guess on how big the buffer needs to be.
310 * We have to convert things like c:/foo to /c=/foo.
311 */
312 nutc_path_len = strlen(path) + (count*2) + 1;
313 nutc_path = xmalloc(nutc_path_len);
314 pathp = nutc_path;
315 *pathp = '\0';
316
317 /*
318 * Loop through PATH and convert one elemnt of the path at at
319 * a time. Single file pathnames will fail this and fall
320 * to the logic below loop.
321 */
322 for (p = path, etok = strpbrk(p, ":;");
323 etok;
324 etok = strpbrk(p, ":;")) {
325
326 /* don't trip up on device specifiers or empty path slots */
327 if ((etok - p) == 1)
328 if (*(etok - 1) == ';' ||
329 *(etok - 1) == ':') {
330 p = ++etok;
331 continue;
332 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
333 break; /* thing found was a WINDOWS32 pathname */
334
335 /* save separator */
336 sep = *etok;
337
338 /* terminate the current path element -- temporarily */
339 *etok = '\0';
340
341#ifdef __NUTC__
342 /* convert to NutC format */
343 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
344 free(nutc_path);
345 rval = savestring(path, strlen(path));
346 return rval;
347 }
348#else
349 *pathp++ = '/';
350 *pathp++ = p[0];
351 *pathp++ = '=';
352 *pathp++ = '/';
353 strcpy(pathp, &p[2]);
354#endif
355
356 pathp += strlen(pathp);
357 *pathp++ = ':'; /* use Unix style path separtor for new path */
358 *pathp = '\0'; /* make sure we are null terminaed */
359
360 /* restore path separator */
361 *etok = sep;
362
363 /* point p to first char of next path element */
364 p = ++etok;
365
366 }
367 } else {
368 nutc_path_len = strlen(path) + 3;
369 nutc_path = xmalloc(nutc_path_len);
370 pathp = nutc_path;
371 *pathp = '\0';
372 p = path;
373 }
374
375 /*
376 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
377 * or the path was a single filename and will be converted
378 * here. Note, testing p here assures that we don't trip up
379 * on paths like a;b; which have trailing delimiter followed by
380 * nothing.
381 */
382 if (*p != '\0') {
383#ifdef __NUTC__
384 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
385 free(nutc_path);
386 rval = savestring(path, strlen(path));
387 return rval;
388 }
389#else
390 *pathp++ = '/';
391 *pathp++ = p[0];
392 *pathp++ = '=';
393 *pathp++ = '/';
394 strcpy(pathp, &p[2]);
395#endif
396 } else
397 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
398
399 rval = savestring(nutc_path, strlen(nutc_path));
400 free(nutc_path);
401 return rval;
402}
403
404#endif
Note: See TracBrowser for help on using the repository browser.