common/src/fixpath.c

Mon, 14 Sep 2020 16:42:03 +0100

author
andrew
date
Mon, 14 Sep 2020 16:42:03 +0100
changeset 2554
7f60c2d9823e
parent 564
befbad2e4d87
child 1133
50aaf272884f
permissions
-rw-r--r--

Added tag jdk8u272-b08 for changeset 34c6baf21464

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

mercurial