Last active
November 1, 2016 12:06
-
-
Save DankRank/df8b9cb12afe5798a5384f6e8a3ae8ff to your computer and use it in GitHub Desktop.
windows bash runner that modifies PATH before running - fixes path/cmake/git for windows/visual studio/git_version.sh fuckery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define UNICODE | |
#define _UNICODE | |
#include <windows.h> | |
#include <wchar.h> | |
#include <string.h> | |
#define MAX_VAR 32767 | |
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ | |
wchar_t path[MAX_VAR]; | |
GetEnvironmentVariable(L"PATH",path,MAX_VAR); | |
wcsncat(path,L";C:\\Program Files\\Git\\usr\\bin",MAX_VAR); | |
SetEnvironmentVariable(L"PATH",path); | |
LPWSTR args=GetCommandLine(); | |
//skip argv0 | |
if(args[0]=='"'){ | |
args++; | |
for(;args[0] != '"' && args[0] != '\0'; args++) | |
; | |
} | |
for(;args[0] != ' ' && args[0] != '\0'; args++) | |
; | |
if(args[0]) args++; | |
wchar_t* args2 = new wchar_t[7+wcslen(args)+1]; | |
wcscpy(args2,L"sh.exe "); | |
wcscat(args2,args); | |
STARTUPINFO si={sizeof(STARTUPINFO)}; | |
PROCESS_INFORMATION pi; | |
si.dwFlags = STARTF_USESTDHANDLES; | |
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | |
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | |
si.hStdError = GetStdHandle(STD_ERROR_HANDLE); | |
int res = CreateProcess( | |
L"C:\\Program Files\\Git\\usr\\bin\\sh.exe", //lpApplicationName | |
args2, //lpCommandLine | |
NULL, //lpProcessAttributes | |
NULL, //lpThreadAttributes | |
TRUE, //bInheritHandles | |
CREATE_NO_WINDOW, //dwCreationFlags | |
NULL,//lpEnvironment | |
NULL,//lpCurrentDirectory | |
&si,//lpStartupInfo | |
&pi//lpProcessInformation | |
); | |
if(res){ | |
WaitForSingleObject(pi.hProcess,INFINITE); | |
CloseHandle(pi.hProcess); | |
CloseHandle(pi.hThread); | |
} | |
delete[] args2; | |
return res ? 0 : 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment