VirtualBox

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

Last change on this file since 2849 was 2849, checked in by bird, 8 years ago

split nt_fullpath_cached out into its own file.

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