VirtualBox

source: kBuild/vendor/gnumake/current/w32/pathstuff.c@ 1989

Last change on this file since 1989 was 1989, checked in by bird, 16 years ago

Load gnumake-2008-10-28-CVS into vendor/gnumake/current.

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