common/src/fixpath.c

changeset 0
75a576e87639
child 1133
50aaf272884f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/common/src/fixpath.c	Wed Apr 27 01:39:08 2016 +0800
     1.3 @@ -0,0 +1,356 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +#include <Windows.h>
    1.30 +#include <io.h>
    1.31 +#include <stdio.h>
    1.32 +#include <string.h>
    1.33 +#include <malloc.h>
    1.34 +
    1.35 +void report_error()
    1.36 +{
    1.37 +  LPVOID lpMsgBuf;
    1.38 +  DWORD dw = GetLastError();
    1.39 +
    1.40 +  FormatMessage(
    1.41 +      FORMAT_MESSAGE_ALLOCATE_BUFFER |
    1.42 +      FORMAT_MESSAGE_FROM_SYSTEM |
    1.43 +      FORMAT_MESSAGE_IGNORE_INSERTS,
    1.44 +      NULL,
    1.45 +      dw,
    1.46 +      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    1.47 +      (LPTSTR) &lpMsgBuf,
    1.48 +      0,
    1.49 +      NULL);
    1.50 +
    1.51 +  fprintf(stderr,
    1.52 +          "Could not start process!  Failed with error %d: %s\n",
    1.53 +          dw, lpMsgBuf);
    1.54 +
    1.55 +  LocalFree(lpMsgBuf);
    1.56 +}
    1.57 +
    1.58 +/*
    1.59 + * Test if pos points to /cygdrive/_/ where _ can
    1.60 + * be any character.
    1.61 + */
    1.62 +int is_cygdrive_here(int pos, char *in, int len)
    1.63 +{
    1.64 +  // Length of /cygdrive/c/ is 12
    1.65 +  if (pos+12 > len) return 0;
    1.66 +  if (in[pos+11]=='/' &&
    1.67 +      in[pos+9]=='/' &&
    1.68 +      in[pos+8]=='e' &&
    1.69 +      in[pos+7]=='v' &&
    1.70 +      in[pos+6]=='i' &&
    1.71 +      in[pos+5]=='r' &&
    1.72 +      in[pos+4]=='d' &&
    1.73 +      in[pos+3]=='g' &&
    1.74 +      in[pos+2]=='y' &&
    1.75 +      in[pos+1]=='c' &&
    1.76 +      in[pos+0]=='/') {
    1.77 +    return 1;
    1.78 +  }
    1.79 +  return 0;
    1.80 +}
    1.81 +
    1.82 +/*
    1.83 + * Replace /cygdrive/_/ with _:/
    1.84 + * Works in place since drive letter is always
    1.85 + * shorter than /cygdrive/
    1.86 + */
    1.87 +char *replace_cygdrive_cygwin(char *in)
    1.88 +{
    1.89 +  int len = strlen(in);
    1.90 +  char *out = malloc(len+1);
    1.91 +  int i,j;
    1.92 +
    1.93 +  if (len < 12) {
    1.94 +    strcpy(out, in);
    1.95 +    return out;
    1.96 +  }
    1.97 +  for (i = 0, j = 0; i<len;) {
    1.98 +    if (is_cygdrive_here(i, in, len)) {
    1.99 +      out[j++] = in[i+10];
   1.100 +      out[j++] = ':';
   1.101 +      i+=11;
   1.102 +    } else {
   1.103 +      out[j] = in[i];
   1.104 +      i++;
   1.105 +      j++;
   1.106 +    }
   1.107 +  }
   1.108 +  out[j] = 0;
   1.109 +  return out;
   1.110 +}
   1.111 +
   1.112 +void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
   1.113 +{
   1.114 +  while ( (addlen+*u+1) > *bl) {
   1.115 +    *bl *= 2;
   1.116 +    *b = realloc(*b, *bl);
   1.117 +  }
   1.118 +  memcpy(*b+*u, add, addlen);
   1.119 +  *u += addlen;
   1.120 +}
   1.121 +
   1.122 +/*
   1.123 + * Creates a new string from in where the first occurance of sub is
   1.124 + * replaced by rep.
   1.125 + */
   1.126 +char *replace_substring(char *in, char *sub, char *rep)
   1.127 +{
   1.128 +  int in_len = strlen(in);
   1.129 +  int sub_len = strlen(sub);
   1.130 +  int rep_len = strlen(rep);
   1.131 +  char *out = malloc(in_len - sub_len + rep_len + 1);
   1.132 +  char *p;
   1.133 +
   1.134 +  if (!(p = strstr(in, sub))) {
   1.135 +    // If sub isn't a substring of in, just return in.
   1.136 +    return in;
   1.137 +  }
   1.138 +
   1.139 +  // Copy characters from beginning of in to start of sub.
   1.140 +  strncpy(out, in, p - in);
   1.141 +  out[p - in] = '\0';
   1.142 +
   1.143 +  sprintf(out + (p - in), "%s%s", rep, p + sub_len);
   1.144 +
   1.145 +  return out;
   1.146 +}
   1.147 +
   1.148 +char* msys_path_list; // @-separated list of paths prefix to look for
   1.149 +char* msys_path_list_end; // Points to last \0 in msys_path_list.
   1.150 +
   1.151 +void setup_msys_path_list(char* argument)
   1.152 +{
   1.153 +  char* p;
   1.154 +  char* drive_letter_pos;
   1.155 +
   1.156 +  msys_path_list = strdup(&argument[2]);
   1.157 +  msys_path_list_end = &msys_path_list[strlen(msys_path_list)];
   1.158 +
   1.159 +  // Convert all at-sign (@) in path list to \0.
   1.160 +  // @ was chosen as separator to minimize risk of other tools messing around with it
   1.161 +  p = msys_path_list;
   1.162 +  do {
   1.163 +    if (p[1] == ':') {
   1.164 +      // msys has mangled our path list, restore it from c:/... to /c/...
   1.165 +      drive_letter_pos = p+1;
   1.166 +      *drive_letter_pos = *p;
   1.167 +      *p = '/';
   1.168 +    }
   1.169 +
   1.170 +    // Look for an @ in the list
   1.171 +    p = strchr(p, '@');
   1.172 +    if (p != NULL) {
   1.173 +      *p = '\0';
   1.174 +      p++;
   1.175 +    }
   1.176 +  } while (p != NULL);
   1.177 +}
   1.178 +
   1.179 +char *replace_cygdrive_msys(char *in)
   1.180 +{
   1.181 +  char* str;
   1.182 +  char* prefix;
   1.183 +  char* p;
   1.184 +
   1.185 +  str = strdup(in);
   1.186 +
   1.187 +  // For each prefix in the path list, search for it and replace /c/... with c:/...
   1.188 +  for (prefix = msys_path_list; prefix < msys_path_list_end && prefix != NULL; prefix += strlen(prefix)+1) {
   1.189 +    p=str;
   1.190 +    while ((p = strstr(p, prefix))) {
   1.191 +      char* drive_letter = p+1;
   1.192 +      *p = *drive_letter;
   1.193 +      *drive_letter = ':';
   1.194 +      p++;
   1.195 +    }
   1.196 +  }
   1.197 +
   1.198 +  return str;
   1.199 +}
   1.200 +
   1.201 +char*(*replace_cygdrive)(char *in) = NULL;
   1.202 +
   1.203 +char *files_to_delete[1024];
   1.204 +int num_files_to_delete = 0;
   1.205 +
   1.206 +char *fix_at_file(char *in)
   1.207 +{
   1.208 +  char *tmpdir;
   1.209 +  char name[2048];
   1.210 +  char *atname;
   1.211 +  char *buffer;
   1.212 +  size_t buflen=65536;
   1.213 +  size_t used=0;
   1.214 +  size_t len;
   1.215 +  int rc;
   1.216 +  FILE *atout;
   1.217 +  FILE *atin;
   1.218 +  char block[2048];
   1.219 +  size_t blocklen;
   1.220 +  char *fixed;
   1.221 +
   1.222 +  atin = fopen(in+1, "r");
   1.223 +  if (atin == NULL) {
   1.224 +    fprintf(stderr, "Could not read at file %s\n", in+1);
   1.225 +    exit(-1);
   1.226 +  }
   1.227 +
   1.228 +  tmpdir = getenv("TMP");
   1.229 +  if (tmpdir == NULL) {
   1.230 +    tmpdir = "c:/cygwin/tmp";
   1.231 +  }
   1.232 +  _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir);
   1.233 +
   1.234 +  rc = _mktemp_s(name, strlen(name)+1);
   1.235 +  if (rc) {
   1.236 +    fprintf(stderr, "Could not create temporary file name for at file!\n");
   1.237 +    exit(-1);
   1.238 +  }
   1.239 +
   1.240 +  atout = fopen(name, "w");
   1.241 +  if (atout == NULL) {
   1.242 +    fprintf(stderr, "Could not open temporary file for writing! %s\n", name);
   1.243 +    exit(-1);
   1.244 +  }
   1.245 +
   1.246 +  buffer = malloc(buflen);
   1.247 +  while((blocklen = fread(block,1,sizeof(block),atin)) > 0) {
   1.248 +    append(&buffer, &buflen, &used, block, blocklen);
   1.249 +  }
   1.250 +  buffer[used] = 0;
   1.251 +  if (getenv("DEBUG_FIXPATH") != NULL) {
   1.252 +    fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer);
   1.253 +  }
   1.254 +  fixed = replace_cygdrive(buffer);
   1.255 +  if (getenv("DEBUG_FIXPATH") != NULL) {
   1.256 +    fprintf(stderr, "fixpath converted to @-file %s is: %s\n", name, fixed);
   1.257 +  }
   1.258 +  fwrite(fixed, strlen(fixed), 1, atout);
   1.259 +  fclose(atin);
   1.260 +  fclose(atout);
   1.261 +  free(fixed);
   1.262 +  free(buffer);
   1.263 +  files_to_delete[num_files_to_delete] = malloc(strlen(name)+1);
   1.264 +  strcpy(files_to_delete[num_files_to_delete], name);
   1.265 +  num_files_to_delete++;
   1.266 +  atname = malloc(strlen(name)+2);
   1.267 +  atname[0] = '@';
   1.268 +  strcpy(atname+1, name);
   1.269 +  return atname;
   1.270 +}
   1.271 +
   1.272 +int main(int argc, char **argv)
   1.273 +{
   1.274 +    STARTUPINFO si;
   1.275 +    PROCESS_INFORMATION pi;
   1.276 +    unsigned short rc;
   1.277 +
   1.278 +    char *new_at_file;
   1.279 +    char *old_at_file;
   1.280 +    char *line;
   1.281 +    int i;
   1.282 +    DWORD exitCode;
   1.283 +
   1.284 +    if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) {
   1.285 +        fprintf(stderr, "Usage: fixpath -c|m<path@path@...> /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt\n");
   1.286 +        exit(0);
   1.287 +    }
   1.288 +
   1.289 +    if (getenv("DEBUG_FIXPATH") != NULL) {
   1.290 +      fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1]));
   1.291 +    }
   1.292 +
   1.293 +    if (argv[1][1] == 'c' && argv[1][2] == '\0') {
   1.294 +      if (getenv("DEBUG_FIXPATH") != NULL) {
   1.295 +        fprintf(stderr, "using cygwin mode\n");
   1.296 +      }
   1.297 +      replace_cygdrive = replace_cygdrive_cygwin;
   1.298 +    } else if (argv[1][1] == 'm') {
   1.299 +      if (getenv("DEBUG_FIXPATH") != NULL) {
   1.300 +        fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]);
   1.301 +      }
   1.302 +      setup_msys_path_list(argv[1]);
   1.303 +      replace_cygdrive = replace_cygdrive_msys;
   1.304 +    } else {
   1.305 +      fprintf(stderr, "Unknown mode: %s\n", argv[1]);
   1.306 +      exit(-1);
   1.307 +    }
   1.308 +    line = replace_cygdrive(strstr(GetCommandLine(), argv[2]));
   1.309 +
   1.310 +    for (i=1; i<argc; ++i) {
   1.311 +       if (argv[i][0] == '@') {
   1.312 +          // Found at-file! Fix it!
   1.313 +          old_at_file = replace_cygdrive(argv[i]);
   1.314 +          new_at_file = fix_at_file(old_at_file);
   1.315 +          line = replace_substring(line, old_at_file, new_at_file);
   1.316 +       }
   1.317 +    }
   1.318 +
   1.319 +    if (getenv("DEBUG_FIXPATH") != NULL) {
   1.320 +      fprintf(stderr, "fixpath converted line >%s<\n", line);
   1.321 +    }
   1.322 +
   1.323 +    ZeroMemory(&si,sizeof(si));
   1.324 +    si.cb=sizeof(si);
   1.325 +    ZeroMemory(&pi,sizeof(pi));
   1.326 +
   1.327 +    rc = CreateProcess(NULL,
   1.328 +                       line,
   1.329 +                       0,
   1.330 +                       0,
   1.331 +                       TRUE,
   1.332 +                       0,
   1.333 +                       0,
   1.334 +                       0,
   1.335 +                       &si,
   1.336 +                       &pi);
   1.337 +    if(!rc) {
   1.338 +      // Could not start process for some reason.  Try to report why:
   1.339 +      report_error();
   1.340 +      exit(rc);
   1.341 +    }
   1.342 +
   1.343 +    WaitForSingleObject(pi.hProcess,INFINITE);
   1.344 +    GetExitCodeProcess(pi.hProcess,&exitCode);
   1.345 +
   1.346 +    if (getenv("DEBUG_FIXPATH") != NULL) {
   1.347 +      for (i=0; i<num_files_to_delete; ++i) {
   1.348 +        fprintf(stderr, "Not deleting temporary fixpath file %s\n",
   1.349 +                files_to_delete[i]);
   1.350 +      }
   1.351 +    }
   1.352 +    else {
   1.353 +      for (i=0; i<num_files_to_delete; ++i) {
   1.354 +        remove(files_to_delete[i]);
   1.355 +      }
   1.356 +    }
   1.357 +
   1.358 +    exit(exitCode);
   1.359 +}

mercurial