VirtualBox

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

Last change on this file since 535 was 532, checked in by bird, 18 years ago

Some fixes.

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