| 1 | /* Path conversion for Windows pathnames.
|
|---|
| 2 | Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
|---|
| 3 | 2006 Free Software Foundation, Inc.
|
|---|
| 4 | This file is part of GNU Make.
|
|---|
| 5 |
|
|---|
| 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 2, or (at your option) any later version.
|
|---|
| 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
|
|---|
| 15 | GNU Make; see the file COPYING. If not, write to the Free Software
|
|---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
|---|
| 17 |
|
|---|
| 18 | #include <Windows.h> /* bird */
|
|---|
| 19 | #include <stdio.h> /* bird */
|
|---|
| 20 | #include <string.h>
|
|---|
| 21 | #include <stdlib.h>
|
|---|
| 22 | #include "make.h"
|
|---|
| 23 | #include "pathstuff.h"
|
|---|
| 24 |
|
|---|
| 25 | /*
|
|---|
| 26 | * Convert delimiter separated vpath to Canonical format.
|
|---|
| 27 | */
|
|---|
| 28 | char *
|
|---|
| 29 | convert_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 | */
|
|---|
| 47 | char *
|
|---|
| 48 | convert_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 | for (p = Path, etok = strpbrk(p, ":;");
|
|---|
| 55 | etok;
|
|---|
| 56 | etok = strpbrk(p, ":;"))
|
|---|
| 57 | if ((etok - p) == 1) {
|
|---|
| 58 | if (*(etok - 1) == ';' ||
|
|---|
| 59 | *(etok - 1) == ':') {
|
|---|
| 60 | etok[-1] = to_delim;
|
|---|
| 61 | etok[0] = to_delim;
|
|---|
| 62 | p = ++etok;
|
|---|
| 63 | continue; /* ignore empty bucket */
|
|---|
| 64 | } else if (!isalpha ((unsigned char) *p)) {
|
|---|
| 65 | /* found one to count, handle things like '.' */
|
|---|
| 66 | *etok = to_delim;
|
|---|
| 67 | p = ++etok;
|
|---|
| 68 | } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
|
|---|
| 69 | /* found one to count, handle drive letter */
|
|---|
| 70 | *etok = to_delim;
|
|---|
| 71 | p = ++etok;
|
|---|
| 72 | } else
|
|---|
| 73 | /* all finished, force abort */
|
|---|
| 74 | p += strlen(p);
|
|---|
| 75 | } else {
|
|---|
| 76 | /* found another one, no drive letter */
|
|---|
| 77 | *etok = to_delim;
|
|---|
| 78 | p = ++etok;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | return Path;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | * Corrects the case of a path.
|
|---|
| 86 | * Expects a fullpath!
|
|---|
| 87 | * Added by bird for the $(abspath ) function and w32ify
|
|---|
| 88 | */
|
|---|
| 89 | void
|
|---|
| 90 | w32_fixcase(char *pszPath)
|
|---|
| 91 | {
|
|---|
| 92 | static char s_szLast[260];
|
|---|
| 93 | unsigned cchLast;
|
|---|
| 94 |
|
|---|
| 95 | #ifndef NDEBUG
|
|---|
| 96 | # define my_assert(expr) \
|
|---|
| 97 | do { \
|
|---|
| 98 | if (!(expr)) { \
|
|---|
| 99 | printf("my_assert: %s, file %s, line %d\npszPath=%s\npsz=%s\n", \
|
|---|
| 100 | #expr, __FILE__, __LINE__, pszPath, psz); \
|
|---|
| 101 | __asm { __asm int 3 } \
|
|---|
| 102 | exit(1); \
|
|---|
| 103 | } \
|
|---|
| 104 | } while (0)
|
|---|
| 105 | #else
|
|---|
| 106 | # define my_assert(expr) do {} while (0)
|
|---|
| 107 | #endif
|
|---|
| 108 |
|
|---|
| 109 | char *psz = pszPath;
|
|---|
| 110 | if (*psz == '/' || *psz == '\\')
|
|---|
| 111 | {
|
|---|
| 112 | if (psz[1] == '/' || psz[1] == '\\')
|
|---|
| 113 | {
|
|---|
| 114 | /* UNC */
|
|---|
| 115 | my_assert(psz[1] == '/' || psz[1] == '\\');
|
|---|
| 116 | my_assert(psz[2] != '/' && psz[2] != '\\');
|
|---|
| 117 |
|
|---|
| 118 | /* skip server name */
|
|---|
| 119 | psz += 2;
|
|---|
| 120 | while (*psz != '\\' && *psz != '/')
|
|---|
| 121 | {
|
|---|
| 122 | if (!*psz)
|
|---|
| 123 | return;
|
|---|
| 124 | *psz++ = toupper(*psz);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /* skip the share name */
|
|---|
| 128 | psz++;
|
|---|
| 129 | my_assert(*psz != '/' && *psz != '\\');
|
|---|
| 130 | while (*psz != '\\' && *psz != '/')
|
|---|
| 131 | {
|
|---|
| 132 | if (!*psz)
|
|---|
| 133 | return;
|
|---|
| 134 | *psz++ = toupper(*psz);
|
|---|
| 135 | }
|
|---|
| 136 | my_assert(*psz == '/' || *psz == '\\');
|
|---|
| 137 | psz++;
|
|---|
| 138 | }
|
|---|
| 139 | else
|
|---|
| 140 | {
|
|---|
| 141 | /* Unix spec */
|
|---|
| 142 | psz++;
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | else
|
|---|
| 146 | {
|
|---|
| 147 | /* Drive letter */
|
|---|
| 148 | my_assert(psz[1] == ':');
|
|---|
| 149 | *psz = toupper(*psz);
|
|---|
| 150 | my_assert(psz[0] >= 'A' && psz[0] <= 'Z');
|
|---|
| 151 | my_assert(psz[2] == '/' || psz[2] == '\\');
|
|---|
| 152 | psz += 3;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /*
|
|---|
| 156 | * Try make use of the result from the previous call.
|
|---|
| 157 | * This is ignorant to slashes and similar, but may help even so.
|
|---|
| 158 | */
|
|---|
| 159 | if ( s_szLast[0] == pszPath[0]
|
|---|
| 160 | && (psz - pszPath == 1 || s_szLast[1] == pszPath[1])
|
|---|
| 161 | && (psz - pszPath <= 2 || s_szLast[2] == pszPath[2])
|
|---|
| 162 | )
|
|---|
| 163 | {
|
|---|
| 164 | char *pszLast = &s_szLast[psz - pszPath];
|
|---|
| 165 | char *pszCur = psz;
|
|---|
| 166 | for (;;)
|
|---|
| 167 | {
|
|---|
| 168 | const char ch1 = *pszCur;
|
|---|
| 169 | const char ch2 = *pszLast;
|
|---|
| 170 | if ( ch1 != ch2
|
|---|
| 171 | && (ch1 != '\\' || ch2 != '/')
|
|---|
| 172 | && (ch1 != '/' || ch2 != '\\'))
|
|---|
| 173 | {
|
|---|
| 174 | if ( tolower(ch1) != tolower(ch2)
|
|---|
| 175 | && toupper(ch1) != toupper(ch2))
|
|---|
| 176 | break;
|
|---|
| 177 | /* optimistic, component mismatch will be corrected in the next loop. */
|
|---|
| 178 | *pszCur = ch2;
|
|---|
| 179 | }
|
|---|
| 180 | if (ch1 == '/' || ch1 == '\\')
|
|---|
| 181 | psz = pszCur + 1;
|
|---|
| 182 | else if (ch1 == '\0')
|
|---|
| 183 | {
|
|---|
| 184 | psz = pszCur;
|
|---|
| 185 | break;
|
|---|
| 186 | }
|
|---|
| 187 | pszCur++;
|
|---|
| 188 | pszLast++;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | * Pointing to the first char after the unc or drive specifier,
|
|---|
| 194 | * or in case of a cache hit, the first non-matching char (following a slash of course).
|
|---|
| 195 | */
|
|---|
| 196 | while (*psz)
|
|---|
| 197 | {
|
|---|
| 198 | WIN32_FIND_DATA FindFileData;
|
|---|
| 199 | HANDLE hDir;
|
|---|
| 200 | char chSaved0;
|
|---|
| 201 | char chSaved1;
|
|---|
| 202 | char *pszEnd;
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 | /* find the end of the component. */
|
|---|
| 206 | pszEnd = psz;
|
|---|
| 207 | while (*pszEnd && *pszEnd != '/' && *pszEnd != '\\')
|
|---|
| 208 | pszEnd++;
|
|---|
| 209 |
|
|---|
| 210 | /* replace the end with "?\0" */
|
|---|
| 211 | chSaved0 = pszEnd[0];
|
|---|
| 212 | chSaved1 = pszEnd[1];
|
|---|
| 213 | pszEnd[0] = '?';
|
|---|
| 214 | pszEnd[1] = '\0';
|
|---|
| 215 |
|
|---|
| 216 | /* find the right filename. */
|
|---|
| 217 | hDir = FindFirstFile(pszPath, &FindFileData);
|
|---|
| 218 | pszEnd[1] = chSaved1;
|
|---|
| 219 | if (!hDir)
|
|---|
| 220 | {
|
|---|
| 221 | cchLast = psz - pszPath;
|
|---|
| 222 | memcpy(s_szLast, pszPath, cchLast + 1);
|
|---|
| 223 | pszEnd[0] = chSaved0;
|
|---|
| 224 | return;
|
|---|
| 225 | }
|
|---|
| 226 | pszEnd[0] = '\0';
|
|---|
| 227 | while (stricmp(FindFileData.cFileName, psz))
|
|---|
| 228 | {
|
|---|
| 229 | if (!FindNextFile(hDir, &FindFileData))
|
|---|
| 230 | {
|
|---|
| 231 | cchLast = psz - pszPath;
|
|---|
| 232 | memcpy(s_szLast, pszPath, cchLast + 1);
|
|---|
| 233 | pszEnd[0] = chSaved0;
|
|---|
| 234 | return;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 | strcpy(psz, FindFileData.cFileName);
|
|---|
| 238 | pszEnd[0] = chSaved0;
|
|---|
| 239 |
|
|---|
| 240 | /* advance to the next component */
|
|---|
| 241 | if (!chSaved0)
|
|---|
| 242 | {
|
|---|
| 243 | psz = pszEnd;
|
|---|
| 244 | break;
|
|---|
| 245 | }
|
|---|
| 246 | psz = pszEnd + 1;
|
|---|
| 247 | my_assert(*psz != '/' && *psz != '\\');
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /* *psz == '\0', the end. */
|
|---|
| 251 | cchLast = psz - pszPath;
|
|---|
| 252 | memcpy(s_szLast, pszPath, cchLast + 1);
|
|---|
| 253 | #undef my_assert
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /*
|
|---|
| 257 | * Convert to forward slashes. Resolve to full pathname optionally
|
|---|
| 258 | */
|
|---|
| 259 | char *
|
|---|
| 260 | w32ify(char *filename, int resolve)
|
|---|
| 261 | {
|
|---|
| 262 | static char w32_path[FILENAME_MAX];
|
|---|
| 263 | char *p;
|
|---|
| 264 |
|
|---|
| 265 | if (resolve) {
|
|---|
| 266 | _fullpath(w32_path, filename, sizeof (w32_path));
|
|---|
| 267 | w32_fixcase(w32_path); /* bird */
|
|---|
| 268 | } else
|
|---|
| 269 | strncpy(w32_path, filename, sizeof (w32_path));
|
|---|
| 270 |
|
|---|
| 271 | for (p = w32_path; p && *p; p++)
|
|---|
| 272 | if (*p == '\\')
|
|---|
| 273 | *p = '/';
|
|---|
| 274 |
|
|---|
| 275 | return w32_path;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | char *
|
|---|
| 279 | getcwd_fs(char* buf, int len)
|
|---|
| 280 | {
|
|---|
| 281 | char *p = getcwd(buf, len);
|
|---|
| 282 |
|
|---|
| 283 | if (p) {
|
|---|
| 284 | char *q = w32ify(buf, 0);
|
|---|
| 285 | strncpy(buf, q, len);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | return p;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | #undef stat
|
|---|
| 292 | /*
|
|---|
| 293 | * Workaround for directory names with trailing slashes.
|
|---|
| 294 | * Added by bird reasons stated.
|
|---|
| 295 | */
|
|---|
| 296 | int
|
|---|
| 297 | my_stat(const char *path, struct stat *st)
|
|---|
| 298 | {
|
|---|
| 299 | int rc = stat(path, st);
|
|---|
| 300 | if ( rc != 0
|
|---|
| 301 | && errno == ENOENT
|
|---|
| 302 | && *path != '\0')
|
|---|
| 303 | {
|
|---|
| 304 | char *slash = strchr(path, '\0') - 1;
|
|---|
| 305 | if (*slash == '/' || *slash == '\\')
|
|---|
| 306 | {
|
|---|
| 307 | size_t len_path = slash - path + 1;
|
|---|
| 308 | char *tmp = alloca(len_path + 4);
|
|---|
| 309 | memcpy(tmp, path, len_path);
|
|---|
| 310 | tmp[len_path] = '.';
|
|---|
| 311 | tmp[len_path + 1] = '\0';
|
|---|
| 312 | errno = 0;
|
|---|
| 313 | rc = stat(tmp, st);
|
|---|
| 314 | if ( rc == 0
|
|---|
| 315 | && !S_ISDIR(st->st_mode))
|
|---|
| 316 | {
|
|---|
| 317 | errno = ENOTDIR;
|
|---|
| 318 | rc = -1;
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 | return rc;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | #ifdef unused
|
|---|
| 326 | /*
|
|---|
| 327 | * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
|
|---|
| 328 | * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
|
|---|
| 329 | * _NutPathToNutc() fails to convert, just return the path we were handed
|
|---|
| 330 | * and assume the caller will know what to do with it (It was probably
|
|---|
| 331 | * a mistake to try and convert it anyway due to some of the bizarre things
|
|---|
| 332 | * that might look like pathnames in makefiles).
|
|---|
| 333 | */
|
|---|
| 334 | char *
|
|---|
| 335 | convert_path_to_nutc(char *path)
|
|---|
| 336 | {
|
|---|
| 337 | int count; /* count of path elements */
|
|---|
| 338 | char *nutc_path; /* new NutC path */
|
|---|
| 339 | int nutc_path_len; /* length of buffer to allocate for new path */
|
|---|
| 340 | char *pathp; /* pointer to nutc_path used to build it */
|
|---|
| 341 | char *etok; /* token separator for old path */
|
|---|
| 342 | char *p; /* points to element of old path */
|
|---|
| 343 | char sep; /* what flavor of separator used in old path */
|
|---|
| 344 | char *rval;
|
|---|
| 345 |
|
|---|
| 346 | /* is this a multi-element path ? */
|
|---|
| 347 | for (p = path, etok = strpbrk(p, ":;"), count = 0;
|
|---|
| 348 | etok;
|
|---|
| 349 | etok = strpbrk(p, ":;"))
|
|---|
| 350 | if ((etok - p) == 1) {
|
|---|
| 351 | if (*(etok - 1) == ';' ||
|
|---|
| 352 | *(etok - 1) == ':') {
|
|---|
| 353 | p = ++etok;
|
|---|
| 354 | continue; /* ignore empty bucket */
|
|---|
| 355 | } else if (etok = strpbrk(etok+1, ":;"))
|
|---|
| 356 | /* found one to count, handle drive letter */
|
|---|
| 357 | p = ++etok, count++;
|
|---|
| 358 | else
|
|---|
| 359 | /* all finished, force abort */
|
|---|
| 360 | p += strlen(p);
|
|---|
| 361 | } else
|
|---|
| 362 | /* found another one, no drive letter */
|
|---|
| 363 | p = ++etok, count++;
|
|---|
| 364 |
|
|---|
| 365 | if (count) {
|
|---|
| 366 | count++; /* x1;x2;x3 <- need to count x3 */
|
|---|
| 367 |
|
|---|
| 368 | /*
|
|---|
| 369 | * Hazard a guess on how big the buffer needs to be.
|
|---|
| 370 | * We have to convert things like c:/foo to /c=/foo.
|
|---|
| 371 | */
|
|---|
| 372 | nutc_path_len = strlen(path) + (count*2) + 1;
|
|---|
| 373 | nutc_path = xmalloc(nutc_path_len);
|
|---|
| 374 | pathp = nutc_path;
|
|---|
| 375 | *pathp = '\0';
|
|---|
| 376 |
|
|---|
| 377 | /*
|
|---|
| 378 | * Loop through PATH and convert one elemnt of the path at at
|
|---|
| 379 | * a time. Single file pathnames will fail this and fall
|
|---|
| 380 | * to the logic below loop.
|
|---|
| 381 | */
|
|---|
| 382 | for (p = path, etok = strpbrk(p, ":;");
|
|---|
| 383 | etok;
|
|---|
| 384 | etok = strpbrk(p, ":;")) {
|
|---|
| 385 |
|
|---|
| 386 | /* don't trip up on device specifiers or empty path slots */
|
|---|
| 387 | if ((etok - p) == 1)
|
|---|
| 388 | if (*(etok - 1) == ';' ||
|
|---|
| 389 | *(etok - 1) == ':') {
|
|---|
| 390 | p = ++etok;
|
|---|
| 391 | continue;
|
|---|
| 392 | } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
|
|---|
| 393 | break; /* thing found was a WINDOWS32 pathname */
|
|---|
| 394 |
|
|---|
| 395 | /* save separator */
|
|---|
| 396 | sep = *etok;
|
|---|
| 397 |
|
|---|
| 398 | /* terminate the current path element -- temporarily */
|
|---|
| 399 | *etok = '\0';
|
|---|
| 400 |
|
|---|
| 401 | #ifdef __NUTC__
|
|---|
| 402 | /* convert to NutC format */
|
|---|
| 403 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
|---|
| 404 | free(nutc_path);
|
|---|
| 405 | rval = savestring(path, strlen(path));
|
|---|
| 406 | return rval;
|
|---|
| 407 | }
|
|---|
| 408 | #else
|
|---|
| 409 | *pathp++ = '/';
|
|---|
| 410 | *pathp++ = p[0];
|
|---|
| 411 | *pathp++ = '=';
|
|---|
| 412 | *pathp++ = '/';
|
|---|
| 413 | strcpy(pathp, &p[2]);
|
|---|
| 414 | #endif
|
|---|
| 415 |
|
|---|
| 416 | pathp += strlen(pathp);
|
|---|
| 417 | *pathp++ = ':'; /* use Unix style path separtor for new path */
|
|---|
| 418 | *pathp = '\0'; /* make sure we are null terminaed */
|
|---|
| 419 |
|
|---|
| 420 | /* restore path separator */
|
|---|
| 421 | *etok = sep;
|
|---|
| 422 |
|
|---|
| 423 | /* point p to first char of next path element */
|
|---|
| 424 | p = ++etok;
|
|---|
| 425 |
|
|---|
| 426 | }
|
|---|
| 427 | } else {
|
|---|
| 428 | nutc_path_len = strlen(path) + 3;
|
|---|
| 429 | nutc_path = xmalloc(nutc_path_len);
|
|---|
| 430 | pathp = nutc_path;
|
|---|
| 431 | *pathp = '\0';
|
|---|
| 432 | p = path;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | /*
|
|---|
| 436 | * OK, here we handle the last element in PATH (e.g. c of a;b;c)
|
|---|
| 437 | * or the path was a single filename and will be converted
|
|---|
| 438 | * here. Note, testing p here assures that we don't trip up
|
|---|
| 439 | * on paths like a;b; which have trailing delimiter followed by
|
|---|
| 440 | * nothing.
|
|---|
| 441 | */
|
|---|
| 442 | if (*p != '\0') {
|
|---|
| 443 | #ifdef __NUTC__
|
|---|
| 444 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
|---|
| 445 | free(nutc_path);
|
|---|
| 446 | rval = savestring(path, strlen(path));
|
|---|
| 447 | return rval;
|
|---|
| 448 | }
|
|---|
| 449 | #else
|
|---|
| 450 | *pathp++ = '/';
|
|---|
| 451 | *pathp++ = p[0];
|
|---|
| 452 | *pathp++ = '=';
|
|---|
| 453 | *pathp++ = '/';
|
|---|
| 454 | strcpy(pathp, &p[2]);
|
|---|
| 455 | #endif
|
|---|
| 456 | } else
|
|---|
| 457 | *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
|
|---|
| 458 |
|
|---|
| 459 | rval = savestring(nutc_path, strlen(nutc_path));
|
|---|
| 460 | free(nutc_path);
|
|---|
| 461 | return rval;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | #endif
|
|---|