| 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 | #ifdef KMK_PRF | 
|---|
| 158 | { | 
|---|
| 159 | int err = errno; | 
|---|
| 160 | fprintf(stderr, "stat(%s,) -> %d/%d\n", path, rc, errno); | 
|---|
| 161 | errno = err; | 
|---|
| 162 | } | 
|---|
| 163 | #endif | 
|---|
| 164 | return rc; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | #ifdef unused | 
|---|
| 168 | /* | 
|---|
| 169 | * Convert delimiter separated pathnames (e.g. PATH) or single file pathname | 
|---|
| 170 | * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that | 
|---|
| 171 | * _NutPathToNutc() fails to convert, just return the path we were handed | 
|---|
| 172 | * and assume the caller will know what to do with it (It was probably | 
|---|
| 173 | * a mistake to try and convert it anyway due to some of the bizarre things | 
|---|
| 174 | * that might look like pathnames in makefiles). | 
|---|
| 175 | */ | 
|---|
| 176 | char * | 
|---|
| 177 | convert_path_to_nutc(char *path) | 
|---|
| 178 | { | 
|---|
| 179 | int  count;            /* count of path elements */ | 
|---|
| 180 | char *nutc_path;     /* new NutC path */ | 
|---|
| 181 | int  nutc_path_len;    /* length of buffer to allocate for new path */ | 
|---|
| 182 | char *pathp;        /* pointer to nutc_path used to build it */ | 
|---|
| 183 | char *etok;            /* token separator for old path */ | 
|---|
| 184 | char *p;            /* points to element of old path */ | 
|---|
| 185 | char sep;            /* what flavor of separator used in old path */ | 
|---|
| 186 | char *rval; | 
|---|
| 187 |  | 
|---|
| 188 | /* is this a multi-element path ? */ | 
|---|
| 189 | for (p = path, etok = strpbrk(p, ":;"), count = 0; | 
|---|
| 190 | etok; | 
|---|
| 191 | etok = strpbrk(p, ":;")) | 
|---|
| 192 | if ((etok - p) == 1) { | 
|---|
| 193 | if (*(etok - 1) == ';' || | 
|---|
| 194 | *(etok - 1) == ':') { | 
|---|
| 195 | p = ++etok; | 
|---|
| 196 | continue;    /* ignore empty bucket */ | 
|---|
| 197 | } else if (etok = strpbrk(etok+1, ":;")) | 
|---|
| 198 | /* found one to count, handle drive letter */ | 
|---|
| 199 | p = ++etok, count++; | 
|---|
| 200 | else | 
|---|
| 201 | /* all finished, force abort */ | 
|---|
| 202 | p += strlen(p); | 
|---|
| 203 | } else | 
|---|
| 204 | /* found another one, no drive letter */ | 
|---|
| 205 | p = ++etok, count++; | 
|---|
| 206 |  | 
|---|
| 207 | if (count) { | 
|---|
| 208 | count++;    /* x1;x2;x3 <- need to count x3 */ | 
|---|
| 209 |  | 
|---|
| 210 | /* | 
|---|
| 211 | * Hazard a guess on how big the buffer needs to be. | 
|---|
| 212 | * We have to convert things like c:/foo to /c=/foo. | 
|---|
| 213 | */ | 
|---|
| 214 | nutc_path_len = strlen(path) + (count*2) + 1; | 
|---|
| 215 | nutc_path = xmalloc(nutc_path_len); | 
|---|
| 216 | pathp = nutc_path; | 
|---|
| 217 | *pathp = '\0'; | 
|---|
| 218 |  | 
|---|
| 219 | /* | 
|---|
| 220 | * Loop through PATH and convert one elemnt of the path at at | 
|---|
| 221 | * a time. Single file pathnames will fail this and fall | 
|---|
| 222 | * to the logic below loop. | 
|---|
| 223 | */ | 
|---|
| 224 | for (p = path, etok = strpbrk(p, ":;"); | 
|---|
| 225 | etok; | 
|---|
| 226 | etok = strpbrk(p, ":;")) { | 
|---|
| 227 |  | 
|---|
| 228 | /* don't trip up on device specifiers or empty path slots */ | 
|---|
| 229 | if ((etok - p) == 1) | 
|---|
| 230 | if (*(etok - 1) == ';' || | 
|---|
| 231 | *(etok - 1) == ':') { | 
|---|
| 232 | p = ++etok; | 
|---|
| 233 | continue; | 
|---|
| 234 | } else if ((etok = strpbrk(etok+1, ":;")) == NULL) | 
|---|
| 235 | break;    /* thing found was a WINDOWS32 pathname */ | 
|---|
| 236 |  | 
|---|
| 237 | /* save separator */ | 
|---|
| 238 | sep = *etok; | 
|---|
| 239 |  | 
|---|
| 240 | /* terminate the current path element -- temporarily */ | 
|---|
| 241 | *etok = '\0'; | 
|---|
| 242 |  | 
|---|
| 243 | #ifdef __NUTC__ | 
|---|
| 244 | /* convert to NutC format */ | 
|---|
| 245 | if (_NutPathToNutc(p, pathp, 0) == FALSE) { | 
|---|
| 246 | free(nutc_path); | 
|---|
| 247 | rval = savestring(path, strlen(path)); | 
|---|
| 248 | return rval; | 
|---|
| 249 | } | 
|---|
| 250 | #else | 
|---|
| 251 | *pathp++ = '/'; | 
|---|
| 252 | *pathp++ = p[0]; | 
|---|
| 253 | *pathp++ = '='; | 
|---|
| 254 | *pathp++ = '/'; | 
|---|
| 255 | strcpy(pathp, &p[2]); | 
|---|
| 256 | #endif | 
|---|
| 257 |  | 
|---|
| 258 | pathp += strlen(pathp); | 
|---|
| 259 | *pathp++ = ':';     /* use Unix style path separtor for new path */ | 
|---|
| 260 | *pathp   = '\0'; /* make sure we are null terminaed */ | 
|---|
| 261 |  | 
|---|
| 262 | /* restore path separator */ | 
|---|
| 263 | *etok = sep; | 
|---|
| 264 |  | 
|---|
| 265 | /* point p to first char of next path element */ | 
|---|
| 266 | p = ++etok; | 
|---|
| 267 |  | 
|---|
| 268 | } | 
|---|
| 269 | } else { | 
|---|
| 270 | nutc_path_len = strlen(path) + 3; | 
|---|
| 271 | nutc_path = xmalloc(nutc_path_len); | 
|---|
| 272 | pathp = nutc_path; | 
|---|
| 273 | *pathp = '\0'; | 
|---|
| 274 | p = path; | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | /* | 
|---|
| 278 | * OK, here we handle the last element in PATH (e.g. c of a;b;c) | 
|---|
| 279 | * or the path was a single filename and will be converted | 
|---|
| 280 | * here. Note, testing p here assures that we don't trip up | 
|---|
| 281 | * on paths like a;b; which have trailing delimiter followed by | 
|---|
| 282 | * nothing. | 
|---|
| 283 | */ | 
|---|
| 284 | if (*p != '\0') { | 
|---|
| 285 | #ifdef __NUTC__ | 
|---|
| 286 | if (_NutPathToNutc(p, pathp, 0) == FALSE) { | 
|---|
| 287 | free(nutc_path); | 
|---|
| 288 | rval = savestring(path, strlen(path)); | 
|---|
| 289 | return rval; | 
|---|
| 290 | } | 
|---|
| 291 | #else | 
|---|
| 292 | *pathp++ = '/'; | 
|---|
| 293 | *pathp++ = p[0]; | 
|---|
| 294 | *pathp++ = '='; | 
|---|
| 295 | *pathp++ = '/'; | 
|---|
| 296 | strcpy(pathp, &p[2]); | 
|---|
| 297 | #endif | 
|---|
| 298 | } else | 
|---|
| 299 | *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */ | 
|---|
| 300 |  | 
|---|
| 301 | rval = savestring(nutc_path, strlen(nutc_path)); | 
|---|
| 302 | free(nutc_path); | 
|---|
| 303 | return rval; | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | #endif | 
|---|