VirtualBox

source: kBuild/trunk/src/kmk/w32/pathstuff.c

最後變更 在這個檔案是 3186,由 bird 提交於 7 年 前

kmk: replaced w32ify() as it uses unsafe static buffer and encourages buffer size assumptions.

  • 屬性 svn:eol-style 設為 native
檔案大小: 9.5 KB
 
1/* Path conversion for Windows pathnames.
2Copyright (C) 1996-2016 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the Free Software
7Foundation; either version 3 of the License, or (at your option) any later
8version.
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
15this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include "makeint.h"
18#include <string.h>
19#include <stdlib.h>
20#include "pathstuff.h"
21#if 1 /* bird */
22# include "nt_fullpath.h"
23# include <assert.h>
24#endif
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 /* FIXME: Perhaps use ":;\"" in strpbrk to convert all quotes to
56 delimiters as well, as a way to handle quoted directories in
57 PATH? */
58 for (p = Path, etok = strpbrk(p, ":;");
59 etok;
60 etok = strpbrk(p, ":;"))
61 if ((etok - p) == 1) {
62 if (*(etok - 1) == ';' ||
63 *(etok - 1) == ':') {
64 etok[-1] = to_delim;
65 etok[0] = to_delim;
66 p = ++etok;
67 continue; /* ignore empty bucket */
68 } else if (!isalpha ((unsigned char) *p)) {
69 /* found one to count, handle things like '.' */
70 *etok = to_delim;
71 p = ++etok;
72 } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
73 /* found one to count, handle drive letter */
74 *etok = to_delim;
75 p = ++etok;
76 } else
77 /* all finished, force abort */
78 p += strlen(p);
79 } else if (*p == '"') { /* a quoted directory */
80 for (p++; *p && *p != '"'; p++) /* skip quoted part */
81 ;
82 etok = strpbrk(p, ":;"); /* find next delimiter */
83 if (etok) {
84 *etok = to_delim;
85 p = ++etok;
86 } else
87 p += strlen(p);
88 } else {
89 /* found another one, no drive letter */
90 *etok = to_delim;
91 p = ++etok;
92 }
93
94 return Path;
95}
96
97/*
98 * Convert to forward slashes directly (w32ify(filename, 0)).
99 */
100char *unix_slashes(char *filename) /* bird */
101{
102 char *slash = filename ;
103 while ((slash = strchr(slash, '\\')) != NULL)
104 *slash++ = '/';
105 return filename;
106}
107
108/*
109 * Resolve and convert to forward slashes directly (w32ify(filename, 1)).
110 * Returns if out of buffer space.
111 */
112char *unix_slashes_resolved(const char *src, char *dst, unsigned len)
113{
114 assert(len >= FILENAME_MAX);
115 *dst = '\0'; /** @todo nt_fullpath_cached needs to return some indication of overflow. */
116#if 1
117 nt_fullpath_cached(src, dst, len);
118#else
119 _fullpath(dst, src, len);
120#endif
121
122 return unix_slashes(dst);
123}
124
125#if 0 /* bird: replaced by unix_slashes and unix_slahes_resolved. */
126/*
127 * Convert to forward slashes. Resolve to full pathname optionally
128 */
129char *
130w32ify(const char *filename, int resolve)
131{
132 static char w32_path[FILENAME_MAX];
133#if 1 /* bird */
134
135 if (resolve) {
136 nt_fullpath_cached(filename, w32_path, sizeof(w32_path));
137 } else {
138 w32_path[0] = '\0';
139 strncat(w32_path, filename, sizeof(w32_path));
140 }
141 return unix_slashes(w32_path);
142
143#else /* !bird */
144 char *p;
145
146 if (resolve) {
147 _fullpath(w32_path, filename, sizeof (w32_path));
148 } else
149 strncpy(w32_path, filename, sizeof (w32_path));
150
151 for (p = w32_path; p && *p; p++)
152 if (*p == '\\')
153 *p = '/';
154
155 return w32_path;
156#endif /* !bird */
157}
158#endif
159
160char *
161getcwd_fs(char* buf, int len)
162{
163 char *p = getcwd(buf, len);
164
165 if (p) {
166#if 1
167 p = unix_slashes(p);
168#else
169 char *q = w32ify(buf, 0);
170#if 1 /* bird - UPSTREAM? */
171 buf[0] = '\0';
172 strncat(buf, q, len);
173#else /* !bird */
174 strncpy(buf, q, len);
175#endif
176#endif
177 }
178
179 return p;
180}
181
182#ifdef unused
183/*
184 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
185 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
186 * _NutPathToNutc() fails to convert, just return the path we were handed
187 * and assume the caller will know what to do with it (It was probably
188 * a mistake to try and convert it anyway due to some of the bizarre things
189 * that might look like pathnames in makefiles).
190 */
191char *
192convert_path_to_nutc(char *path)
193{
194 int count; /* count of path elements */
195 char *nutc_path; /* new NutC path */
196 int nutc_path_len; /* length of buffer to allocate for new path */
197 char *pathp; /* pointer to nutc_path used to build it */
198 char *etok; /* token separator for old path */
199 char *p; /* points to element of old path */
200 char sep; /* what flavor of separator used in old path */
201 char *rval;
202
203 /* is this a multi-element path ? */
204 for (p = path, etok = strpbrk(p, ":;"), count = 0;
205 etok;
206 etok = strpbrk(p, ":;"))
207 if ((etok - p) == 1) {
208 if (*(etok - 1) == ';' ||
209 *(etok - 1) == ':') {
210 p = ++etok;
211 continue; /* ignore empty bucket */
212 } else if (etok = strpbrk(etok+1, ":;"))
213 /* found one to count, handle drive letter */
214 p = ++etok, count++;
215 else
216 /* all finished, force abort */
217 p += strlen(p);
218 } else
219 /* found another one, no drive letter */
220 p = ++etok, count++;
221
222 if (count) {
223 count++; /* x1;x2;x3 <- need to count x3 */
224
225 /*
226 * Hazard a guess on how big the buffer needs to be.
227 * We have to convert things like c:/foo to /c=/foo.
228 */
229 nutc_path_len = strlen(path) + (count*2) + 1;
230 nutc_path = xmalloc(nutc_path_len);
231 pathp = nutc_path;
232 *pathp = '\0';
233
234 /*
235 * Loop through PATH and convert one elemnt of the path at at
236 * a time. Single file pathnames will fail this and fall
237 * to the logic below loop.
238 */
239 for (p = path, etok = strpbrk(p, ":;");
240 etok;
241 etok = strpbrk(p, ":;")) {
242
243 /* don't trip up on device specifiers or empty path slots */
244 if ((etok - p) == 1)
245 if (*(etok - 1) == ';' ||
246 *(etok - 1) == ':') {
247 p = ++etok;
248 continue;
249 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
250 break; /* thing found was a WINDOWS32 pathname */
251
252 /* save separator */
253 sep = *etok;
254
255 /* terminate the current path element -- temporarily */
256 *etok = '\0';
257
258#ifdef __NUTC__
259 /* convert to NutC format */
260 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
261 free(nutc_path);
262 rval = savestring(path, strlen(path));
263 return rval;
264 }
265#else
266 *pathp++ = '/';
267 *pathp++ = p[0];
268 *pathp++ = '=';
269 *pathp++ = '/';
270 strcpy(pathp, &p[2]);
271#endif
272
273 pathp += strlen(pathp);
274 *pathp++ = ':'; /* use Unix style path separtor for new path */
275 *pathp = '\0'; /* make sure we are null terminaed */
276
277 /* restore path separator */
278 *etok = sep;
279
280 /* point p to first char of next path element */
281 p = ++etok;
282
283 }
284 } else {
285 nutc_path_len = strlen(path) + 3;
286 nutc_path = xmalloc(nutc_path_len);
287 pathp = nutc_path;
288 *pathp = '\0';
289 p = path;
290 }
291
292 /*
293 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
294 * or the path was a single filename and will be converted
295 * here. Note, testing p here assures that we don't trip up
296 * on paths like a;b; which have trailing delimiter followed by
297 * nothing.
298 */
299 if (*p != '\0') {
300#ifdef __NUTC__
301 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
302 free(nutc_path);
303 rval = savestring(path, strlen(path));
304 return rval;
305 }
306#else
307 *pathp++ = '/';
308 *pathp++ = p[0];
309 *pathp++ = '=';
310 *pathp++ = '/';
311 strcpy(pathp, &p[2]);
312#endif
313 } else
314 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
315
316 rval = savestring(nutc_path, strlen(nutc_path));
317 free(nutc_path);
318 return rval;
319}
320
321#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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