| 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 | #if 1 /* bird */
|
|---|
| 85 | extern void nt_fullpath(const char *pszPath, char *pszFull, size_t cchFull);
|
|---|
| 86 | #endif
|
|---|
| 87 |
|
|---|
| 88 | /*
|
|---|
| 89 | * Convert to forward slashes. Resolve to full pathname optionally
|
|---|
| 90 | */
|
|---|
| 91 | char *
|
|---|
| 92 | w32ify(const char *filename, int resolve)
|
|---|
| 93 | {
|
|---|
| 94 | static char w32_path[FILENAME_MAX];
|
|---|
| 95 | char *p;
|
|---|
| 96 |
|
|---|
| 97 | if (resolve) {
|
|---|
| 98 | #if 1 /* bird */
|
|---|
| 99 | nt_fullpath(filename, w32_path, sizeof(w32_path));
|
|---|
| 100 | #else /* !bird */
|
|---|
| 101 | _fullpath(w32_path, filename, sizeof (w32_path));
|
|---|
| 102 | #endif /* !bird */
|
|---|
| 103 | } else
|
|---|
| 104 | strncpy(w32_path, filename, sizeof (w32_path));
|
|---|
| 105 |
|
|---|
| 106 | for (p = w32_path; p && *p; p++)
|
|---|
| 107 | if (*p == '\\')
|
|---|
| 108 | *p = '/';
|
|---|
| 109 |
|
|---|
| 110 | return w32_path;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | char *
|
|---|
| 114 | getcwd_fs(char* buf, int len)
|
|---|
| 115 | {
|
|---|
| 116 | char *p = getcwd(buf, len);
|
|---|
| 117 |
|
|---|
| 118 | if (p) {
|
|---|
| 119 | char *q = w32ify(buf, 0);
|
|---|
| 120 | strncpy(buf, q, len);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | return p;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | #undef stat
|
|---|
| 127 | /*
|
|---|
| 128 | * Workaround for directory names with trailing slashes.
|
|---|
| 129 | * Added by bird reasons stated.
|
|---|
| 130 | */
|
|---|
| 131 | int
|
|---|
| 132 | my_stat(const char *path, struct stat *st)
|
|---|
| 133 | {
|
|---|
| 134 | int rc = stat(path, st);
|
|---|
| 135 | if ( rc != 0
|
|---|
| 136 | && errno == ENOENT
|
|---|
| 137 | && *path != '\0')
|
|---|
| 138 | {
|
|---|
| 139 | char *slash = strchr(path, '\0') - 1;
|
|---|
| 140 | if (*slash == '/' || *slash == '\\')
|
|---|
| 141 | {
|
|---|
| 142 | size_t len_path = slash - path + 1;
|
|---|
| 143 | char *tmp = alloca(len_path + 4);
|
|---|
| 144 | memcpy(tmp, path, len_path);
|
|---|
| 145 | tmp[len_path] = '.';
|
|---|
| 146 | tmp[len_path + 1] = '\0';
|
|---|
| 147 | errno = 0;
|
|---|
| 148 | rc = stat(tmp, st);
|
|---|
| 149 | if ( rc == 0
|
|---|
| 150 | && !S_ISDIR(st->st_mode))
|
|---|
| 151 | {
|
|---|
| 152 | errno = ENOTDIR;
|
|---|
| 153 | rc = -1;
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | return rc;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | #ifdef unused
|
|---|
| 161 | /*
|
|---|
| 162 | * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
|
|---|
| 163 | * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
|
|---|
| 164 | * _NutPathToNutc() fails to convert, just return the path we were handed
|
|---|
| 165 | * and assume the caller will know what to do with it (It was probably
|
|---|
| 166 | * a mistake to try and convert it anyway due to some of the bizarre things
|
|---|
| 167 | * that might look like pathnames in makefiles).
|
|---|
| 168 | */
|
|---|
| 169 | char *
|
|---|
| 170 | convert_path_to_nutc(char *path)
|
|---|
| 171 | {
|
|---|
| 172 | int count; /* count of path elements */
|
|---|
| 173 | char *nutc_path; /* new NutC path */
|
|---|
| 174 | int nutc_path_len; /* length of buffer to allocate for new path */
|
|---|
| 175 | char *pathp; /* pointer to nutc_path used to build it */
|
|---|
| 176 | char *etok; /* token separator for old path */
|
|---|
| 177 | char *p; /* points to element of old path */
|
|---|
| 178 | char sep; /* what flavor of separator used in old path */
|
|---|
| 179 | char *rval;
|
|---|
| 180 |
|
|---|
| 181 | /* is this a multi-element path ? */
|
|---|
| 182 | for (p = path, etok = strpbrk(p, ":;"), count = 0;
|
|---|
| 183 | etok;
|
|---|
| 184 | etok = strpbrk(p, ":;"))
|
|---|
| 185 | if ((etok - p) == 1) {
|
|---|
| 186 | if (*(etok - 1) == ';' ||
|
|---|
| 187 | *(etok - 1) == ':') {
|
|---|
| 188 | p = ++etok;
|
|---|
| 189 | continue; /* ignore empty bucket */
|
|---|
| 190 | } else if (etok = strpbrk(etok+1, ":;"))
|
|---|
| 191 | /* found one to count, handle drive letter */
|
|---|
| 192 | p = ++etok, count++;
|
|---|
| 193 | else
|
|---|
| 194 | /* all finished, force abort */
|
|---|
| 195 | p += strlen(p);
|
|---|
| 196 | } else
|
|---|
| 197 | /* found another one, no drive letter */
|
|---|
| 198 | p = ++etok, count++;
|
|---|
| 199 |
|
|---|
| 200 | if (count) {
|
|---|
| 201 | count++; /* x1;x2;x3 <- need to count x3 */
|
|---|
| 202 |
|
|---|
| 203 | /*
|
|---|
| 204 | * Hazard a guess on how big the buffer needs to be.
|
|---|
| 205 | * We have to convert things like c:/foo to /c=/foo.
|
|---|
| 206 | */
|
|---|
| 207 | nutc_path_len = strlen(path) + (count*2) + 1;
|
|---|
| 208 | nutc_path = xmalloc(nutc_path_len);
|
|---|
| 209 | pathp = nutc_path;
|
|---|
| 210 | *pathp = '\0';
|
|---|
| 211 |
|
|---|
| 212 | /*
|
|---|
| 213 | * Loop through PATH and convert one elemnt of the path at at
|
|---|
| 214 | * a time. Single file pathnames will fail this and fall
|
|---|
| 215 | * to the logic below loop.
|
|---|
| 216 | */
|
|---|
| 217 | for (p = path, etok = strpbrk(p, ":;");
|
|---|
| 218 | etok;
|
|---|
| 219 | etok = strpbrk(p, ":;")) {
|
|---|
| 220 |
|
|---|
| 221 | /* don't trip up on device specifiers or empty path slots */
|
|---|
| 222 | if ((etok - p) == 1)
|
|---|
| 223 | if (*(etok - 1) == ';' ||
|
|---|
| 224 | *(etok - 1) == ':') {
|
|---|
| 225 | p = ++etok;
|
|---|
| 226 | continue;
|
|---|
| 227 | } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
|
|---|
| 228 | break; /* thing found was a WINDOWS32 pathname */
|
|---|
| 229 |
|
|---|
| 230 | /* save separator */
|
|---|
| 231 | sep = *etok;
|
|---|
| 232 |
|
|---|
| 233 | /* terminate the current path element -- temporarily */
|
|---|
| 234 | *etok = '\0';
|
|---|
| 235 |
|
|---|
| 236 | #ifdef __NUTC__
|
|---|
| 237 | /* convert to NutC format */
|
|---|
| 238 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
|---|
| 239 | free(nutc_path);
|
|---|
| 240 | rval = savestring(path, strlen(path));
|
|---|
| 241 | return rval;
|
|---|
| 242 | }
|
|---|
| 243 | #else
|
|---|
| 244 | *pathp++ = '/';
|
|---|
| 245 | *pathp++ = p[0];
|
|---|
| 246 | *pathp++ = '=';
|
|---|
| 247 | *pathp++ = '/';
|
|---|
| 248 | strcpy(pathp, &p[2]);
|
|---|
| 249 | #endif
|
|---|
| 250 |
|
|---|
| 251 | pathp += strlen(pathp);
|
|---|
| 252 | *pathp++ = ':'; /* use Unix style path separtor for new path */
|
|---|
| 253 | *pathp = '\0'; /* make sure we are null terminaed */
|
|---|
| 254 |
|
|---|
| 255 | /* restore path separator */
|
|---|
| 256 | *etok = sep;
|
|---|
| 257 |
|
|---|
| 258 | /* point p to first char of next path element */
|
|---|
| 259 | p = ++etok;
|
|---|
| 260 |
|
|---|
| 261 | }
|
|---|
| 262 | } else {
|
|---|
| 263 | nutc_path_len = strlen(path) + 3;
|
|---|
| 264 | nutc_path = xmalloc(nutc_path_len);
|
|---|
| 265 | pathp = nutc_path;
|
|---|
| 266 | *pathp = '\0';
|
|---|
| 267 | p = path;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /*
|
|---|
| 271 | * OK, here we handle the last element in PATH (e.g. c of a;b;c)
|
|---|
| 272 | * or the path was a single filename and will be converted
|
|---|
| 273 | * here. Note, testing p here assures that we don't trip up
|
|---|
| 274 | * on paths like a;b; which have trailing delimiter followed by
|
|---|
| 275 | * nothing.
|
|---|
| 276 | */
|
|---|
| 277 | if (*p != '\0') {
|
|---|
| 278 | #ifdef __NUTC__
|
|---|
| 279 | if (_NutPathToNutc(p, pathp, 0) == FALSE) {
|
|---|
| 280 | free(nutc_path);
|
|---|
| 281 | rval = savestring(path, strlen(path));
|
|---|
| 282 | return rval;
|
|---|
| 283 | }
|
|---|
| 284 | #else
|
|---|
| 285 | *pathp++ = '/';
|
|---|
| 286 | *pathp++ = p[0];
|
|---|
| 287 | *pathp++ = '=';
|
|---|
| 288 | *pathp++ = '/';
|
|---|
| 289 | strcpy(pathp, &p[2]);
|
|---|
| 290 | #endif
|
|---|
| 291 | } else
|
|---|
| 292 | *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
|
|---|
| 293 |
|
|---|
| 294 | rval = savestring(nutc_path, strlen(nutc_path));
|
|---|
| 295 | free(nutc_path);
|
|---|
| 296 | return rval;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | #endif
|
|---|