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

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

Untested merge with GNU Make v3.81 (vendor/gnumake/2005-05-16 -> vendor/gnumake/current).

  • Property svn:eol-style set to native
File size: 10.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>
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#ifdef unused
236/*
237 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
238 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
239 * _NutPathToNutc() fails to convert, just return the path we were handed
240 * and assume the caller will know what to do with it (It was probably
241 * a mistake to try and convert it anyway due to some of the bizarre things
242 * that might look like pathnames in makefiles).
243 */
244char *
245convert_path_to_nutc(char *path)
246{
247 int count; /* count of path elements */
248 char *nutc_path; /* new NutC path */
249 int nutc_path_len; /* length of buffer to allocate for new path */
250 char *pathp; /* pointer to nutc_path used to build it */
251 char *etok; /* token separator for old path */
252 char *p; /* points to element of old path */
253 char sep; /* what flavor of separator used in old path */
254 char *rval;
255
256 /* is this a multi-element path ? */
257 for (p = path, etok = strpbrk(p, ":;"), count = 0;
258 etok;
259 etok = strpbrk(p, ":;"))
260 if ((etok - p) == 1) {
261 if (*(etok - 1) == ';' ||
262 *(etok - 1) == ':') {
263 p = ++etok;
264 continue; /* ignore empty bucket */
265 } else if (etok = strpbrk(etok+1, ":;"))
266 /* found one to count, handle drive letter */
267 p = ++etok, count++;
268 else
269 /* all finished, force abort */
270 p += strlen(p);
271 } else
272 /* found another one, no drive letter */
273 p = ++etok, count++;
274
275 if (count) {
276 count++; /* x1;x2;x3 <- need to count x3 */
277
278 /*
279 * Hazard a guess on how big the buffer needs to be.
280 * We have to convert things like c:/foo to /c=/foo.
281 */
282 nutc_path_len = strlen(path) + (count*2) + 1;
283 nutc_path = xmalloc(nutc_path_len);
284 pathp = nutc_path;
285 *pathp = '\0';
286
287 /*
288 * Loop through PATH and convert one elemnt of the path at at
289 * a time. Single file pathnames will fail this and fall
290 * to the logic below loop.
291 */
292 for (p = path, etok = strpbrk(p, ":;");
293 etok;
294 etok = strpbrk(p, ":;")) {
295
296 /* don't trip up on device specifiers or empty path slots */
297 if ((etok - p) == 1)
298 if (*(etok - 1) == ';' ||
299 *(etok - 1) == ':') {
300 p = ++etok;
301 continue;
302 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
303 break; /* thing found was a WINDOWS32 pathname */
304
305 /* save separator */
306 sep = *etok;
307
308 /* terminate the current path element -- temporarily */
309 *etok = '\0';
310
311#ifdef __NUTC__
312 /* convert to NutC format */
313 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
314 free(nutc_path);
315 rval = savestring(path, strlen(path));
316 return rval;
317 }
318#else
319 *pathp++ = '/';
320 *pathp++ = p[0];
321 *pathp++ = '=';
322 *pathp++ = '/';
323 strcpy(pathp, &p[2]);
324#endif
325
326 pathp += strlen(pathp);
327 *pathp++ = ':'; /* use Unix style path separtor for new path */
328 *pathp = '\0'; /* make sure we are null terminaed */
329
330 /* restore path separator */
331 *etok = sep;
332
333 /* point p to first char of next path element */
334 p = ++etok;
335
336 }
337 } else {
338 nutc_path_len = strlen(path) + 3;
339 nutc_path = xmalloc(nutc_path_len);
340 pathp = nutc_path;
341 *pathp = '\0';
342 p = path;
343 }
344
345 /*
346 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
347 * or the path was a single filename and will be converted
348 * here. Note, testing p here assures that we don't trip up
349 * on paths like a;b; which have trailing delimiter followed by
350 * nothing.
351 */
352 if (*p != '\0') {
353#ifdef __NUTC__
354 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
355 free(nutc_path);
356 rval = savestring(path, strlen(path));
357 return rval;
358 }
359#else
360 *pathp++ = '/';
361 *pathp++ = p[0];
362 *pathp++ = '=';
363 *pathp++ = '/';
364 strcpy(pathp, &p[2]);
365#endif
366 } else
367 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
368
369 rval = savestring(nutc_path, strlen(nutc_path));
370 free(nutc_path);
371 return rval;
372}
373
374#endif
Note: See TracBrowser for help on using the repository browser.