VirtualBox

source: kBuild/trunk/src/gmake/w32/pathstuff.c@ 803

Last change on this file since 803 was 728, checked in by bird, 18 years ago

don't stop fixing the path case if one component happens to be a short name.

  • Property svn:eol-style set to native
File size: 13.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> /* 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 */
28char *
29convert_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 */
47char *
48convert_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 {
76 /* found another one, no drive letter */
77 *etok = to_delim;
78 p = ++etok;
79 }
80
81 return Path;
82}
83
84/*
85 * Corrects the case of a path.
86 * Expects a fullpath!
87 * Added by bird for the $(abspath ) function and w32ify
88 */
89void
90w32_fixcase(char *pszPath)
91{
92 static char s_szLast[260];
93 unsigned cchLast;
94
95#ifndef NDEBUG
96# define my_assert(expr) \
97 do { \
98 if (!(expr)) { \
99 printf("my_assert: %s, file %s, line %d\npszPath=%s\npsz=%s\n", \
100 #expr, __FILE__, __LINE__, pszPath, psz); \
101 __asm { __asm int 3 } \
102 exit(1); \
103 } \
104 } while (0)
105#else
106# define my_assert(expr) do {} while (0)
107#endif
108
109 char *psz = pszPath;
110 if (*psz == '/' || *psz == '\\')
111 {
112 if (psz[1] == '/' || psz[1] == '\\')
113 {
114 /* UNC */
115 my_assert(psz[1] == '/' || psz[1] == '\\');
116 my_assert(psz[2] != '/' && psz[2] != '\\');
117
118 /* skip server name */
119 psz += 2;
120 while (*psz != '\\' && *psz != '/')
121 {
122 if (!*psz)
123 return;
124 *psz++ = toupper(*psz);
125 }
126
127 /* skip the share name */
128 psz++;
129 my_assert(*psz != '/' && *psz != '\\');
130 while (*psz != '\\' && *psz != '/')
131 {
132 if (!*psz)
133 return;
134 *psz++ = toupper(*psz);
135 }
136 my_assert(*psz == '/' || *psz == '\\');
137 psz++;
138 }
139 else
140 {
141 /* Unix spec */
142 psz++;
143 }
144 }
145 else
146 {
147 /* Drive letter */
148 my_assert(psz[1] == ':');
149 *psz = toupper(*psz);
150 my_assert(psz[0] >= 'A' && psz[0] <= 'Z');
151 my_assert(psz[2] == '/' || psz[2] == '\\');
152 psz += 3;
153 }
154
155 /*
156 * Try make use of the result from the previous call.
157 * This is ignorant to slashes and similar, but may help even so.
158 */
159 if ( s_szLast[0] == pszPath[0]
160 && (psz - pszPath == 1 || s_szLast[1] == pszPath[1])
161 && (psz - pszPath <= 2 || s_szLast[2] == pszPath[2])
162 )
163 {
164 char *pszLast = &s_szLast[psz - pszPath];
165 char *pszCur = psz;
166 for (;;)
167 {
168 const char ch1 = *pszCur;
169 const char ch2 = *pszLast;
170 if ( ch1 != ch2
171 && (ch1 != '\\' || ch2 != '/')
172 && (ch1 != '/' || ch2 != '\\'))
173 {
174 if ( tolower(ch1) != tolower(ch2)
175 && toupper(ch1) != toupper(ch2))
176 break;
177 /* optimistic, component mismatch will be corrected in the next loop. */
178 *pszCur = ch2;
179 }
180 if (ch1 == '/' || ch1 == '\\')
181 psz = pszCur + 1;
182 else if (ch1 == '\0')
183 {
184 psz = pszCur;
185 break;
186 }
187 pszCur++;
188 pszLast++;
189 }
190 }
191
192 /*
193 * Pointing to the first char after the unc or drive specifier,
194 * or in case of a cache hit, the first non-matching char (following a slash of course).
195 */
196 while (*psz)
197 {
198 WIN32_FIND_DATA FindFileData;
199 HANDLE hDir;
200 char chSaved0;
201 char chSaved1;
202 char *pszEnd;
203 int iLongNameDiff;
204
205
206 /* find the end of the component. */
207 pszEnd = psz;
208 while (*pszEnd && *pszEnd != '/' && *pszEnd != '\\')
209 pszEnd++;
210
211 /* replace the end with "?\0" */
212 chSaved0 = pszEnd[0];
213 chSaved1 = pszEnd[1];
214 pszEnd[0] = '?';
215 pszEnd[1] = '\0';
216
217 /* find the right filename. */
218 hDir = FindFirstFile(pszPath, &FindFileData);
219 pszEnd[1] = chSaved1;
220 if (!hDir)
221 {
222 cchLast = psz - pszPath;
223 memcpy(s_szLast, pszPath, cchLast + 1);
224 pszEnd[0] = chSaved0;
225 return;
226 }
227 pszEnd[0] = '\0';
228 while ( (iLongNameDiff = stricmp(FindFileData.cFileName, psz))
229 && stricmp(FindFileData.cAlternateFileName, psz))
230 {
231 if (!FindNextFile(hDir, &FindFileData))
232 {
233 cchLast = psz - pszPath;
234 memcpy(s_szLast, pszPath, cchLast + 1);
235 pszEnd[0] = chSaved0;
236 return;
237 }
238 }
239 strcpy(psz, !iLongNameDiff ? FindFileData.cFileName : FindFileData.cAlternateFileName);
240 pszEnd[0] = chSaved0;
241 FindClose(hDir);
242
243 /* advance to the next component */
244 if (!chSaved0)
245 {
246 psz = pszEnd;
247 break;
248 }
249 psz = pszEnd + 1;
250 my_assert(*psz != '/' && *psz != '\\');
251 }
252
253 /* *psz == '\0', the end. */
254 cchLast = psz - pszPath;
255 memcpy(s_szLast, pszPath, cchLast + 1);
256#undef my_assert
257}
258
259/*
260 * Convert to forward slashes. Resolve to full pathname optionally
261 */
262char *
263w32ify(char *filename, int resolve)
264{
265 static char w32_path[FILENAME_MAX];
266 char *p;
267
268 if (resolve) {
269 _fullpath(w32_path, filename, sizeof (w32_path));
270 w32_fixcase(w32_path); /* bird */
271 } else
272 strncpy(w32_path, filename, sizeof (w32_path));
273
274 for (p = w32_path; p && *p; p++)
275 if (*p == '\\')
276 *p = '/';
277
278 return w32_path;
279}
280
281char *
282getcwd_fs(char* buf, int len)
283{
284 char *p = getcwd(buf, len);
285
286 if (p) {
287 char *q = w32ify(buf, 0);
288 strncpy(buf, q, len);
289 }
290
291 return p;
292}
293
294#undef stat
295/*
296 * Workaround for directory names with trailing slashes.
297 * Added by bird reasons stated.
298 */
299int
300my_stat(const char *path, struct stat *st)
301{
302 int rc = stat(path, st);
303 if ( rc != 0
304 && errno == ENOENT
305 && *path != '\0')
306 {
307 char *slash = strchr(path, '\0') - 1;
308 if (*slash == '/' || *slash == '\\')
309 {
310 size_t len_path = slash - path + 1;
311 char *tmp = alloca(len_path + 4);
312 memcpy(tmp, path, len_path);
313 tmp[len_path] = '.';
314 tmp[len_path + 1] = '\0';
315 errno = 0;
316 rc = stat(tmp, st);
317 if ( rc == 0
318 && !S_ISDIR(st->st_mode))
319 {
320 errno = ENOTDIR;
321 rc = -1;
322 }
323 }
324 }
325 return rc;
326}
327
328#ifdef unused
329/*
330 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
331 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
332 * _NutPathToNutc() fails to convert, just return the path we were handed
333 * and assume the caller will know what to do with it (It was probably
334 * a mistake to try and convert it anyway due to some of the bizarre things
335 * that might look like pathnames in makefiles).
336 */
337char *
338convert_path_to_nutc(char *path)
339{
340 int count; /* count of path elements */
341 char *nutc_path; /* new NutC path */
342 int nutc_path_len; /* length of buffer to allocate for new path */
343 char *pathp; /* pointer to nutc_path used to build it */
344 char *etok; /* token separator for old path */
345 char *p; /* points to element of old path */
346 char sep; /* what flavor of separator used in old path */
347 char *rval;
348
349 /* is this a multi-element path ? */
350 for (p = path, etok = strpbrk(p, ":;"), count = 0;
351 etok;
352 etok = strpbrk(p, ":;"))
353 if ((etok - p) == 1) {
354 if (*(etok - 1) == ';' ||
355 *(etok - 1) == ':') {
356 p = ++etok;
357 continue; /* ignore empty bucket */
358 } else if (etok = strpbrk(etok+1, ":;"))
359 /* found one to count, handle drive letter */
360 p = ++etok, count++;
361 else
362 /* all finished, force abort */
363 p += strlen(p);
364 } else
365 /* found another one, no drive letter */
366 p = ++etok, count++;
367
368 if (count) {
369 count++; /* x1;x2;x3 <- need to count x3 */
370
371 /*
372 * Hazard a guess on how big the buffer needs to be.
373 * We have to convert things like c:/foo to /c=/foo.
374 */
375 nutc_path_len = strlen(path) + (count*2) + 1;
376 nutc_path = xmalloc(nutc_path_len);
377 pathp = nutc_path;
378 *pathp = '\0';
379
380 /*
381 * Loop through PATH and convert one elemnt of the path at at
382 * a time. Single file pathnames will fail this and fall
383 * to the logic below loop.
384 */
385 for (p = path, etok = strpbrk(p, ":;");
386 etok;
387 etok = strpbrk(p, ":;")) {
388
389 /* don't trip up on device specifiers or empty path slots */
390 if ((etok - p) == 1)
391 if (*(etok - 1) == ';' ||
392 *(etok - 1) == ':') {
393 p = ++etok;
394 continue;
395 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
396 break; /* thing found was a WINDOWS32 pathname */
397
398 /* save separator */
399 sep = *etok;
400
401 /* terminate the current path element -- temporarily */
402 *etok = '\0';
403
404#ifdef __NUTC__
405 /* convert to NutC format */
406 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
407 free(nutc_path);
408 rval = savestring(path, strlen(path));
409 return rval;
410 }
411#else
412 *pathp++ = '/';
413 *pathp++ = p[0];
414 *pathp++ = '=';
415 *pathp++ = '/';
416 strcpy(pathp, &p[2]);
417#endif
418
419 pathp += strlen(pathp);
420 *pathp++ = ':'; /* use Unix style path separtor for new path */
421 *pathp = '\0'; /* make sure we are null terminaed */
422
423 /* restore path separator */
424 *etok = sep;
425
426 /* point p to first char of next path element */
427 p = ++etok;
428
429 }
430 } else {
431 nutc_path_len = strlen(path) + 3;
432 nutc_path = xmalloc(nutc_path_len);
433 pathp = nutc_path;
434 *pathp = '\0';
435 p = path;
436 }
437
438 /*
439 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
440 * or the path was a single filename and will be converted
441 * here. Note, testing p here assures that we don't trip up
442 * on paths like a;b; which have trailing delimiter followed by
443 * nothing.
444 */
445 if (*p != '\0') {
446#ifdef __NUTC__
447 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
448 free(nutc_path);
449 rval = savestring(path, strlen(path));
450 return rval;
451 }
452#else
453 *pathp++ = '/';
454 *pathp++ = p[0];
455 *pathp++ = '=';
456 *pathp++ = '/';
457 strcpy(pathp, &p[2]);
458#endif
459 } else
460 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
461
462 rval = savestring(nutc_path, strlen(nutc_path));
463 free(nutc_path);
464 return rval;
465}
466
467#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette