VirtualBox

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

Last change on this file since 2657 was 2592, checked in by bird, 13 years ago

kmk build fixes for win.amd64.

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1/* Path conversion for Windows pathnames.
2Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
32007, 2008, 2009, 2010 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 3 of the License, or (at your option) any later
9version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16this program. If not, see <http://www.gnu.org/licenses/>. */
17
18#include <Windows.h> /* bird */
19#include "make.h"
20#include <string.h>
21#include <stdlib.h>
22#include "pathstuff.h"
23
24/*
25 * Convert delimiter separated vpath to Canonical format.
26 */
27char *
28convert_vpath_to_windows32(char *Path, char to_delim)
29{
30 char *etok; /* token separator for old Path */
31
32 /*
33 * Convert all spaces to delimiters. Note that pathnames which
34 * contain blanks get trounced here. Use 8.3 format as a workaround.
35 */
36 for (etok = Path; etok && *etok; etok++)
37 if (isblank ((unsigned char) *etok))
38 *etok = to_delim;
39
40 return (convert_Path_to_windows32(Path, to_delim));
41}
42
43/*
44 * Convert delimiter separated path to Canonical format.
45 */
46char *
47convert_Path_to_windows32(char *Path, char to_delim)
48{
49 char *etok; /* token separator for old Path */
50 char *p; /* points to element of old Path */
51
52 /* is this a multi-element Path ? */
53 /* FIXME: Perhaps use ":;\"" in strpbrk to convert all quotes to
54 delimiters as well, as a way to handle quoted directories in
55 PATH? */
56 for (p = Path, etok = strpbrk(p, ":;");
57 etok;
58 etok = strpbrk(p, ":;"))
59 if ((etok - p) == 1) {
60 if (*(etok - 1) == ';' ||
61 *(etok - 1) == ':') {
62 etok[-1] = to_delim;
63 etok[0] = to_delim;
64 p = ++etok;
65 continue; /* ignore empty bucket */
66 } else if (!isalpha ((unsigned char) *p)) {
67 /* found one to count, handle things like '.' */
68 *etok = to_delim;
69 p = ++etok;
70 } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
71 /* found one to count, handle drive letter */
72 *etok = to_delim;
73 p = ++etok;
74 } else
75 /* all finished, force abort */
76 p += strlen(p);
77 } else if (*p == '"') { /* a quoted directory */
78 for (p++; *p && *p != '"'; p++) /* skip quoted part */
79 ;
80 etok = strpbrk(p, ":;"); /* find next delimiter */
81 if (etok) {
82 *etok = to_delim;
83 p = ++etok;
84 } else
85 p += strlen(p);
86 } else {
87 /* found another one, no drive letter */
88 *etok = to_delim;
89 p = ++etok;
90 }
91
92 return Path;
93}
94
95#if 1 /* bird */
96extern void nt_fullpath(const char *pszPath, char *pszFull, size_t cchFull);
97#endif
98
99/*
100 * Convert to forward slashes. Resolve to full pathname optionally
101 */
102char *
103w32ify(const char *filename, int resolve)
104{
105 static char w32_path[FILENAME_MAX];
106 char *p;
107
108#if 1 /* bird */
109 if (resolve) {
110 nt_fullpath(filename, w32_path, sizeof(w32_path));
111 } else {
112 w32_path[0] = '\0';
113 strncat(w32_path, filename, sizeof(w32_path));
114 }
115#else /* !bird */
116 if (resolve) {
117 _fullpath(w32_path, filename, sizeof (w32_path));
118 } else
119 strncpy(w32_path, filename, sizeof (w32_path));
120#endif /* !bird */
121
122 for (p = w32_path; p && *p; p++)
123 if (*p == '\\')
124 *p = '/';
125
126 return w32_path;
127}
128
129char *
130getcwd_fs(char* buf, int len)
131{
132 char *p = getcwd(buf, len);
133
134 if (p) {
135 char *q = w32ify(buf, 0);
136#if 1 /* bird */
137 buf[0] = '\0';
138 strncat(buf, q, len);
139#else /* !bird */
140 strncpy(buf, q, len);
141#endif /* !bird */
142 }
143
144 return p;
145}
146
147#ifdef unused
148/*
149 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
150 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
151 * _NutPathToNutc() fails to convert, just return the path we were handed
152 * and assume the caller will know what to do with it (It was probably
153 * a mistake to try and convert it anyway due to some of the bizarre things
154 * that might look like pathnames in makefiles).
155 */
156char *
157convert_path_to_nutc(char *path)
158{
159 int count; /* count of path elements */
160 char *nutc_path; /* new NutC path */
161 int nutc_path_len; /* length of buffer to allocate for new path */
162 char *pathp; /* pointer to nutc_path used to build it */
163 char *etok; /* token separator for old path */
164 char *p; /* points to element of old path */
165 char sep; /* what flavor of separator used in old path */
166 char *rval;
167
168 /* is this a multi-element path ? */
169 for (p = path, etok = strpbrk(p, ":;"), count = 0;
170 etok;
171 etok = strpbrk(p, ":;"))
172 if ((etok - p) == 1) {
173 if (*(etok - 1) == ';' ||
174 *(etok - 1) == ':') {
175 p = ++etok;
176 continue; /* ignore empty bucket */
177 } else if (etok = strpbrk(etok+1, ":;"))
178 /* found one to count, handle drive letter */
179 p = ++etok, count++;
180 else
181 /* all finished, force abort */
182 p += strlen(p);
183 } else
184 /* found another one, no drive letter */
185 p = ++etok, count++;
186
187 if (count) {
188 count++; /* x1;x2;x3 <- need to count x3 */
189
190 /*
191 * Hazard a guess on how big the buffer needs to be.
192 * We have to convert things like c:/foo to /c=/foo.
193 */
194 nutc_path_len = strlen(path) + (count*2) + 1;
195 nutc_path = xmalloc(nutc_path_len);
196 pathp = nutc_path;
197 *pathp = '\0';
198
199 /*
200 * Loop through PATH and convert one elemnt of the path at at
201 * a time. Single file pathnames will fail this and fall
202 * to the logic below loop.
203 */
204 for (p = path, etok = strpbrk(p, ":;");
205 etok;
206 etok = strpbrk(p, ":;")) {
207
208 /* don't trip up on device specifiers or empty path slots */
209 if ((etok - p) == 1)
210 if (*(etok - 1) == ';' ||
211 *(etok - 1) == ':') {
212 p = ++etok;
213 continue;
214 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
215 break; /* thing found was a WINDOWS32 pathname */
216
217 /* save separator */
218 sep = *etok;
219
220 /* terminate the current path element -- temporarily */
221 *etok = '\0';
222
223#ifdef __NUTC__
224 /* convert to NutC format */
225 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
226 free(nutc_path);
227 rval = savestring(path, strlen(path));
228 return rval;
229 }
230#else
231 *pathp++ = '/';
232 *pathp++ = p[0];
233 *pathp++ = '=';
234 *pathp++ = '/';
235 strcpy(pathp, &p[2]);
236#endif
237
238 pathp += strlen(pathp);
239 *pathp++ = ':'; /* use Unix style path separtor for new path */
240 *pathp = '\0'; /* make sure we are null terminaed */
241
242 /* restore path separator */
243 *etok = sep;
244
245 /* point p to first char of next path element */
246 p = ++etok;
247
248 }
249 } else {
250 nutc_path_len = strlen(path) + 3;
251 nutc_path = xmalloc(nutc_path_len);
252 pathp = nutc_path;
253 *pathp = '\0';
254 p = path;
255 }
256
257 /*
258 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
259 * or the path was a single filename and will be converted
260 * here. Note, testing p here assures that we don't trip up
261 * on paths like a;b; which have trailing delimiter followed by
262 * nothing.
263 */
264 if (*p != '\0') {
265#ifdef __NUTC__
266 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
267 free(nutc_path);
268 rval = savestring(path, strlen(path));
269 return rval;
270 }
271#else
272 *pathp++ = '/';
273 *pathp++ = p[0];
274 *pathp++ = '=';
275 *pathp++ = '/';
276 strcpy(pathp, &p[2]);
277#endif
278 } else
279 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
280
281 rval = savestring(nutc_path, strlen(nutc_path));
282 free(nutc_path);
283 return rval;
284}
285
286#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