common/src/fixpath.c

Wed, 27 Apr 2016 01:39:08 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:39:08 +0800
changeset 0
75a576e87639
child 1133
50aaf272884f
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/
changeset: 1170:d117f01bfb4f
tag: jdk8u25-b17

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

mercurial