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  */
  25 
  26 #include <Windows.h>
  27 #include <io.h>
  28 #include <stdio.h>
  29 #include <string.h>
  30 #include <malloc.h>
  31 
  32 /* 
  33  * Test if pos points to /cygdrive/_/ where _ can 
  34  * be any character. 
  35  */
  36 int is_cygdrive_here(int pos, char *in, int len)
  37 {
  38   // Length of /cygdrive/c/ is 12
  39   if (pos+12 > len) return 0;
  40   if (in[pos+11]=='/' &&
  41       in[pos+9]=='/' &&
  42       in[pos+8]=='e' &&
  43       in[pos+7]=='v' &&
  44       in[pos+6]=='i' &&
  45       in[pos+5]=='r' &&
  46       in[pos+4]=='d' &&
  47       in[pos+3]=='g' &&
  48       in[pos+2]=='y' &&
  49       in[pos+1]=='c' &&
  50       in[pos+0]=='/') {
  51     return 1;
  52   }
  53   return 0;
  54 }
  55 
  56 /* 
  57  * Replace /cygdrive/_/ with _:/
  58  * Works in place since drive letter is always
  59  * shorter than /cygdrive/
  60  */
  61 char *replace_cygdrive(char *in)
  62 {
  63   int len = strlen(in);
  64   char *out = malloc(len+1);
  65   int i,j;
  66 
  67   if (len < 12) {
  68     strcpy(out, in);
  69     return out;
  70   }
  71   for (i = 0, j = 0; i<len;) {
  72     if (is_cygdrive_here(i, in, len)) {
  73       out[j++] = in[i+10];
  74       out[j++] = ':';
  75       i+=11;
  76     } else {
  77       out[j] = in[i];
  78       i++;
  79       j++;
  80     }
  81   }
  82   out[j] = 0;
  83   return out;
  84 }
  85 
  86 void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
  87 {
  88   while ( (addlen+*u+1) > *bl) {
  89     *bl *= 2;
  90     *b = realloc(*b, *bl);
  91   }
  92   memcpy(*b+*u, add, addlen);
  93   *u += addlen;   
  94 }
  95 
  96 /*
  97  * Creates a new string from in where the first occurance of sub is
  98  * replaced by rep.
  99  */
 100 char *replace_substring(char *in, char *sub, char *rep)
 101 {
 102   int in_len = strlen(in);
 103   int sub_len = strlen(sub);
 104   int rep_len = strlen(rep);
 105   char *out = malloc(in_len - sub_len + rep_len + 1);
 106   char *p;
 107 
 108   if (!(p = strstr(in, sub))) {
 109     // If sub isn't a substring of in, just return in.
 110     return in;
 111   }
 112 
 113   // Copy characters from beginning of in to start of sub.
 114   strncpy(out, in, p - in);
 115   out[p - in] = '\0';
 116 
 117   sprintf(out + (p - in), "%s%s", rep, p + sub_len);
 118 
 119   return out;
 120 }
 121 
 122 char *files_to_delete[1024];
 123 int num_files_to_delete = 0;
 124 
 125 char *fix_at_file(char *in)
 126 {
 127   char *tmpdir;
 128   char name[2048]; 
 129   char *atname;
 130   char *buffer;
 131   size_t buflen=65536;
 132   size_t used=0;
 133   size_t len;
 134   int rc;
 135   FILE *atout;
 136   FILE *atin;
 137   char block[2048];
 138   size_t blocklen;
 139   char *fixed;
 140 
 141   atin = fopen(in+1, "r");
 142   if (atin == NULL) {
 143     fprintf(stderr, "Could not read at file %s\n", in+1);
 144     exit(-1);
 145   }
 146 
 147   tmpdir = getenv("TMP");
 148   if (tmpdir == NULL) {
 149     tmpdir = "c:/cygwin/tmp";
 150   }
 151   _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir);
 152 
 153   rc = _mktemp_s(name, strlen(name)+1); 
 154   if (rc) {
 155     fprintf(stderr, "Could not create temporary file name for at file!\n");
 156     exit(-1);
 157   }
 158 
 159   atout = fopen(name, "w");
 160   if (atout == NULL) {
 161     fprintf(stderr, "Could open temporary file for writing! %s\n", name);
 162     exit(-1);
 163   }
 164 
 165   buffer = malloc(buflen);
 166   while((blocklen = fread(block,1,sizeof(block),atin)) > 0) {
 167     append(&buffer, &buflen, &used, block, blocklen);
 168   }
 169   buffer[used] = 0;
 170   fixed = replace_cygdrive(buffer);
 171   fwrite(fixed, strlen(fixed), 1, atout);
 172   fclose(atin);
 173   fclose(atout);
 174   free(fixed);
 175   free(buffer);
 176   files_to_delete[num_files_to_delete] = malloc(strlen(name)+1);
 177   strcpy(files_to_delete[num_files_to_delete], name);
 178   num_files_to_delete++;
 179   atname = malloc(strlen(name)+2);
 180   atname[0] = '@';
 181   strcpy(atname+1, name);
 182   return atname;
 183 }
 184 
 185 int main(int argc, char **argv)
 186 {
 187     STARTUPINFO si;
 188     PROCESS_INFORMATION pi;
 189     unsigned short rc;    
 190 
 191     char *new_at_file;
 192     char *old_at_file;
 193     char *line;
 194     int i;
 195     DWORD exitCode;
 196 
 197     if (argc<2) {
 198         fprintf(stderr, "Usage: uncygdrive.exe /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt");
 199         exit(0);
 200     }
 201 
 202     line = replace_cygdrive(strstr(GetCommandLine(), argv[1]));
 203 
 204     for (i=1; i<argc; ++i) {
 205        if (argv[i][0] == '@') {
 206           // Found at-file! Fix it!
 207           old_at_file = replace_cygdrive(argv[i]);
 208           new_at_file = fix_at_file(old_at_file);
 209           line = replace_substring(line, old_at_file, new_at_file);
 210        }
 211     }
 212 
 213     if (getenv("DEBUG_UNCYGDRIVE") != NULL) {
 214       fprintf(stderr, "uncygdrive >%s<\n", line);
 215     }
 216 
 217     ZeroMemory(&si,sizeof(si));
 218     si.cb=sizeof(si);
 219     ZeroMemory(&pi,sizeof(pi));
 220 
 221     rc = CreateProcess(NULL,
 222                        line,
 223                        0,
 224                        0,
 225                        TRUE,
 226                        0,
 227                        0,
 228                        0,
 229                        &si,
 230                        &pi);
 231     if(!rc)
 232     {
 233       //Could not start process;
 234       fprintf(stderr, "Could not start process!\n");
 235       exit(-1);
 236     }
 237 
 238     WaitForSingleObject(pi.hProcess,INFINITE);
 239     GetExitCodeProcess(pi.hProcess,&exitCode);
 240 
 241     if (getenv("DEBUG_UNCYGDRIVE") != NULL) {
 242       for (i=0; i<num_files_to_delete; ++i) {
 243         fprintf(stderr, "Not deleting temporary uncygdrive file %s\n",
 244                 files_to_delete[i]);     
 245       }
 246     }
 247     else {
 248       for (i=0; i<num_files_to_delete; ++i) {
 249         remove(files_to_delete[i]);
 250       }
 251     }
 252     
 253     exit(exitCode);
 254 }