aoqi@0: /* aoqi@0: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: #include aoqi@0: #include aoqi@0: #include aoqi@0: #include aoqi@0: #include aoqi@0: aoqi@0: void report_error() aoqi@0: { aoqi@0: LPVOID lpMsgBuf; aoqi@0: DWORD dw = GetLastError(); aoqi@0: aoqi@0: FormatMessage( aoqi@0: FORMAT_MESSAGE_ALLOCATE_BUFFER | aoqi@0: FORMAT_MESSAGE_FROM_SYSTEM | aoqi@0: FORMAT_MESSAGE_IGNORE_INSERTS, aoqi@0: NULL, aoqi@0: dw, aoqi@0: MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), aoqi@0: (LPTSTR) &lpMsgBuf, aoqi@0: 0, aoqi@0: NULL); aoqi@0: aoqi@0: fprintf(stderr, aoqi@0: "Could not start process! Failed with error %d: %s\n", aoqi@0: dw, lpMsgBuf); aoqi@0: aoqi@0: LocalFree(lpMsgBuf); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Test if pos points to /cygdrive/_/ where _ can aoqi@0: * be any character. aoqi@0: */ aoqi@0: int is_cygdrive_here(int pos, char *in, int len) aoqi@0: { aoqi@0: // Length of /cygdrive/c/ is 12 aoqi@0: if (pos+12 > len) return 0; aoqi@0: if (in[pos+11]=='/' && aoqi@0: in[pos+9]=='/' && aoqi@0: in[pos+8]=='e' && aoqi@0: in[pos+7]=='v' && aoqi@0: in[pos+6]=='i' && aoqi@0: in[pos+5]=='r' && aoqi@0: in[pos+4]=='d' && aoqi@0: in[pos+3]=='g' && aoqi@0: in[pos+2]=='y' && aoqi@0: in[pos+1]=='c' && aoqi@0: in[pos+0]=='/') { aoqi@0: return 1; aoqi@0: } aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Replace /cygdrive/_/ with _:/ aoqi@0: * Works in place since drive letter is always aoqi@0: * shorter than /cygdrive/ aoqi@0: */ aoqi@0: char *replace_cygdrive_cygwin(char *in) aoqi@0: { aoqi@0: int len = strlen(in); aoqi@0: char *out = malloc(len+1); aoqi@0: int i,j; aoqi@0: aoqi@0: if (len < 12) { aoqi@0: strcpy(out, in); aoqi@0: return out; aoqi@0: } aoqi@0: for (i = 0, j = 0; i *bl) { aoqi@0: *bl *= 2; aoqi@0: *b = realloc(*b, *bl); aoqi@0: } aoqi@0: memcpy(*b+*u, add, addlen); aoqi@0: *u += addlen; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Creates a new string from in where the first occurance of sub is aoqi@0: * replaced by rep. aoqi@0: */ aoqi@0: char *replace_substring(char *in, char *sub, char *rep) aoqi@0: { aoqi@0: int in_len = strlen(in); aoqi@0: int sub_len = strlen(sub); aoqi@0: int rep_len = strlen(rep); aoqi@0: char *out = malloc(in_len - sub_len + rep_len + 1); aoqi@0: char *p; aoqi@0: aoqi@0: if (!(p = strstr(in, sub))) { aoqi@0: // If sub isn't a substring of in, just return in. aoqi@0: return in; aoqi@0: } aoqi@0: aoqi@0: // Copy characters from beginning of in to start of sub. aoqi@0: strncpy(out, in, p - in); aoqi@0: out[p - in] = '\0'; aoqi@0: aoqi@0: sprintf(out + (p - in), "%s%s", rep, p + sub_len); aoqi@0: aoqi@0: return out; aoqi@0: } aoqi@0: aoqi@0: char* msys_path_list; // @-separated list of paths prefix to look for aoqi@0: char* msys_path_list_end; // Points to last \0 in msys_path_list. aoqi@0: aoqi@0: void setup_msys_path_list(char* argument) aoqi@0: { aoqi@0: char* p; aoqi@0: char* drive_letter_pos; aoqi@0: aoqi@0: msys_path_list = strdup(&argument[2]); aoqi@0: msys_path_list_end = &msys_path_list[strlen(msys_path_list)]; aoqi@0: aoqi@0: // Convert all at-sign (@) in path list to \0. aoqi@0: // @ was chosen as separator to minimize risk of other tools messing around with it aoqi@0: p = msys_path_list; aoqi@0: do { aoqi@0: if (p[1] == ':') { aoqi@0: // msys has mangled our path list, restore it from c:/... to /c/... aoqi@0: drive_letter_pos = p+1; aoqi@0: *drive_letter_pos = *p; aoqi@0: *p = '/'; aoqi@0: } aoqi@0: aoqi@0: // Look for an @ in the list aoqi@0: p = strchr(p, '@'); aoqi@0: if (p != NULL) { aoqi@0: *p = '\0'; aoqi@0: p++; aoqi@0: } aoqi@0: } while (p != NULL); aoqi@0: } aoqi@0: aoqi@0: char *replace_cygdrive_msys(char *in) aoqi@0: { aoqi@0: char* str; aoqi@0: char* prefix; aoqi@0: char* p; aoqi@0: aoqi@0: str = strdup(in); aoqi@0: aoqi@0: // For each prefix in the path list, search for it and replace /c/... with c:/... aoqi@0: for (prefix = msys_path_list; prefix < msys_path_list_end && prefix != NULL; prefix += strlen(prefix)+1) { aoqi@0: p=str; aoqi@0: while ((p = strstr(p, prefix))) { aoqi@0: char* drive_letter = p+1; aoqi@0: *p = *drive_letter; aoqi@0: *drive_letter = ':'; aoqi@0: p++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return str; aoqi@0: } aoqi@0: aoqi@0: char*(*replace_cygdrive)(char *in) = NULL; aoqi@0: aoqi@0: char *files_to_delete[1024]; aoqi@0: int num_files_to_delete = 0; aoqi@0: aoqi@0: char *fix_at_file(char *in) aoqi@0: { aoqi@0: char *tmpdir; aoqi@0: char name[2048]; aoqi@0: char *atname; aoqi@0: char *buffer; aoqi@0: size_t buflen=65536; aoqi@0: size_t used=0; aoqi@0: size_t len; aoqi@0: int rc; aoqi@0: FILE *atout; aoqi@0: FILE *atin; aoqi@0: char block[2048]; aoqi@0: size_t blocklen; aoqi@0: char *fixed; aoqi@0: aoqi@0: atin = fopen(in+1, "r"); aoqi@0: if (atin == NULL) { aoqi@0: fprintf(stderr, "Could not read at file %s\n", in+1); aoqi@0: exit(-1); aoqi@0: } aoqi@0: aoqi@0: tmpdir = getenv("TMP"); aoqi@0: if (tmpdir == NULL) { aoqi@0: tmpdir = "c:/cygwin/tmp"; aoqi@0: } aoqi@0: _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir); aoqi@0: aoqi@0: rc = _mktemp_s(name, strlen(name)+1); aoqi@0: if (rc) { aoqi@0: fprintf(stderr, "Could not create temporary file name for at file!\n"); aoqi@0: exit(-1); aoqi@0: } aoqi@0: aoqi@0: atout = fopen(name, "w"); aoqi@0: if (atout == NULL) { aoqi@0: fprintf(stderr, "Could not open temporary file for writing! %s\n", name); aoqi@0: exit(-1); aoqi@0: } aoqi@0: aoqi@0: buffer = malloc(buflen); aoqi@0: while((blocklen = fread(block,1,sizeof(block),atin)) > 0) { aoqi@0: append(&buffer, &buflen, &used, block, blocklen); aoqi@0: } aoqi@0: buffer[used] = 0; aoqi@0: if (getenv("DEBUG_FIXPATH") != NULL) { aoqi@0: fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer); aoqi@0: } aoqi@0: fixed = replace_cygdrive(buffer); aoqi@0: if (getenv("DEBUG_FIXPATH") != NULL) { aoqi@0: fprintf(stderr, "fixpath converted to @-file %s is: %s\n", name, fixed); aoqi@0: } aoqi@0: fwrite(fixed, strlen(fixed), 1, atout); aoqi@0: fclose(atin); aoqi@0: fclose(atout); aoqi@0: free(fixed); aoqi@0: free(buffer); aoqi@0: files_to_delete[num_files_to_delete] = malloc(strlen(name)+1); aoqi@0: strcpy(files_to_delete[num_files_to_delete], name); aoqi@0: num_files_to_delete++; aoqi@0: atname = malloc(strlen(name)+2); aoqi@0: atname[0] = '@'; aoqi@0: strcpy(atname+1, name); aoqi@0: return atname; aoqi@0: } aoqi@0: aoqi@0: int main(int argc, char **argv) aoqi@0: { aoqi@0: STARTUPINFO si; aoqi@0: PROCESS_INFORMATION pi; aoqi@0: unsigned short rc; aoqi@0: aoqi@0: char *new_at_file; aoqi@0: char *old_at_file; aoqi@0: char *line; aoqi@0: int i; aoqi@0: DWORD exitCode; aoqi@0: aoqi@0: if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) { aoqi@0: fprintf(stderr, "Usage: fixpath -c|m /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt\n"); aoqi@0: exit(0); aoqi@0: } aoqi@0: aoqi@0: if (getenv("DEBUG_FIXPATH") != NULL) { aoqi@0: fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1])); aoqi@0: } aoqi@0: aoqi@0: if (argv[1][1] == 'c' && argv[1][2] == '\0') { aoqi@0: if (getenv("DEBUG_FIXPATH") != NULL) { aoqi@0: fprintf(stderr, "using cygwin mode\n"); aoqi@0: } aoqi@0: replace_cygdrive = replace_cygdrive_cygwin; aoqi@0: } else if (argv[1][1] == 'm') { aoqi@0: if (getenv("DEBUG_FIXPATH") != NULL) { aoqi@0: fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]); aoqi@0: } aoqi@0: setup_msys_path_list(argv[1]); aoqi@0: replace_cygdrive = replace_cygdrive_msys; aoqi@0: } else { aoqi@0: fprintf(stderr, "Unknown mode: %s\n", argv[1]); aoqi@0: exit(-1); aoqi@0: } aoqi@0: line = replace_cygdrive(strstr(GetCommandLine(), argv[2])); aoqi@0: aoqi@0: for (i=1; i%s<\n", line); aoqi@0: } aoqi@0: aoqi@0: ZeroMemory(&si,sizeof(si)); aoqi@0: si.cb=sizeof(si); aoqi@0: ZeroMemory(&pi,sizeof(pi)); aoqi@0: aoqi@0: rc = CreateProcess(NULL, aoqi@0: line, aoqi@0: 0, aoqi@0: 0, aoqi@0: TRUE, aoqi@0: 0, aoqi@0: 0, aoqi@0: 0, aoqi@0: &si, aoqi@0: &pi); aoqi@0: if(!rc) { aoqi@0: // Could not start process for some reason. Try to report why: aoqi@0: report_error(); aoqi@0: exit(rc); aoqi@0: } aoqi@0: aoqi@0: WaitForSingleObject(pi.hProcess,INFINITE); aoqi@0: GetExitCodeProcess(pi.hProcess,&exitCode); aoqi@0: aoqi@0: if (getenv("DEBUG_FIXPATH") != NULL) { aoqi@0: for (i=0; i