1 /* Path conversion for Windows pathnames.
2 Copyright (C) 1996-2022 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 <https://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, ":;")) != NULL)) {
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];
104 char *fp = _fullpath (NULL, filename, sizeof (w32_path));
105 strncpy (w32_path, fp, sizeof (w32_path) - 1);
109 strncpy(w32_path, filename, sizeof (w32_path) - 1);
111 for (p = w32_path; p && *p; p++)
119 getcwd_fs(char* buf, int len)
121 char *p = getcwd(buf, len);
124 char *q = w32ify(buf, 0);
125 strncpy(buf, q, len);
133 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
134 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
135 * _NutPathToNutc() fails to convert, just return the path we were handed
136 * and assume the caller will know what to do with it (It was probably
137 * a mistake to try and convert it anyway due to some of the bizarre things
138 * that might look like pathnames in makefiles).
141 convert_path_to_nutc(char *path)
143 int count; /* count of path elements */
144 char *nutc_path; /* new NutC path */
145 int nutc_path_len; /* length of buffer to allocate for new path */
146 char *pathp; /* pointer to nutc_path used to build it */
147 char *etok; /* token separator for old path */
148 char *p; /* points to element of old path */
149 char sep; /* what flavor of separator used in old path */
152 /* is this a multi-element path ? */
153 for (p = path, etok = strpbrk(p, ":;"), count = 0;
155 etok = strpbrk(p, ":;"))
156 if ((etok - p) == 1) {
157 if (*(etok - 1) == ';' ||
158 *(etok - 1) == ':') {
160 continue; /* ignore empty bucket */
161 } else if (etok = strpbrk(etok+1, ":;"))
162 /* found one to count, handle drive letter */
165 /* all finished, force abort */
168 /* found another one, no drive letter */
172 count++; /* x1;x2;x3 <- need to count x3 */
175 * Hazard a guess on how big the buffer needs to be.
176 * We have to convert things like c:/foo to /c=/foo.
178 nutc_path_len = strlen(path) + (count*2) + 1;
179 nutc_path = xmalloc(nutc_path_len);
184 * Loop through PATH and convert one element of the path at at
185 * a time. Single file pathnames will fail this and fall
186 * to the logic below loop.
188 for (p = path, etok = strpbrk(p, ":;");
190 etok = strpbrk(p, ":;")) {
192 /* don't trip up on device specifiers or empty path slots */
194 if (*(etok - 1) == ';' ||
195 *(etok - 1) == ':') {
198 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
199 break; /* thing found was a WINDOWS32 pathname */
204 /* terminate the current path element -- temporarily */
208 /* convert to NutC format */
209 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
211 rval = savestring(path, strlen(path));
219 strcpy(pathp, &p[2]);
222 pathp += strlen(pathp);
223 *pathp++ = ':'; /* use Unix style path separtor for new path */
224 *pathp = '\0'; /* make sure we are null terminaed */
226 /* restore path separator */
229 /* point p to first char of next path element */
234 nutc_path_len = strlen(path) + 3;
235 nutc_path = xmalloc(nutc_path_len);
242 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
243 * or the path was a single filename and will be converted
244 * here. Note, testing p here assures that we don't trip up
245 * on paths like a;b; which have trailing delimiter followed by
250 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
252 rval = savestring(path, strlen(path));
260 strcpy(pathp, &p[2]);
263 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
265 rval = savestring(nutc_path, strlen(nutc_path));