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