VirtualBox

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

最後變更 在這個檔案從691是 691,由 bird 提交於 18 年 前

Fixed FindFirstFile handle leak in pathstuff.c.

  • 屬性 svn:eol-style 設為 native
檔案大小: 13.1 KB
 
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
204
205 /* find the end of the component. */
206 pszEnd = psz;
207 while (*pszEnd && *pszEnd != '/' && *pszEnd != '\\')
208 pszEnd++;
209
210 /* replace the end with "?\0" */
211 chSaved0 = pszEnd[0];
212 chSaved1 = pszEnd[1];
213 pszEnd[0] = '?';
214 pszEnd[1] = '\0';
215
216 /* find the right filename. */
217 hDir = FindFirstFile(pszPath, &FindFileData);
218 pszEnd[1] = chSaved1;
219 if (!hDir)
220 {
221 cchLast = psz - pszPath;
222 memcpy(s_szLast, pszPath, cchLast + 1);
223 pszEnd[0] = chSaved0;
224 return;
225 }
226 pszEnd[0] = '\0';
227 while (stricmp(FindFileData.cFileName, psz))
228 {
229 if (!FindNextFile(hDir, &FindFileData))
230 {
231 cchLast = psz - pszPath;
232 memcpy(s_szLast, pszPath, cchLast + 1);
233 pszEnd[0] = chSaved0;
234 return;
235 }
236 }
237 strcpy(psz, FindFileData.cFileName);
238 pszEnd[0] = chSaved0;
239 FindClose(hDir);
240
241 /* advance to the next component */
242 if (!chSaved0)
243 {
244 psz = pszEnd;
245 break;
246 }
247 psz = pszEnd + 1;
248 my_assert(*psz != '/' && *psz != '\\');
249 }
250
251 /* *psz == '\0', the end. */
252 cchLast = psz - pszPath;
253 memcpy(s_szLast, pszPath, cchLast + 1);
254#undef my_assert
255}
256
257/*
258 * Convert to forward slashes. Resolve to full pathname optionally
259 */
260char *
261w32ify(char *filename, int resolve)
262{
263 static char w32_path[FILENAME_MAX];
264 char *p;
265
266 if (resolve) {
267 _fullpath(w32_path, filename, sizeof (w32_path));
268 w32_fixcase(w32_path); /* bird */
269 } else
270 strncpy(w32_path, filename, sizeof (w32_path));
271
272 for (p = w32_path; p && *p; p++)
273 if (*p == '\\')
274 *p = '/';
275
276 return w32_path;
277}
278
279char *
280getcwd_fs(char* buf, int len)
281{
282 char *p = getcwd(buf, len);
283
284 if (p) {
285 char *q = w32ify(buf, 0);
286 strncpy(buf, q, len);
287 }
288
289 return p;
290}
291
292#undef stat
293/*
294 * Workaround for directory names with trailing slashes.
295 * Added by bird reasons stated.
296 */
297int
298my_stat(const char *path, struct stat *st)
299{
300 int rc = stat(path, st);
301 if ( rc != 0
302 && errno == ENOENT
303 && *path != '\0')
304 {
305 char *slash = strchr(path, '\0') - 1;
306 if (*slash == '/' || *slash == '\\')
307 {
308 size_t len_path = slash - path + 1;
309 char *tmp = alloca(len_path + 4);
310 memcpy(tmp, path, len_path);
311 tmp[len_path] = '.';
312 tmp[len_path + 1] = '\0';
313 errno = 0;
314 rc = stat(tmp, st);
315 if ( rc == 0
316 && !S_ISDIR(st->st_mode))
317 {
318 errno = ENOTDIR;
319 rc = -1;
320 }
321 }
322 }
323 return rc;
324}
325
326#ifdef unused
327/*
328 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
329 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
330 * _NutPathToNutc() fails to convert, just return the path we were handed
331 * and assume the caller will know what to do with it (It was probably
332 * a mistake to try and convert it anyway due to some of the bizarre things
333 * that might look like pathnames in makefiles).
334 */
335char *
336convert_path_to_nutc(char *path)
337{
338 int count; /* count of path elements */
339 char *nutc_path; /* new NutC path */
340 int nutc_path_len; /* length of buffer to allocate for new path */
341 char *pathp; /* pointer to nutc_path used to build it */
342 char *etok; /* token separator for old path */
343 char *p; /* points to element of old path */
344 char sep; /* what flavor of separator used in old path */
345 char *rval;
346
347 /* is this a multi-element path ? */
348 for (p = path, etok = strpbrk(p, ":;"), count = 0;
349 etok;
350 etok = strpbrk(p, ":;"))
351 if ((etok - p) == 1) {
352 if (*(etok - 1) == ';' ||
353 *(etok - 1) == ':') {
354 p = ++etok;
355 continue; /* ignore empty bucket */
356 } else if (etok = strpbrk(etok+1, ":;"))
357 /* found one to count, handle drive letter */
358 p = ++etok, count++;
359 else
360 /* all finished, force abort */
361 p += strlen(p);
362 } else
363 /* found another one, no drive letter */
364 p = ++etok, count++;
365
366 if (count) {
367 count++; /* x1;x2;x3 <- need to count x3 */
368
369 /*
370 * Hazard a guess on how big the buffer needs to be.
371 * We have to convert things like c:/foo to /c=/foo.
372 */
373 nutc_path_len = strlen(path) + (count*2) + 1;
374 nutc_path = xmalloc(nutc_path_len);
375 pathp = nutc_path;
376 *pathp = '\0';
377
378 /*
379 * Loop through PATH and convert one elemnt of the path at at
380 * a time. Single file pathnames will fail this and fall
381 * to the logic below loop.
382 */
383 for (p = path, etok = strpbrk(p, ":;");
384 etok;
385 etok = strpbrk(p, ":;")) {
386
387 /* don't trip up on device specifiers or empty path slots */
388 if ((etok - p) == 1)
389 if (*(etok - 1) == ';' ||
390 *(etok - 1) == ':') {
391 p = ++etok;
392 continue;
393 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
394 break; /* thing found was a WINDOWS32 pathname */
395
396 /* save separator */
397 sep = *etok;
398
399 /* terminate the current path element -- temporarily */
400 *etok = '\0';
401
402#ifdef __NUTC__
403 /* convert to NutC format */
404 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
405 free(nutc_path);
406 rval = savestring(path, strlen(path));
407 return rval;
408 }
409#else
410 *pathp++ = '/';
411 *pathp++ = p[0];
412 *pathp++ = '=';
413 *pathp++ = '/';
414 strcpy(pathp, &p[2]);
415#endif
416
417 pathp += strlen(pathp);
418 *pathp++ = ':'; /* use Unix style path separtor for new path */
419 *pathp = '\0'; /* make sure we are null terminaed */
420
421 /* restore path separator */
422 *etok = sep;
423
424 /* point p to first char of next path element */
425 p = ++etok;
426
427 }
428 } else {
429 nutc_path_len = strlen(path) + 3;
430 nutc_path = xmalloc(nutc_path_len);
431 pathp = nutc_path;
432 *pathp = '\0';
433 p = path;
434 }
435
436 /*
437 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
438 * or the path was a single filename and will be converted
439 * here. Note, testing p here assures that we don't trip up
440 * on paths like a;b; which have trailing delimiter followed by
441 * nothing.
442 */
443 if (*p != '\0') {
444#ifdef __NUTC__
445 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
446 free(nutc_path);
447 rval = savestring(path, strlen(path));
448 return rval;
449 }
450#else
451 *pathp++ = '/';
452 *pathp++ = p[0];
453 *pathp++ = '=';
454 *pathp++ = '/';
455 strcpy(pathp, &p[2]);
456#endif
457 } else
458 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
459
460 rval = savestring(nutc_path, strlen(nutc_path));
461 free(nutc_path);
462 return rval;
463}
464
465#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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