Wed, 27 Jun 2018 05:10:21 -0700
Added tag jdk8u181-b12 for changeset 760af13b8923
ohair@494 | 1 | /* |
ohair@494 | 2 | * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
ohair@494 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@494 | 4 | * |
ohair@494 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@494 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@494 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@494 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@494 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@494 | 10 | * |
ohair@494 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@494 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@494 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@494 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@494 | 15 | * accompanied this code). |
ohair@494 | 16 | * |
ohair@494 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@494 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@494 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@494 | 20 | * |
ohair@494 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@494 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@494 | 23 | * questions. |
ohair@494 | 24 | */ |
ohair@494 | 25 | |
ohair@494 | 26 | #include <Windows.h> |
ohair@494 | 27 | #include <io.h> |
ohair@494 | 28 | #include <stdio.h> |
ohair@494 | 29 | #include <string.h> |
ohair@494 | 30 | #include <malloc.h> |
ohair@494 | 31 | |
erikj@564 | 32 | void report_error() |
erikj@564 | 33 | { |
erikj@564 | 34 | LPVOID lpMsgBuf; |
erikj@564 | 35 | DWORD dw = GetLastError(); |
erikj@564 | 36 | |
erikj@564 | 37 | FormatMessage( |
erikj@564 | 38 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
erikj@564 | 39 | FORMAT_MESSAGE_FROM_SYSTEM | |
erikj@564 | 40 | FORMAT_MESSAGE_IGNORE_INSERTS, |
erikj@564 | 41 | NULL, |
erikj@564 | 42 | dw, |
erikj@564 | 43 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
erikj@564 | 44 | (LPTSTR) &lpMsgBuf, |
erikj@564 | 45 | 0, |
erikj@564 | 46 | NULL); |
erikj@564 | 47 | |
erikj@564 | 48 | fprintf(stderr, |
erikj@564 | 49 | "Could not start process! Failed with error %d: %s\n", |
erikj@564 | 50 | dw, lpMsgBuf); |
erikj@564 | 51 | |
erikj@564 | 52 | LocalFree(lpMsgBuf); |
erikj@564 | 53 | } |
erikj@564 | 54 | |
ohair@494 | 55 | /* |
ohair@494 | 56 | * Test if pos points to /cygdrive/_/ where _ can |
ohair@494 | 57 | * be any character. |
ohair@494 | 58 | */ |
ohair@494 | 59 | int is_cygdrive_here(int pos, char *in, int len) |
ohair@494 | 60 | { |
ohair@494 | 61 | // Length of /cygdrive/c/ is 12 |
ohair@494 | 62 | if (pos+12 > len) return 0; |
ohair@494 | 63 | if (in[pos+11]=='/' && |
ohair@494 | 64 | in[pos+9]=='/' && |
ohair@494 | 65 | in[pos+8]=='e' && |
ohair@494 | 66 | in[pos+7]=='v' && |
ohair@494 | 67 | in[pos+6]=='i' && |
ohair@494 | 68 | in[pos+5]=='r' && |
ohair@494 | 69 | in[pos+4]=='d' && |
ohair@494 | 70 | in[pos+3]=='g' && |
ohair@494 | 71 | in[pos+2]=='y' && |
ohair@494 | 72 | in[pos+1]=='c' && |
ohair@494 | 73 | in[pos+0]=='/') { |
ohair@494 | 74 | return 1; |
ohair@494 | 75 | } |
ohair@494 | 76 | return 0; |
ohair@494 | 77 | } |
ohair@494 | 78 | |
ohair@494 | 79 | /* |
ohair@494 | 80 | * Replace /cygdrive/_/ with _:/ |
ohair@494 | 81 | * Works in place since drive letter is always |
ohair@494 | 82 | * shorter than /cygdrive/ |
ohair@494 | 83 | */ |
ohair@494 | 84 | char *replace_cygdrive_cygwin(char *in) |
ohair@494 | 85 | { |
ohair@494 | 86 | int len = strlen(in); |
ohair@494 | 87 | char *out = malloc(len+1); |
ohair@494 | 88 | int i,j; |
ohair@494 | 89 | |
ohair@494 | 90 | if (len < 12) { |
ohair@494 | 91 | strcpy(out, in); |
ohair@494 | 92 | return out; |
ohair@494 | 93 | } |
ohair@494 | 94 | for (i = 0, j = 0; i<len;) { |
ohair@494 | 95 | if (is_cygdrive_here(i, in, len)) { |
ohair@494 | 96 | out[j++] = in[i+10]; |
ohair@494 | 97 | out[j++] = ':'; |
ohair@494 | 98 | i+=11; |
ohair@494 | 99 | } else { |
ohair@494 | 100 | out[j] = in[i]; |
ohair@494 | 101 | i++; |
ohair@494 | 102 | j++; |
ohair@494 | 103 | } |
ohair@494 | 104 | } |
ohair@494 | 105 | out[j] = 0; |
ohair@494 | 106 | return out; |
ohair@494 | 107 | } |
ohair@494 | 108 | |
ohair@494 | 109 | void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen) |
ohair@494 | 110 | { |
ohair@494 | 111 | while ( (addlen+*u+1) > *bl) { |
ohair@494 | 112 | *bl *= 2; |
ohair@494 | 113 | *b = realloc(*b, *bl); |
ohair@494 | 114 | } |
ohair@494 | 115 | memcpy(*b+*u, add, addlen); |
ohair@494 | 116 | *u += addlen; |
ohair@494 | 117 | } |
ohair@494 | 118 | |
ohair@494 | 119 | /* |
ohair@494 | 120 | * Creates a new string from in where the first occurance of sub is |
ohair@494 | 121 | * replaced by rep. |
ohair@494 | 122 | */ |
ohair@494 | 123 | char *replace_substring(char *in, char *sub, char *rep) |
ohair@494 | 124 | { |
ohair@494 | 125 | int in_len = strlen(in); |
ohair@494 | 126 | int sub_len = strlen(sub); |
ohair@494 | 127 | int rep_len = strlen(rep); |
ohair@494 | 128 | char *out = malloc(in_len - sub_len + rep_len + 1); |
ohair@494 | 129 | char *p; |
ohair@494 | 130 | |
ohair@494 | 131 | if (!(p = strstr(in, sub))) { |
ohair@494 | 132 | // If sub isn't a substring of in, just return in. |
ohair@494 | 133 | return in; |
ohair@494 | 134 | } |
ohair@494 | 135 | |
ohair@494 | 136 | // Copy characters from beginning of in to start of sub. |
ohair@494 | 137 | strncpy(out, in, p - in); |
ohair@494 | 138 | out[p - in] = '\0'; |
ohair@494 | 139 | |
ohair@494 | 140 | sprintf(out + (p - in), "%s%s", rep, p + sub_len); |
ohair@494 | 141 | |
ohair@494 | 142 | return out; |
ohair@494 | 143 | } |
ohair@494 | 144 | |
ohair@494 | 145 | char* msys_path_list; // @-separated list of paths prefix to look for |
ohair@494 | 146 | char* msys_path_list_end; // Points to last \0 in msys_path_list. |
ohair@494 | 147 | |
ohair@494 | 148 | void setup_msys_path_list(char* argument) |
ohair@494 | 149 | { |
ohair@494 | 150 | char* p; |
ohair@494 | 151 | char* drive_letter_pos; |
ohair@494 | 152 | |
ohair@494 | 153 | msys_path_list = strdup(&argument[2]); |
ohair@494 | 154 | msys_path_list_end = &msys_path_list[strlen(msys_path_list)]; |
ohair@494 | 155 | |
ohair@494 | 156 | // Convert all at-sign (@) in path list to \0. |
ohair@494 | 157 | // @ was chosen as separator to minimize risk of other tools messing around with it |
ohair@494 | 158 | p = msys_path_list; |
ohair@494 | 159 | do { |
ohair@494 | 160 | if (p[1] == ':') { |
ohair@494 | 161 | // msys has mangled our path list, restore it from c:/... to /c/... |
ohair@494 | 162 | drive_letter_pos = p+1; |
ohair@494 | 163 | *drive_letter_pos = *p; |
ohair@494 | 164 | *p = '/'; |
ohair@494 | 165 | } |
ohair@494 | 166 | |
ohair@494 | 167 | // Look for an @ in the list |
ohair@494 | 168 | p = strchr(p, '@'); |
ohair@494 | 169 | if (p != NULL) { |
ohair@494 | 170 | *p = '\0'; |
ohair@494 | 171 | p++; |
ohair@494 | 172 | } |
ohair@494 | 173 | } while (p != NULL); |
ohair@494 | 174 | } |
ohair@494 | 175 | |
ohair@494 | 176 | char *replace_cygdrive_msys(char *in) |
ohair@494 | 177 | { |
ohair@494 | 178 | char* str; |
ohair@494 | 179 | char* prefix; |
ohair@494 | 180 | char* p; |
ohair@494 | 181 | |
ohair@494 | 182 | str = strdup(in); |
ohair@494 | 183 | |
ohair@494 | 184 | // For each prefix in the path list, search for it and replace /c/... with c:/... |
ohair@494 | 185 | for (prefix = msys_path_list; prefix < msys_path_list_end && prefix != NULL; prefix += strlen(prefix)+1) { |
ohair@494 | 186 | p=str; |
ohair@494 | 187 | while ((p = strstr(p, prefix))) { |
ohair@494 | 188 | char* drive_letter = p+1; |
ohair@494 | 189 | *p = *drive_letter; |
ohair@494 | 190 | *drive_letter = ':'; |
ohair@494 | 191 | p++; |
ohair@494 | 192 | } |
ohair@494 | 193 | } |
ohair@494 | 194 | |
ohair@494 | 195 | return str; |
ohair@494 | 196 | } |
ohair@494 | 197 | |
ohair@494 | 198 | char*(*replace_cygdrive)(char *in) = NULL; |
ohair@494 | 199 | |
ohair@494 | 200 | char *files_to_delete[1024]; |
ohair@494 | 201 | int num_files_to_delete = 0; |
ohair@494 | 202 | |
ohair@494 | 203 | char *fix_at_file(char *in) |
ohair@494 | 204 | { |
ohair@494 | 205 | char *tmpdir; |
ohair@494 | 206 | char name[2048]; |
ohair@494 | 207 | char *atname; |
ohair@494 | 208 | char *buffer; |
ohair@494 | 209 | size_t buflen=65536; |
ohair@494 | 210 | size_t used=0; |
ohair@494 | 211 | size_t len; |
ohair@494 | 212 | int rc; |
ohair@494 | 213 | FILE *atout; |
ohair@494 | 214 | FILE *atin; |
ohair@494 | 215 | char block[2048]; |
ohair@494 | 216 | size_t blocklen; |
ohair@494 | 217 | char *fixed; |
ohair@494 | 218 | |
ohair@494 | 219 | atin = fopen(in+1, "r"); |
ohair@494 | 220 | if (atin == NULL) { |
ohair@494 | 221 | fprintf(stderr, "Could not read at file %s\n", in+1); |
ohair@494 | 222 | exit(-1); |
ohair@494 | 223 | } |
ohair@494 | 224 | |
ohair@494 | 225 | tmpdir = getenv("TMP"); |
ohair@494 | 226 | if (tmpdir == NULL) { |
ohair@494 | 227 | tmpdir = "c:/cygwin/tmp"; |
ohair@494 | 228 | } |
ohair@494 | 229 | _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir); |
ohair@494 | 230 | |
ohair@494 | 231 | rc = _mktemp_s(name, strlen(name)+1); |
ohair@494 | 232 | if (rc) { |
ohair@494 | 233 | fprintf(stderr, "Could not create temporary file name for at file!\n"); |
ohair@494 | 234 | exit(-1); |
ohair@494 | 235 | } |
ohair@494 | 236 | |
ohair@494 | 237 | atout = fopen(name, "w"); |
ohair@494 | 238 | if (atout == NULL) { |
ohair@494 | 239 | fprintf(stderr, "Could not open temporary file for writing! %s\n", name); |
ohair@494 | 240 | exit(-1); |
ohair@494 | 241 | } |
ohair@494 | 242 | |
ohair@494 | 243 | buffer = malloc(buflen); |
ohair@494 | 244 | while((blocklen = fread(block,1,sizeof(block),atin)) > 0) { |
ohair@494 | 245 | append(&buffer, &buflen, &used, block, blocklen); |
ohair@494 | 246 | } |
ohair@494 | 247 | buffer[used] = 0; |
ohair@494 | 248 | if (getenv("DEBUG_FIXPATH") != NULL) { |
ohair@494 | 249 | fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer); |
ohair@494 | 250 | } |
ohair@494 | 251 | fixed = replace_cygdrive(buffer); |
ohair@494 | 252 | if (getenv("DEBUG_FIXPATH") != NULL) { |
ohair@494 | 253 | fprintf(stderr, "fixpath converted to @-file %s is: %s\n", name, fixed); |
ohair@494 | 254 | } |
ohair@494 | 255 | fwrite(fixed, strlen(fixed), 1, atout); |
ohair@494 | 256 | fclose(atin); |
ohair@494 | 257 | fclose(atout); |
ohair@494 | 258 | free(fixed); |
ohair@494 | 259 | free(buffer); |
ohair@494 | 260 | files_to_delete[num_files_to_delete] = malloc(strlen(name)+1); |
ohair@494 | 261 | strcpy(files_to_delete[num_files_to_delete], name); |
ohair@494 | 262 | num_files_to_delete++; |
ohair@494 | 263 | atname = malloc(strlen(name)+2); |
ohair@494 | 264 | atname[0] = '@'; |
ohair@494 | 265 | strcpy(atname+1, name); |
ohair@494 | 266 | return atname; |
ohair@494 | 267 | } |
ohair@494 | 268 | |
ohair@494 | 269 | int main(int argc, char **argv) |
ohair@494 | 270 | { |
ohair@494 | 271 | STARTUPINFO si; |
ohair@494 | 272 | PROCESS_INFORMATION pi; |
ohair@494 | 273 | unsigned short rc; |
ohair@494 | 274 | |
ohair@494 | 275 | char *new_at_file; |
ohair@494 | 276 | char *old_at_file; |
ohair@494 | 277 | char *line; |
ohair@494 | 278 | int i; |
ohair@494 | 279 | DWORD exitCode; |
ohair@494 | 280 | |
ohair@494 | 281 | if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) { |
erikj@564 | 282 | fprintf(stderr, "Usage: fixpath -c|m<path@path@...> /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt\n"); |
ohair@494 | 283 | exit(0); |
ohair@494 | 284 | } |
ohair@494 | 285 | |
ohair@494 | 286 | if (getenv("DEBUG_FIXPATH") != NULL) { |
ohair@494 | 287 | fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1])); |
ohair@494 | 288 | } |
ohair@494 | 289 | |
ohair@494 | 290 | if (argv[1][1] == 'c' && argv[1][2] == '\0') { |
ohair@494 | 291 | if (getenv("DEBUG_FIXPATH") != NULL) { |
ohair@494 | 292 | fprintf(stderr, "using cygwin mode\n"); |
ohair@494 | 293 | } |
ohair@494 | 294 | replace_cygdrive = replace_cygdrive_cygwin; |
ohair@494 | 295 | } else if (argv[1][1] == 'm') { |
ohair@494 | 296 | if (getenv("DEBUG_FIXPATH") != NULL) { |
ohair@494 | 297 | fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]); |
ohair@494 | 298 | } |
ohair@494 | 299 | setup_msys_path_list(argv[1]); |
ohair@494 | 300 | replace_cygdrive = replace_cygdrive_msys; |
ohair@494 | 301 | } else { |
ohair@494 | 302 | fprintf(stderr, "Unknown mode: %s\n", argv[1]); |
ohair@494 | 303 | exit(-1); |
ohair@494 | 304 | } |
ohair@494 | 305 | line = replace_cygdrive(strstr(GetCommandLine(), argv[2])); |
ohair@494 | 306 | |
ohair@494 | 307 | for (i=1; i<argc; ++i) { |
ohair@494 | 308 | if (argv[i][0] == '@') { |
ohair@494 | 309 | // Found at-file! Fix it! |
ohair@494 | 310 | old_at_file = replace_cygdrive(argv[i]); |
ohair@494 | 311 | new_at_file = fix_at_file(old_at_file); |
ohair@494 | 312 | line = replace_substring(line, old_at_file, new_at_file); |
ohair@494 | 313 | } |
ohair@494 | 314 | } |
ohair@494 | 315 | |
ohair@494 | 316 | if (getenv("DEBUG_FIXPATH") != NULL) { |
ohair@494 | 317 | fprintf(stderr, "fixpath converted line >%s<\n", line); |
ohair@494 | 318 | } |
ohair@494 | 319 | |
ohair@494 | 320 | ZeroMemory(&si,sizeof(si)); |
ohair@494 | 321 | si.cb=sizeof(si); |
ohair@494 | 322 | ZeroMemory(&pi,sizeof(pi)); |
ohair@494 | 323 | |
ohair@494 | 324 | rc = CreateProcess(NULL, |
ohair@494 | 325 | line, |
ohair@494 | 326 | 0, |
ohair@494 | 327 | 0, |
ohair@494 | 328 | TRUE, |
ohair@494 | 329 | 0, |
ohair@494 | 330 | 0, |
ohair@494 | 331 | 0, |
ohair@494 | 332 | &si, |
ohair@494 | 333 | &pi); |
erikj@564 | 334 | if(!rc) { |
erikj@564 | 335 | // Could not start process for some reason. Try to report why: |
erikj@564 | 336 | report_error(); |
erikj@564 | 337 | exit(rc); |
ohair@494 | 338 | } |
ohair@494 | 339 | |
ohair@494 | 340 | WaitForSingleObject(pi.hProcess,INFINITE); |
ohair@494 | 341 | GetExitCodeProcess(pi.hProcess,&exitCode); |
ohair@494 | 342 | |
ohair@494 | 343 | if (getenv("DEBUG_FIXPATH") != NULL) { |
ohair@494 | 344 | for (i=0; i<num_files_to_delete; ++i) { |
ohair@494 | 345 | fprintf(stderr, "Not deleting temporary fixpath file %s\n", |
ohair@494 | 346 | files_to_delete[i]); |
ohair@494 | 347 | } |
ohair@494 | 348 | } |
ohair@494 | 349 | else { |
ohair@494 | 350 | for (i=0; i<num_files_to_delete; ++i) { |
ohair@494 | 351 | remove(files_to_delete[i]); |
ohair@494 | 352 | } |
ohair@494 | 353 | } |
ohair@494 | 354 | |
ohair@494 | 355 | exit(exitCode); |
ohair@494 | 356 | } |