1 /* Path conversion for Windows pathnames.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 3 of the License, or (at your option) any later
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "pathstuff.h"
24 * Convert delimiter separated vpath to Canonical format.
27 convert_vpath_to_windows32(char *Path
, char to_delim
)
29 char *etok
; /* token separator for old Path */
32 * Convert all spaces to delimiters. Note that pathnames which
33 * contain blanks get trounced here. Use 8.3 format as a workaround.
35 for (etok
= Path
; etok
&& *etok
; etok
++)
36 if (isblank ((unsigned char) *etok
))
39 return (convert_Path_to_windows32(Path
, to_delim
));
43 * Convert delimiter separated path to Canonical format.
46 convert_Path_to_windows32(char *Path
, char to_delim
)
48 char *etok
; /* token separator for old Path */
49 char *p
; /* points to element of old Path */
51 /* is this a multi-element Path ? */
52 /* FIXME: Perhaps use ":;\"" in strpbrk to convert all quotes to
53 delimiters as well, as a way to handle quoted directories in
55 for (p
= Path
, etok
= strpbrk(p
, ":;");
57 etok
= strpbrk(p
, ":;"))
58 if ((etok
- p
) == 1) {
59 if (*(etok
- 1) == ';' ||
64 continue; /* ignore empty bucket */
65 } else if (!isalpha ((unsigned char) *p
)) {
66 /* found one to count, handle things like '.' */
69 } else if ((*etok
== ':') && (etok
= strpbrk(etok
+1, ":;"))) {
70 /* found one to count, handle drive letter */
74 /* all finished, force abort */
76 } else if (*p
== '"') { /* a quoted directory */
77 for (p
++; *p
&& *p
!= '"'; p
++) /* skip quoted part */
79 etok
= strpbrk(p
, ":;"); /* find next delimiter */
86 /* found another one, no drive letter */
95 * Convert to forward slashes. Resolve to full pathname optionally
98 w32ify(const char *filename
, int resolve
)
100 static char w32_path
[FILENAME_MAX
];
104 _fullpath(w32_path
, filename
, sizeof (w32_path
));
106 strncpy(w32_path
, filename
, sizeof (w32_path
));
108 for (p
= w32_path
; p
&& *p
; p
++)
116 getcwd_fs(char* buf
, int len
)
118 char *p
= getcwd(buf
, len
);
121 char *q
= w32ify(buf
, 0);
122 strncpy(buf
, q
, len
);
130 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
131 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
132 * _NutPathToNutc() fails to convert, just return the path we were handed
133 * and assume the caller will know what to do with it (It was probably
134 * a mistake to try and convert it anyway due to some of the bizarre things
135 * that might look like pathnames in makefiles).
138 convert_path_to_nutc(char *path
)
140 int count
; /* count of path elements */
141 char *nutc_path
; /* new NutC path */
142 int nutc_path_len
; /* length of buffer to allocate for new path */
143 char *pathp
; /* pointer to nutc_path used to build it */
144 char *etok
; /* token separator for old path */
145 char *p
; /* points to element of old path */
146 char sep
; /* what flavor of separator used in old path */
149 /* is this a multi-element path ? */
150 for (p
= path
, etok
= strpbrk(p
, ":;"), count
= 0;
152 etok
= strpbrk(p
, ":;"))
153 if ((etok
- p
) == 1) {
154 if (*(etok
- 1) == ';' ||
155 *(etok
- 1) == ':') {
157 continue; /* ignore empty bucket */
158 } else if (etok
= strpbrk(etok
+1, ":;"))
159 /* found one to count, handle drive letter */
162 /* all finished, force abort */
165 /* found another one, no drive letter */
169 count
++; /* x1;x2;x3 <- need to count x3 */
172 * Hazard a guess on how big the buffer needs to be.
173 * We have to convert things like c:/foo to /c=/foo.
175 nutc_path_len
= strlen(path
) + (count
*2) + 1;
176 nutc_path
= xmalloc(nutc_path_len
);
181 * Loop through PATH and convert one elemnt of the path at at
182 * a time. Single file pathnames will fail this and fall
183 * to the logic below loop.
185 for (p
= path
, etok
= strpbrk(p
, ":;");
187 etok
= strpbrk(p
, ":;")) {
189 /* don't trip up on device specifiers or empty path slots */
191 if (*(etok
- 1) == ';' ||
192 *(etok
- 1) == ':') {
195 } else if ((etok
= strpbrk(etok
+1, ":;")) == NULL
)
196 break; /* thing found was a WINDOWS32 pathname */
201 /* terminate the current path element -- temporarily */
205 /* convert to NutC format */
206 if (_NutPathToNutc(p
, pathp
, 0) == FALSE
) {
208 rval
= savestring(path
, strlen(path
));
216 strcpy(pathp
, &p
[2]);
219 pathp
+= strlen(pathp
);
220 *pathp
++ = ':'; /* use Unix style path separtor for new path */
221 *pathp
= '\0'; /* make sure we are null terminaed */
223 /* restore path separator */
226 /* point p to first char of next path element */
231 nutc_path_len
= strlen(path
) + 3;
232 nutc_path
= xmalloc(nutc_path_len
);
239 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
240 * or the path was a single filename and will be converted
241 * here. Note, testing p here assures that we don't trip up
242 * on paths like a;b; which have trailing delimiter followed by
247 if (_NutPathToNutc(p
, pathp
, 0) == FALSE
) {
249 rval
= savestring(path
, strlen(path
));
257 strcpy(pathp
, &p
[2]);
260 *(pathp
-1) = '\0'; /* we're already done, don't leave trailing : */
262 rval
= savestring(nutc_path
, strlen(nutc_path
));