Skip to content

Instantly share code, notes, and snippets.

@juntalis
Created November 19, 2012 20:15
Show Gist options
  • Save juntalis/4113598 to your computer and use it in GitHub Desktop.
Save juntalis/4113598 to your computer and use it in GitHub Desktop.
MSYS bash stub
#include <WinSDKVer.h>
#define _MBCS 1
#define MBCS 1
// Target Windows 2000 & higher
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
// Speed up build process with minimal headers.
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <SDKDDKVer.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <io.h>
/* Macros for prettier code. */
#ifndef MAX_PATH
# define MAX_PATH _MAX_PATH
#endif
#define zstr(str,sz) memset(str, '\0', sz)
#define tchar TCHAR
#define ctstr ctchar*
#define tstr tchar*
#ifdef _UNICODE
# define _tspawnvp _wspawnvp
# define _tspawnlp _wspawnlp
#else
# define _tspawnvp _spawnvp
# define _tspawnlp _spawnlp
#endif
#ifndef MAX_PATH
# define MAX_PATH _MAX_PATH
#endif
#ifndef TEXT
# define TEXT _T
#endif
#ifndef TC
# define TC _T
#endif
#define EMPTY TEXT("")
static int exec_cmd(tchar* cmd, tchar** args)
{
return (args == NULL) ?
(int)_tspawnlp(_P_WAIT, cmd, cmd, NULL) :
(int)_tspawnvp(_P_WAIT, cmd, (const tchar* const*)args);
}
int _tmain(int argc, tchar* argv[])
{
UINT cpOld;
int result, i = -1;
TCHAR pProg[MAX_PATH+1] = EMPTY,
pMsysRoot[MAX_PATH+10] = EMPTY, // MAX_PATH + len("MSYSROOT=") + 1
pHome[MAX_PATH+6] = EMPTY, // MAX_PATH + len("HOME=") + 1
//pPath[_MAX_ENV+6] = EMPTY, // _MAX_ENV + len("PATH=") + 1
//*pCurrPath = NULL,
*pUserName = NULL,
*pRoot = NULL;
if((pRoot = _tgetenv(TEXT("MINGWROOT"))) == NULL) {
_tprintf(TEXT("FATAL: Could not locate mingw root.\n"));
ExitProcess(1);
}
/*if((pCurrPath = _tgetenv(TEXT("PATH"))) == NULL) {
_tprintf(TEXT("FATAL: Could not get PATH.\n"));
ExitProcess(1);
}*/
if((pUserName = _tgetenv(TEXT("USERNAME"))) == NULL) {
_tprintf(TEXT("FATAL: Could not get current user.\n"));
ExitProcess(1);
}
_tcscpy(pHome, TEXT("HOME="));
_tcscpy(pMsysRoot, TEXT("MSYSROOT="));
//_tcscpy(pPath, TEXT("PATH="));
_tcsncat(pHome, pRoot, MAX_PATH+5);
_tcsncat(pHome, TEXT("\\msys\\1.0\\home\\"), MAX_PATH+5);
_tcsncat(pHome, pUserName, MAX_PATH+5);
_tcsncat(pMsysRoot, pRoot, MAX_PATH+9);
_tcsncat(pMsysRoot, TEXT("\\msys\\1.0\\"), MAX_PATH+9);
while(pHome[++i] != TC('\0'))
if(pHome[i] == TC('\\'))
pHome[i] = TC('/');
_tputenv(pHome);
_tputenv(pMsysRoot);
_tputenv(TEXT("MSYSCON=sh.exe"));
_tputenv(TEXT("PLINK_PROTOCOL=ssh"));
_tcsncpy(pProg, pRoot, MAX_PATH);
_tcsncat(pProg, TEXT("\\msys\\1.0\\bin\\bash.exe"), MAX_PATH);
argv[0] = pProg;
cpOld = GetConsoleOutputCP();
SetConsoleOutputCP(1252);
result = exec_cmd(pProg, argv);
SetConsoleOutputCP(cpOld);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment