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