source: trunk/src/kmk/w32/pathstuff.c@ 3157

Last change on this file since 3157 was 3140, checked in by bird, 7 years ago

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

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