[503] | 1 | /* Path conversion for Windows pathnames.
|
---|
[3140] | 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc.
|
---|
[503] | 3 | This file is part of GNU Make.
|
---|
| 4 |
|
---|
| 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
|
---|
[1993] | 7 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
| 8 | version.
|
---|
[503] | 9 |
|
---|
| 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.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU General Public License along with
|
---|
[1993] | 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
[503] | 16 |
|
---|
[3140] | 17 | #include "makeint.h"
|
---|
[53] | 18 | #include <string.h>
|
---|
| 19 | #include <stdlib.h>
|
---|
| 20 | #include "pathstuff.h"
|
---|
[2849] | 21 | #if 1 /* bird */
|
---|
| 22 | # include "nt_fullpath.h"
|
---|
[3186] | 23 | # include <assert.h>
|
---|
[2849] | 24 | #endif
|
---|
[53] | 25 |
|
---|
| 26 | /*
|
---|
| 27 | * Convert delimiter separated vpath to Canonical format.
|
---|
| 28 | */
|
---|
| 29 | char *
|
---|
| 30 | convert_vpath_to_windows32(char *Path, char to_delim)
|
---|
| 31 | {
|
---|
| 32 | char *etok; /* token separator for old Path */
|
---|
| 33 |
|
---|
[3140] | 34 | /*
|
---|
| 35 | * Convert all spaces to delimiters. Note that pathnames which
|
---|
| 36 | * contain blanks get trounced here. Use 8.3 format as a workaround.
|
---|
| 37 | */
|
---|
| 38 | for (etok = Path; etok && *etok; etok++)
|
---|
| 39 | if (ISBLANK ((unsigned char) *etok))
|
---|
| 40 | *etok = to_delim;
|
---|
[53] | 41 |
|
---|
[3140] | 42 | return (convert_Path_to_windows32(Path, to_delim));
|
---|
[53] | 43 | }
|
---|
| 44 |
|
---|
| 45 | /*
|
---|
| 46 | * Convert delimiter separated path to Canonical format.
|
---|
| 47 | */
|
---|
| 48 | char *
|
---|
| 49 | convert_Path_to_windows32(char *Path, char to_delim)
|
---|
| 50 | {
|
---|
| 51 | char *etok; /* token separator for old Path */
|
---|
| 52 | char *p; /* points to element of old Path */
|
---|
| 53 |
|
---|
| 54 | /* is this a multi-element Path ? */
|
---|
[2454] | 55 | /* FIXME: Perhaps use ":;\"" in strpbrk to convert all quotes to
|
---|
| 56 | delimiters as well, as a way to handle quoted directories in
|
---|
| 57 | PATH? */
|
---|
[53] | 58 | for (p = Path, etok = strpbrk(p, ":;");
|
---|
| 59 | etok;
|
---|
| 60 | etok = strpbrk(p, ":;"))
|
---|
| 61 | if ((etok - p) == 1) {
|
---|
| 62 | if (*(etok - 1) == ';' ||
|
---|
| 63 | *(etok - 1) == ':') {
|
---|
| 64 | etok[-1] = to_delim;
|
---|
| 65 | etok[0] = to_delim;
|
---|
| 66 | p = ++etok;
|
---|
| 67 | continue; /* ignore empty bucket */
|
---|
| 68 | } else if (!isalpha ((unsigned char) *p)) {
|
---|
| 69 | /* found one to count, handle things like '.' */
|
---|
| 70 | *etok = to_delim;
|
---|
| 71 | p = ++etok;
|
---|
| 72 | } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
|
---|
| 73 | /* found one to count, handle drive letter */
|
---|
| 74 | *etok = to_delim;
|
---|
| 75 | p = ++etok;
|
---|
| 76 | } else
|
---|
| 77 | /* all finished, force abort */
|
---|
| 78 | p += strlen(p);
|
---|
[1993] | 79 | } else if (*p == '"') { /* a quoted directory */
|
---|
| 80 | for (p++; *p && *p != '"'; p++) /* skip quoted part */
|
---|
| 81 | ;
|
---|
| 82 | etok = strpbrk(p, ":;"); /* find next delimiter */
|
---|
[2454] | 83 | if (etok) {
|
---|
| 84 | *etok = to_delim;
|
---|
| 85 | p = ++etok;
|
---|
[3140] | 86 | } else
|
---|
[2454] | 87 | p += strlen(p);
|
---|
[53] | 88 | } else {
|
---|
| 89 | /* found another one, no drive letter */
|
---|
| 90 | *etok = to_delim;
|
---|
| 91 | p = ++etok;
|
---|
[1993] | 92 | }
|
---|
[53] | 93 |
|
---|
| 94 | return Path;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[304] | 97 | /*
|
---|
[3186] | 98 | * Convert to forward slashes directly (w32ify(filename, 0)).
|
---|
| 99 | */
|
---|
| 100 | char *unix_slashes(char *filename) /* bird */
|
---|
| 101 | {
|
---|
| 102 | char *slash = filename ;
|
---|
| 103 | while ((slash = strchr(slash, '\\')) != NULL)
|
---|
| 104 | *slash++ = '/';
|
---|
| 105 | return filename;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /*
|
---|
| 109 | * Resolve and convert to forward slashes directly (w32ify(filename, 1)).
|
---|
| 110 | * Returns if out of buffer space.
|
---|
| 111 | */
|
---|
| 112 | char *unix_slashes_resolved(const char *src, char *dst, unsigned len)
|
---|
| 113 | {
|
---|
| 114 | assert(len >= FILENAME_MAX);
|
---|
| 115 | *dst = '\0'; /** @todo nt_fullpath_cached needs to return some indication of overflow. */
|
---|
| 116 | #if 1
|
---|
| 117 | nt_fullpath_cached(src, dst, len);
|
---|
| 118 | #else
|
---|
| 119 | _fullpath(dst, src, len);
|
---|
| 120 | #endif
|
---|
| 121 |
|
---|
| 122 | return unix_slashes(dst);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | #if 0 /* bird: replaced by unix_slashes and unix_slahes_resolved. */
|
---|
| 126 | /*
|
---|
[53] | 127 | * Convert to forward slashes. Resolve to full pathname optionally
|
---|
| 128 | */
|
---|
| 129 | char *
|
---|
[903] | 130 | w32ify(const char *filename, int resolve)
|
---|
[53] | 131 | {
|
---|
| 132 | static char w32_path[FILENAME_MAX];
|
---|
[3186] | 133 | #if 1 /* bird */
|
---|
[53] | 134 |
|
---|
[529] | 135 | if (resolve) {
|
---|
[2848] | 136 | nt_fullpath_cached(filename, w32_path, sizeof(w32_path));
|
---|
[2317] | 137 | } else {
|
---|
| 138 | w32_path[0] = '\0';
|
---|
| 139 | strncat(w32_path, filename, sizeof(w32_path));
|
---|
| 140 | }
|
---|
[3186] | 141 | return unix_slashes(w32_path);
|
---|
| 142 |
|
---|
[1127] | 143 | #else /* !bird */
|
---|
[3186] | 144 | char *p;
|
---|
| 145 |
|
---|
[2317] | 146 | if (resolve) {
|
---|
[53] | 147 | _fullpath(w32_path, filename, sizeof (w32_path));
|
---|
[529] | 148 | } else
|
---|
[53] | 149 | strncpy(w32_path, filename, sizeof (w32_path));
|
---|
| 150 |
|
---|
| 151 | for (p = w32_path; p && *p; p++)
|
---|
| 152 | if (*p == '\\')
|
---|
| 153 | *p = '/';
|
---|
| 154 |
|
---|
| 155 | return w32_path;
|
---|
[3186] | 156 | #endif /* !bird */
|
---|
[53] | 157 | }
|
---|
[3186] | 158 | #endif
|
---|
[53] | 159 |
|
---|
| 160 | char *
|
---|
| 161 | getcwd_fs(char* buf, int len)
|
---|
| 162 | {
|
---|
[3140] | 163 | char *p = getcwd(buf, len);
|
---|
[53] | 164 |
|
---|
[3140] | 165 | if (p) {
|
---|
[3186] | 166 | #if 1
|
---|
| 167 | p = unix_slashes(p);
|
---|
| 168 | #else
|
---|
[3140] | 169 | char *q = w32ify(buf, 0);
|
---|
| 170 | #if 1 /* bird - UPSTREAM? */
|
---|
[3186] | 171 | buf[0] = '\0';
|
---|
| 172 | strncat(buf, q, len);
|
---|
[2317] | 173 | #else /* !bird */
|
---|
[3140] | 174 | strncpy(buf, q, len);
|
---|
| 175 | #endif
|
---|
[3186] | 176 | #endif
|
---|
[3140] | 177 | }
|
---|
[53] | 178 |
|
---|
[3140] | 179 | return p;
|
---|
[53] | 180 | }
|
---|
| 181 |
|
---|
| 182 | #ifdef unused
|
---|
| 183 | /*
|
---|
| 184 | * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
|
---|
| 185 | * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
|
---|
| 186 | * _NutPathToNutc() fails to convert, just return the path we were handed
|
---|
| 187 | * and assume the caller will know what to do with it (It was probably
|
---|
| 188 | * a mistake to try and convert it anyway due to some of the bizarre things
|
---|
| 189 | * that might look like pathnames in makefiles).
|
---|
| 190 | */
|
---|
| 191 | char *
|
---|
| 192 | convert_path_to_nutc(char *path)
|
---|
| 193 | {
|
---|
| 194 | int count; /* count of path elements */
|
---|
| 195 | char *nutc_path; /* new NutC path */
|
---|
| 196 | int nutc_path_len; /* length of buffer to allocate for new path */
|
---|
| 197 | char *pathp; /* pointer to nutc_path used to build it */
|
---|
| 198 | char *etok; /* token separator for old path */
|
---|
| 199 | char *p; /* points to element of old path */
|
---|
| 200 | char sep; /* what flavor of separator used in old path */
|
---|
| 201 | char *rval;
|
---|
| 202 |
|
---|
| 203 | /* is this a multi-element path ? */
|
---|
| 204 | for (p = path, etok = strpbrk(p, ":;"), count = 0;
|
---|
| 205 | etok;
|
---|
| 206 | etok = strpbrk(p, ":;"))
|
---|
| 207 | if ((etok - p) == 1) {
|
---|
| 208 | if (*(etok - 1) == ';' ||
|
---|
| 209 | *(etok - 1) == ':') {
|
---|
| 210 | p = ++etok;
|
---|
| 211 | continue; /* ignore empty bucket */
|
---|
| 212 | } else if (etok = strpbrk(etok+1, ":;"))
|
---|
| 213 | /* found one to count, handle drive letter */
|
---|
| 214 | p = ++etok, count++;
|
---|
| 215 | else
|
---|
| 216 | /* all finished, force abort */
|
---|
| 217 | p += strlen(p);
|
---|
| 218 | } else
|
---|
| 219 | /* found another one, no drive letter */
|
---|
| 220 | p = ++etok, count++;
|
---|
| 221 |
|
---|
| 222 | if (count) {
|
---|
| 223 | count++; /* x1;x2;x3 <- need to count x3 */
|
---|
| 224 |
|
---|
| 225 | /*
|
---|
| 226 | * Hazard a guess on how big the buffer needs to be.
|
---|
| 227 | * We have to convert things like c:/foo to /c=/foo.
|
---|
| 228 | */
|
---|
| 229 | nutc_path_len = strlen(path) + (count*2) + 1;
|
---|
| 230 | nutc_path = xmalloc(nutc_path_len);
|
---|
| 231 | pathp = nutc_path;
|
---|
| 232 | *pathp = '\0';
|
---|
| 233 |
|
---|
| 234 | /*
|
---|
| 235 | * Loop through PATH and convert one elemnt of the path at at
|
---|
| 236 | * a time. Single file pathnames will fail this and fall
|
---|
| 237 | * to the logic below loop.
|
---|
| 238 | */
|
---|
| 239 | for (p = path, etok = strpbrk(p, ":;");
|
---|
| 240 | etok;
|
---|
| 241 | etok = strpbrk(p, ":;")) {
|
---|
| 242 |
|
---|
| 243 | /* don't trip up on device specifiers or empty path slots */
|
---|
| 244 | if ((etok - p) == 1)
|
---|
| 245 | if (*(etok - 1) == ';' ||
|
---|
| 246 | *(etok - 1) == ':') {
|
---|
| 247 | p = ++etok;
|
---|
| 248 | continue;
|
---|
| 249 | } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
|
---|
| 250 | break; /* thing found was a WINDOWS32 pathname */
|
---|
| 251 |
|
---|
| 252 | /* save separator */
|
---|
| 253 | sep = *etok;
|
---|
| 254 |
|
---|
| 255 | /* terminate the current path element -- temporarily */
|
---|
| 256 | *etok = '\0';
|
---|
| 257 |
|
---|
| 258 | #ifdef __NUTC__
|
---|
| 259 | /* convert to NutC format */
|
---|
| 260 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
---|
| 261 | free(nutc_path);
|
---|
| 262 | rval = savestring(path, strlen(path));
|
---|
| 263 | return rval;
|
---|
| 264 | }
|
---|
| 265 | #else
|
---|
| 266 | *pathp++ = '/';
|
---|
| 267 | *pathp++ = p[0];
|
---|
| 268 | *pathp++ = '=';
|
---|
| 269 | *pathp++ = '/';
|
---|
| 270 | strcpy(pathp, &p[2]);
|
---|
| 271 | #endif
|
---|
| 272 |
|
---|
| 273 | pathp += strlen(pathp);
|
---|
| 274 | *pathp++ = ':'; /* use Unix style path separtor for new path */
|
---|
| 275 | *pathp = '\0'; /* make sure we are null terminaed */
|
---|
| 276 |
|
---|
| 277 | /* restore path separator */
|
---|
| 278 | *etok = sep;
|
---|
| 279 |
|
---|
| 280 | /* point p to first char of next path element */
|
---|
| 281 | p = ++etok;
|
---|
| 282 |
|
---|
| 283 | }
|
---|
| 284 | } else {
|
---|
| 285 | nutc_path_len = strlen(path) + 3;
|
---|
| 286 | nutc_path = xmalloc(nutc_path_len);
|
---|
| 287 | pathp = nutc_path;
|
---|
| 288 | *pathp = '\0';
|
---|
| 289 | p = path;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /*
|
---|
| 293 | * OK, here we handle the last element in PATH (e.g. c of a;b;c)
|
---|
| 294 | * or the path was a single filename and will be converted
|
---|
| 295 | * here. Note, testing p here assures that we don't trip up
|
---|
| 296 | * on paths like a;b; which have trailing delimiter followed by
|
---|
| 297 | * nothing.
|
---|
| 298 | */
|
---|
| 299 | if (*p != '\0') {
|
---|
| 300 | #ifdef __NUTC__
|
---|
| 301 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
---|
| 302 | free(nutc_path);
|
---|
| 303 | rval = savestring(path, strlen(path));
|
---|
| 304 | return rval;
|
---|
| 305 | }
|
---|
| 306 | #else
|
---|
| 307 | *pathp++ = '/';
|
---|
| 308 | *pathp++ = p[0];
|
---|
| 309 | *pathp++ = '=';
|
---|
| 310 | *pathp++ = '/';
|
---|
| 311 | strcpy(pathp, &p[2]);
|
---|
| 312 | #endif
|
---|
| 313 | } else
|
---|
| 314 | *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
|
---|
| 315 |
|
---|
| 316 | rval = savestring(nutc_path, strlen(nutc_path));
|
---|
| 317 | free(nutc_path);
|
---|
| 318 | return rval;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | #endif
|
---|