1 /* Path conversion for Windows pathnames.
2 Copyright (C) 1996-2012 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "pathstuff.h"
23 * Convert delimiter separated vpath to Canonical format.
26 convert_vpath_to_windows32(char *Path
, char to_delim
)
28 char *etok
; /* token separator for old Path */
31 * Convert all spaces to delimiters. Note that pathnames which
32 * contain blanks get trounced here. Use 8.3 format as a workaround.
34 for (etok
= Path
; etok
&& *etok
; etok
++)
35 if (isblank ((unsigned char) *etok
))
38 return (convert_Path_to_windows32(Path
, to_delim
));
42 * Convert delimiter separated path to Canonical format.
45 convert_Path_to_windows32(char *Path
, char to_delim
)
47 char *etok
; /* token separator for old Path */
48 char *p
; /* points to element of old Path */
50 /* is this a multi-element Path ? */
51 /* FIXME: Perhaps use ":;\"" in strpbrk to convert all quotes to
52 delimiters as well, as a way to handle quoted directories in
54 for (p
= Path
, etok
= strpbrk(p
, ":;");
56 etok
= strpbrk(p
, ":;"))
57 if ((etok
- p
) == 1) {
58 if (*(etok
- 1) == ';' ||
63 continue; /* ignore empty bucket */
64 } else if (!isalpha ((unsigned char) *p
)) {
65 /* found one to count, handle things like '.' */
68 } else if ((*etok
== ':') && (etok
= strpbrk(etok
+1, ":;"))) {
69 /* found one to count, handle drive letter */
73 /* all finished, force abort */
75 } else if (*p
== '"') { /* a quoted directory */
76 for (p
++; *p
&& *p
!= '"'; p
++) /* skip quoted part */
78 etok
= strpbrk(p
, ":;"); /* find next delimiter */
85 /* found another one, no drive letter */
94 * Convert to forward slashes. Resolve to full pathname optionally
97 w32ify(const char *filename
, int resolve
)
99 static char w32_path
[FILENAME_MAX
];
103 _fullpath(w32_path
, filename
, sizeof (w32_path
));
105 strncpy(w32_path
, filename
, sizeof (w32_path
));
107 for (p
= w32_path
; p
&& *p
; p
++)
115 getcwd_fs(char* buf
, int len
)
117 char *p
= getcwd(buf
, len
);
120 char *q
= w32ify(buf
, 0);
121 strncpy(buf
, q
, len
);
129 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
130 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
131 * _NutPathToNutc() fails to convert, just return the path we were handed
132 * and assume the caller will know what to do with it (It was probably
133 * a mistake to try and convert it anyway due to some of the bizarre things
134 * that might look like pathnames in makefiles).
137 convert_path_to_nutc(char *path
)
139 int count
; /* count of path elements */
140 char *nutc_path
; /* new NutC path */
141 int nutc_path_len
; /* length of buffer to allocate for new path */
142 char *pathp
; /* pointer to nutc_path used to build it */
143 char *etok
; /* token separator for old path */
144 char *p
; /* points to element of old path */
145 char sep
; /* what flavor of separator used in old path */
148 /* is this a multi-element path ? */
149 for (p
= path
, etok
= strpbrk(p
, ":;"), count
= 0;
151 etok
= strpbrk(p
, ":;"))
152 if ((etok
- p
) == 1) {
153 if (*(etok
- 1) == ';' ||
154 *(etok
- 1) == ':') {
156 continue; /* ignore empty bucket */
157 } else if (etok
= strpbrk(etok
+1, ":;"))
158 /* found one to count, handle drive letter */
161 /* all finished, force abort */
164 /* found another one, no drive letter */
168 count
++; /* x1;x2;x3 <- need to count x3 */
171 * Hazard a guess on how big the buffer needs to be.
172 * We have to convert things like c:/foo to /c=/foo.
174 nutc_path_len
= strlen(path
) + (count
*2) + 1;
175 nutc_path
= xmalloc(nutc_path_len
);
180 * Loop through PATH and convert one elemnt of the path at at
181 * a time. Single file pathnames will fail this and fall
182 * to the logic below loop.
184 for (p
= path
, etok
= strpbrk(p
, ":;");
186 etok
= strpbrk(p
, ":;")) {
188 /* don't trip up on device specifiers or empty path slots */
190 if (*(etok
- 1) == ';' ||
191 *(etok
- 1) == ':') {
194 } else if ((etok
= strpbrk(etok
+1, ":;")) == NULL
)
195 break; /* thing found was a WINDOWS32 pathname */
200 /* terminate the current path element -- temporarily */
204 /* convert to NutC format */
205 if (_NutPathToNutc(p
, pathp
, 0) == FALSE
) {
207 rval
= savestring(path
, strlen(path
));
215 strcpy(pathp
, &p
[2]);
218 pathp
+= strlen(pathp
);
219 *pathp
++ = ':'; /* use Unix style path separtor for new path */
220 *pathp
= '\0'; /* make sure we are null terminaed */
222 /* restore path separator */
225 /* point p to first char of next path element */
230 nutc_path_len
= strlen(path
) + 3;
231 nutc_path
= xmalloc(nutc_path_len
);
238 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
239 * or the path was a single filename and will be converted
240 * here. Note, testing p here assures that we don't trip up
241 * on paths like a;b; which have trailing delimiter followed by
246 if (_NutPathToNutc(p
, pathp
, 0) == FALSE
) {
248 rval
= savestring(path
, strlen(path
));
256 strcpy(pathp
, &p
[2]);
259 *(pathp
-1) = '\0'; /* we're already done, don't leave trailing : */
261 rval
= savestring(nutc_path
, strlen(nutc_path
));