probably exit nsExec's internal process with ExitProcess

without this, if running nsExec immediately after booting, it'd always return 0 even if CreateProcess failed to execute
more @ http://forums.winamp.com/showthread.php?threadid=279683

git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@5363 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
kichik 2007-11-09 18:29:18 +00:00
parent 7aec6c7e74
commit 183a176b32

View file

@ -1,453 +1,457 @@
/* /*
Copyright (c) 2002 Robert Rainwater <rrainwater@yahoo.com> Copyright (c) 2002 Robert Rainwater <rrainwater@yahoo.com>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
arising from the use of this software. arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions: freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not 1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be in a product, an acknowledgment in the product documentation would be
appreciated but is not required. appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be 2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software. misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
#include <windows.h> #include <windows.h>
#include <commctrl.h> #include <commctrl.h>
#include <winnt.h> #include <winnt.h>
#include "../ExDLL/exdll.h" #include "../ExDLL/exdll.h"
#ifndef true #ifndef true
#define true TRUE #define true TRUE
#endif #endif
#ifndef false #ifndef false
#define false FALSE #define false FALSE
#endif #endif
#define LOOPTIMEOUT 100 #define LOOPTIMEOUT 100
HWND g_hwndParent; HWND g_hwndParent;
HWND g_hwndList; HWND g_hwndList;
void ExecScript(BOOL log); void ExecScript(BOOL log);
void LogMessage(const char *pStr, BOOL bOEM); void LogMessage(const char *pStr, BOOL bOEM);
char *my_strstr(char *a, char *b); char *my_strstr(char *a, char *b);
unsigned int my_atoi(char *s); unsigned int my_atoi(char *s);
void __declspec(dllexport) Exec(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) { void __declspec(dllexport) Exec(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
g_hwndParent=hwndParent; g_hwndParent=hwndParent;
EXDLL_INIT(); EXDLL_INIT();
{ {
ExecScript(0); ExecScript(0);
} }
} }
void __declspec(dllexport) ExecToLog(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) { void __declspec(dllexport) ExecToLog(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
g_hwndParent=hwndParent; g_hwndParent=hwndParent;
EXDLL_INIT(); EXDLL_INIT();
{ {
ExecScript(1); ExecScript(1);
} }
} }
void __declspec(dllexport) ExecToStack(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) { void __declspec(dllexport) ExecToStack(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
g_hwndParent=hwndParent; g_hwndParent=hwndParent;
EXDLL_INIT(); EXDLL_INIT();
{ {
ExecScript(2); ExecScript(2);
} }
} }
HINSTANCE g_hInst; HINSTANCE g_hInst;
BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) { BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) {
g_hInst = hInst; g_hInst = hInst;
return TRUE; return TRUE;
} }
#define TAB_REPLACE " " #define TAB_REPLACE " "
#define TAB_REPLACE_SIZE (sizeof(TAB_REPLACE)-1) #define TAB_REPLACE_SIZE (sizeof(TAB_REPLACE)-1)
// Turn a pair of chars into a word // Turn a pair of chars into a word
// Turn four chars into a dword // Turn four chars into a dword
#ifdef __BIG_ENDIAN__ // Not very likely, but, still... #ifdef __BIG_ENDIAN__ // Not very likely, but, still...
#define CHAR2_TO_WORD(a,b) (((WORD)(b))|((a)<<8)) #define CHAR2_TO_WORD(a,b) (((WORD)(b))|((a)<<8))
#define CHAR4_TO_DWORD(a,b,c,d) (((DWORD)CHAR2_TO_WORD(c,d))|(CHAR2_TO_WORD(a,b)<<16)) #define CHAR4_TO_DWORD(a,b,c,d) (((DWORD)CHAR2_TO_WORD(c,d))|(CHAR2_TO_WORD(a,b)<<16))
#else #else
#define CHAR2_TO_WORD(a,b) (((WORD)(a))|((b)<<8)) #define CHAR2_TO_WORD(a,b) (((WORD)(a))|((b)<<8))
#define CHAR4_TO_DWORD(a,b,c,d) (((DWORD)CHAR2_TO_WORD(a,b))|(CHAR2_TO_WORD(c,d)<<16)) #define CHAR4_TO_DWORD(a,b,c,d) (((DWORD)CHAR2_TO_WORD(a,b))|(CHAR2_TO_WORD(c,d)<<16))
#endif #endif
void ExecScript(int log) { void ExecScript(int log) {
char szRet[128] = ""; char szRet[128] = "";
char *pExec; char *pExec;
int nComSpecSize; int nComSpecSize;
char meDLLPath[MAX_PATH]; char meDLLPath[MAX_PATH];
char *p; char *p;
char *executor; char *executor;
char *g_exec; char *g_exec;
unsigned int g_to; unsigned int g_to;
BOOL bOEM; BOOL bOEM;
nComSpecSize = GetModuleFileName(g_hInst, meDLLPath, MAX_PATH) + 2; // 2 chars for quotes nComSpecSize = GetModuleFileName(g_hInst, meDLLPath, MAX_PATH) + 2; // 2 chars for quotes
p = meDLLPath + nComSpecSize - 2; // point p at null char of meDLLPath p = meDLLPath + nComSpecSize - 2; // point p at null char of meDLLPath
g_exec = (char *)GlobalAlloc(GPTR, sizeof(char)*g_stringsize+nComSpecSize+2); // 1 for space, 1 for null g_exec = (char *)GlobalAlloc(GPTR, sizeof(char)*g_stringsize+nComSpecSize+2); // 1 for space, 1 for null
*g_exec = '"'; *g_exec = '"';
executor = g_exec + 1; executor = g_exec + 1;
do do
{ {
if (*p == '\\') if (*p == '\\')
break; break;
p = CharPrev(meDLLPath, p); p = CharPrev(meDLLPath, p);
} }
while (p > meDLLPath); while (p > meDLLPath);
if (p == meDLLPath) if (p == meDLLPath)
{ {
// bad path // bad path
pushstring("error"); pushstring("error");
GlobalFree(g_exec); GlobalFree(g_exec);
return; return;
} }
*p = 0; *p = 0;
GetTempFileName(meDLLPath, "ns", 0, executor); GetTempFileName(meDLLPath, "ns", 0, executor);
*p = '\\'; *p = '\\';
if (CopyFile(meDLLPath, executor, FALSE)) if (CopyFile(meDLLPath, executor, FALSE))
{ {
HANDLE hFile, hMapping; HANDLE hFile, hMapping;
LPBYTE pMapView; LPBYTE pMapView;
PIMAGE_NT_HEADERS pNTHeaders; PIMAGE_NT_HEADERS pNTHeaders;
hFile = CreateFile(executor, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0, 0); hFile = CreateFile(executor, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0, 0);
hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
pMapView = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0); pMapView = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);
if (pMapView) if (pMapView)
{ {
pNTHeaders = (PIMAGE_NT_HEADERS)(pMapView + ((PIMAGE_DOS_HEADER)pMapView)->e_lfanew); pNTHeaders = (PIMAGE_NT_HEADERS)(pMapView + ((PIMAGE_DOS_HEADER)pMapView)->e_lfanew);
pNTHeaders->FileHeader.Characteristics = IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_LOCAL_SYMS_STRIPPED | pNTHeaders->FileHeader.Characteristics = IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_LOCAL_SYMS_STRIPPED |
IMAGE_FILE_LINE_NUMS_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE; IMAGE_FILE_LINE_NUMS_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE;
pNTHeaders->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI; pNTHeaders->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
pNTHeaders->OptionalHeader.AddressOfEntryPoint = (DWORD)WinMain - (DWORD)g_hInst; pNTHeaders->OptionalHeader.AddressOfEntryPoint = (DWORD)WinMain - (DWORD)g_hInst;
UnmapViewOfFile(pMapView); UnmapViewOfFile(pMapView);
} }
CloseHandle(hMapping); CloseHandle(hMapping);
CloseHandle(hFile); CloseHandle(hFile);
} }
lstrcat(g_exec, "\""); lstrcat(g_exec, "\"");
g_to = 0; // default is no timeout g_to = 0; // default is no timeout
bOEM = FALSE; // default is no OEM->ANSI conversion bOEM = FALSE; // default is no OEM->ANSI conversion
g_hwndList = NULL; g_hwndList = NULL;
if (g_hwndParent) if (g_hwndParent)
g_hwndList = FindWindowEx(FindWindowEx(g_hwndParent,NULL,"#32770",NULL),NULL,"SysListView32",NULL); g_hwndList = FindWindowEx(FindWindowEx(g_hwndParent,NULL,"#32770",NULL),NULL,"SysListView32",NULL);
// add space // add space
pExec = g_exec + lstrlen(g_exec); pExec = g_exec + lstrlen(g_exec);
*pExec = ' '; *pExec = ' ';
pExec++; pExec++;
params: params:
popstring(pExec); popstring(pExec);
if (my_strstr(pExec, "/TIMEOUT=") == pExec) { if (my_strstr(pExec, "/TIMEOUT=") == pExec) {
char *szTimeout = pExec + 9; char *szTimeout = pExec + 9;
g_to = my_atoi(szTimeout); g_to = my_atoi(szTimeout);
*pExec = 0; *pExec = 0;
goto params; goto params;
} }
if (!lstrcmpi(pExec, "/OEM")) { if (!lstrcmpi(pExec, "/OEM")) {
bOEM = TRUE; bOEM = TRUE;
*pExec = 0; *pExec = 0;
goto params; goto params;
} }
if (!pExec[0]) if (!pExec[0])
{ {
pushstring("error"); pushstring("error");
*(pExec-2) = '\0'; // skip space and quote *(pExec-2) = '\0'; // skip space and quote
DeleteFile(executor); DeleteFile(executor);
GlobalFree(g_exec); GlobalFree(g_exec);
return; return;
} }
{ {
STARTUPINFO si={sizeof(si),}; STARTUPINFO si={sizeof(si),};
SECURITY_ATTRIBUTES sa={sizeof(sa),}; SECURITY_ATTRIBUTES sa={sizeof(sa),};
SECURITY_DESCRIPTOR sd={0,}; SECURITY_DESCRIPTOR sd={0,};
PROCESS_INFORMATION pi={0,}; PROCESS_INFORMATION pi={0,};
OSVERSIONINFO osv={sizeof(osv)}; OSVERSIONINFO osv={sizeof(osv)};
HANDLE newstdout=0,read_stdout=0; HANDLE newstdout=0,read_stdout=0;
HANDLE newstdin=0,read_stdin=0; HANDLE newstdin=0,read_stdin=0;
DWORD dwRead = 1; DWORD dwRead = 1;
DWORD dwExit = !STILL_ACTIVE; DWORD dwExit = !STILL_ACTIVE;
DWORD dwLastOutput; DWORD dwLastOutput;
static char szBuf[1024]; static char szBuf[1024];
HGLOBAL hUnusedBuf = NULL; HGLOBAL hUnusedBuf = NULL;
char *szUnusedBuf = 0; char *szUnusedBuf = 0;
if (log) { if (log) {
hUnusedBuf = GlobalAlloc(GHND, log & 2 ? g_stringsize : sizeof(szBuf)*4); hUnusedBuf = GlobalAlloc(GHND, log & 2 ? g_stringsize : sizeof(szBuf)*4);
if (!hUnusedBuf) { if (!hUnusedBuf) {
lstrcpy(szRet, "error"); lstrcpy(szRet, "error");
goto done; goto done;
} }
szUnusedBuf = (char *)GlobalLock(hUnusedBuf); szUnusedBuf = (char *)GlobalLock(hUnusedBuf);
} }
GetVersionEx(&osv); GetVersionEx(&osv);
if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT) { if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT) {
InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION); InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd,true,NULL,false); SetSecurityDescriptorDacl(&sd,true,NULL,false);
sa.lpSecurityDescriptor = &sd; sa.lpSecurityDescriptor = &sd;
} }
else else
sa.lpSecurityDescriptor = NULL; sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = true; sa.bInheritHandle = true;
if (!CreatePipe(&read_stdout,&newstdout,&sa,0)) { if (!CreatePipe(&read_stdout,&newstdout,&sa,0)) {
lstrcpy(szRet, "error"); lstrcpy(szRet, "error");
goto done; goto done;
} }
if (!CreatePipe(&read_stdin,&newstdin,&sa,0)) { if (!CreatePipe(&read_stdin,&newstdin,&sa,0)) {
lstrcpy(szRet, "error"); lstrcpy(szRet, "error");
goto done; goto done;
} }
GetStartupInfo(&si); GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; si.wShowWindow = SW_HIDE;
si.hStdInput = newstdin; si.hStdInput = newstdin;
si.hStdOutput = newstdout; si.hStdOutput = newstdout;
si.hStdError = newstdout; si.hStdError = newstdout;
if (!CreateProcess(NULL,g_exec,NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)) { if (!CreateProcess(NULL,g_exec,NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)) {
lstrcpy(szRet, "error"); lstrcpy(szRet, "error");
goto done; goto done;
} }
dwLastOutput = GetTickCount(); dwLastOutput = GetTickCount();
while (dwExit == STILL_ACTIVE || dwRead) { while (dwExit == STILL_ACTIVE || dwRead) {
PeekNamedPipe(read_stdout, 0, 0, 0, &dwRead, NULL); PeekNamedPipe(read_stdout, 0, 0, 0, &dwRead, NULL);
if (dwRead) { if (dwRead) {
dwLastOutput = GetTickCount(); dwLastOutput = GetTickCount();
ReadFile(read_stdout, szBuf, sizeof(szBuf)-1, &dwRead, NULL); ReadFile(read_stdout, szBuf, sizeof(szBuf)-1, &dwRead, NULL);
szBuf[dwRead] = 0; szBuf[dwRead] = 0;
if (log) { if (log) {
char *p, *p2; char *p, *p2;
SIZE_T iReqLen = lstrlen(szBuf) + lstrlen(szUnusedBuf); SIZE_T iReqLen = lstrlen(szBuf) + lstrlen(szUnusedBuf);
if (GlobalSize(hUnusedBuf) < iReqLen && (iReqLen < g_stringsize || !(log & 2))) { if (GlobalSize(hUnusedBuf) < iReqLen && (iReqLen < g_stringsize || !(log & 2))) {
GlobalUnlock(hUnusedBuf); GlobalUnlock(hUnusedBuf);
hUnusedBuf = GlobalReAlloc(hUnusedBuf, iReqLen+sizeof(szBuf), GHND); hUnusedBuf = GlobalReAlloc(hUnusedBuf, iReqLen+sizeof(szBuf), GHND);
if (!hUnusedBuf) { if (!hUnusedBuf) {
lstrcpy(szRet, "error"); lstrcpy(szRet, "error");
break; break;
} }
szUnusedBuf = (char *)GlobalLock(hUnusedBuf); szUnusedBuf = (char *)GlobalLock(hUnusedBuf);
} }
p = szUnusedBuf; // get the old left overs p = szUnusedBuf; // get the old left overs
if (iReqLen < g_stringsize || !(log & 2)) lstrcat(p, szBuf); if (iReqLen < g_stringsize || !(log & 2)) lstrcat(p, szBuf);
else { else {
lstrcpyn(p + lstrlen(p), szBuf, g_stringsize - lstrlen(p)); lstrcpyn(p + lstrlen(p), szBuf, g_stringsize - lstrlen(p));
} }
if (!(log & 2)) { if (!(log & 2)) {
while ((p = my_strstr(p, "\t"))) { while ((p = my_strstr(p, "\t"))) {
if ((int)(p - szUnusedBuf) > (int)(GlobalSize(hUnusedBuf) - TAB_REPLACE_SIZE - 1)) if ((int)(p - szUnusedBuf) > (int)(GlobalSize(hUnusedBuf) - TAB_REPLACE_SIZE - 1))
{ {
*p++ = ' '; *p++ = ' ';
} }
else else
{ {
int len = lstrlen(p); int len = lstrlen(p);
char *c_out=(char*)p+TAB_REPLACE_SIZE+len; char *c_out=(char*)p+TAB_REPLACE_SIZE+len;
char *c_in=(char *)p+len; char *c_in=(char *)p+len;
while (len-- > 0) { while (len-- > 0) {
*c_out--=*c_in--; *c_out--=*c_in--;
} }
lstrcpy(p, TAB_REPLACE); lstrcpy(p, TAB_REPLACE);
p += TAB_REPLACE_SIZE; p += TAB_REPLACE_SIZE;
*p = ' '; *p = ' ';
} }
} }
p = szUnusedBuf; // get the old left overs p = szUnusedBuf; // get the old left overs
for (p2 = p; *p2;) { for (p2 = p; *p2;) {
if (*p2 == '\r') { if (*p2 == '\r') {
*p2++ = 0; *p2++ = 0;
continue; continue;
} }
if (*p2 == '\n') { if (*p2 == '\n') {
*p2 = 0; *p2 = 0;
while (!*p && p != p2) p++; while (!*p && p != p2) p++;
LogMessage(p, bOEM); LogMessage(p, bOEM);
p = ++p2; p = ++p2;
continue; continue;
} }
p2 = CharNext(p2); p2 = CharNext(p2);
} }
// If data was taken out from the unused buffer, move p contents to the start of szUnusedBuf // If data was taken out from the unused buffer, move p contents to the start of szUnusedBuf
if (p != szUnusedBuf) { if (p != szUnusedBuf) {
char *p2 = szUnusedBuf; char *p2 = szUnusedBuf;
while (*p) *p2++ = *p++; while (*p) *p2++ = *p++;
*p2 = 0; *p2 = 0;
} }
} }
} }
} }
else { else {
if (g_to && GetTickCount() > dwLastOutput+g_to) { if (g_to && GetTickCount() > dwLastOutput+g_to) {
TerminateProcess(pi.hProcess, -1); TerminateProcess(pi.hProcess, -1);
lstrcpy(szRet, "timeout"); lstrcpy(szRet, "timeout");
} }
else Sleep(LOOPTIMEOUT); else Sleep(LOOPTIMEOUT);
} }
GetExitCodeProcess(pi.hProcess, &dwExit); GetExitCodeProcess(pi.hProcess, &dwExit);
if (dwExit != STILL_ACTIVE) { if (dwExit != STILL_ACTIVE) {
PeekNamedPipe(read_stdout, 0, 0, 0, &dwRead, NULL); PeekNamedPipe(read_stdout, 0, 0, 0, &dwRead, NULL);
} }
} }
done: done:
if (log & 2) pushstring(szUnusedBuf); if (log & 2) pushstring(szUnusedBuf);
if (log & 1 && *szUnusedBuf) LogMessage(szUnusedBuf, bOEM); if (log & 1 && *szUnusedBuf) LogMessage(szUnusedBuf, bOEM);
if ( dwExit == STATUS_ILLEGAL_INSTRUCTION ) if ( dwExit == STATUS_ILLEGAL_INSTRUCTION )
lstrcpy(szRet, "error"); lstrcpy(szRet, "error");
if (!szRet[0]) wsprintf(szRet,"%d",dwExit); if (!szRet[0]) wsprintf(szRet,"%d",dwExit);
pushstring(szRet); pushstring(szRet);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(newstdout); CloseHandle(newstdout);
CloseHandle(read_stdout); CloseHandle(read_stdout);
CloseHandle(newstdin); CloseHandle(newstdin);
CloseHandle(read_stdin); CloseHandle(read_stdin);
*(pExec-2) = '\0'; // skip space and quote *(pExec-2) = '\0'; // skip space and quote
DeleteFile(executor); DeleteFile(executor);
GlobalFree(g_exec); GlobalFree(g_exec);
if (log) { if (log) {
GlobalUnlock(hUnusedBuf); GlobalUnlock(hUnusedBuf);
GlobalFree(hUnusedBuf); GlobalFree(hUnusedBuf);
} }
} }
} }
// Tim Kosse's LogMessage // Tim Kosse's LogMessage
void LogMessage(const char *pStr, BOOL bOEM) { void LogMessage(const char *pStr, BOOL bOEM) {
LVITEM item={0}; LVITEM item={0};
int nItemCount; int nItemCount;
if (!g_hwndList) return; if (!g_hwndList) return;
//if (!lstrlen(pStr)) return; //if (!lstrlen(pStr)) return;
if (bOEM == TRUE) OemToCharBuff(pStr, (char *)pStr, lstrlen(pStr)); if (bOEM == TRUE) OemToCharBuff(pStr, (char *)pStr, lstrlen(pStr));
nItemCount=SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0); nItemCount=SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0);
item.mask=LVIF_TEXT; item.mask=LVIF_TEXT;
item.pszText=(char *)pStr; item.pszText=(char *)pStr;
item.cchTextMax=0; item.cchTextMax=0;
item.iItem=nItemCount; item.iItem=nItemCount;
ListView_InsertItem(g_hwndList, &item); ListView_InsertItem(g_hwndList, &item);
ListView_EnsureVisible(g_hwndList, item.iItem, 0); ListView_EnsureVisible(g_hwndList, item.iItem, 0);
} }
char *my_strstr(char *a, char *b) char *my_strstr(char *a, char *b)
{ {
int l = lstrlen(b); int l = lstrlen(b);
while (lstrlen(a) >= l) while (lstrlen(a) >= l)
{ {
char c = a[l]; char c = a[l];
a[l] = 0; a[l] = 0;
if (!lstrcmpi(a, b)) if (!lstrcmpi(a, b))
{ {
a[l] = c; a[l] = c;
return a; return a;
} }
a[l] = c; a[l] = c;
a = CharNext(a); a = CharNext(a);
} }
return NULL; return NULL;
} }
unsigned int my_atoi(char *s) { unsigned int my_atoi(char *s) {
unsigned int v=0; unsigned int v=0;
if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) { if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
s+=2; s+=2;
for (;;) { for (;;) {
int c=*s++; int c=*s++;
if (c >= '0' && c <= '9') c-='0'; if (c >= '0' && c <= '9') c-='0';
else if (c >= 'a' && c <= 'f') c-='a'-10; else if (c >= 'a' && c <= 'f') c-='a'-10;
else if (c >= 'A' && c <= 'F') c-='A'-10; else if (c >= 'A' && c <= 'F') c-='A'-10;
else break; else break;
v<<=4; v<<=4;
v+=c; v+=c;
} }
} }
else if (*s == '0' && s[1] <= '7' && s[1] >= '0') { else if (*s == '0' && s[1] <= '7' && s[1] >= '0') {
s++; s++;
for (;;) { for (;;) {
int c=*s++; int c=*s++;
if (c >= '0' && c <= '7') c-='0'; if (c >= '0' && c <= '7') c-='0';
else break; else break;
v<<=3; v<<=3;
v+=c; v+=c;
} }
} }
else { else {
for (;;) { for (;;) {
int c=*s++ - '0'; int c=*s++ - '0';
if (c < 0 || c > 9) break; if (c < 0 || c > 9) break;
v*=10; v*=10;
v+=c; v+=c;
} }
} }
return (int)v; return (int)v;
} }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ {
DWORD Ret; DWORD Ret;
STARTUPINFO si = {0}; STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0}; PROCESS_INFORMATION pi = {0};
char command_line[1024]; char command_line[1024];
char seekchar=' '; char seekchar=' ';
char *cmdline; char *cmdline;
si.cb = sizeof(si); si.cb = sizeof(si);
// Make child process use this app's standard files. Not needed because the handles // Make child process use this app's standard files. Not needed because the handles
// we created when executing this process were inheritable. // we created when executing this process were inheritable.
//si.dwFlags = STARTF_USESTDHANDLES; //si.dwFlags = STARTF_USESTDHANDLES;
//si.hStdInput = GetStdHandle (STD_INPUT_HANDLE); //si.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
//si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE); //si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
//si.hStdError = GetStdHandle (STD_ERROR_HANDLE); //si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
lstrcpyn(command_line, GetCommandLine(), 1024); lstrcpyn(command_line, GetCommandLine(), 1024);
cmdline = command_line; cmdline = command_line;
if (*cmdline == '\"') seekchar = *cmdline++; if (*cmdline == '\"') seekchar = *cmdline++;
while (*cmdline && *cmdline != seekchar) cmdline=CharNext(cmdline); while (*cmdline && *cmdline != seekchar) cmdline=CharNext(cmdline);
cmdline=CharNext(cmdline); cmdline=CharNext(cmdline);
// skip any spaces before the arguments // skip any spaces before the arguments
while (*cmdline && *cmdline == ' ') cmdline++; while (*cmdline && *cmdline == ' ') cmdline++;
Ret = CreateProcess (NULL, cmdline, Ret = CreateProcess (NULL, cmdline,
NULL, NULL, NULL, NULL,
TRUE, 0, TRUE, 0,
NULL, NULL, NULL, NULL,
&si, &pi &si, &pi
); );
if (Ret) if (Ret)
{ {
do do
{ {
GetExitCodeProcess(pi.hProcess, &Ret); GetExitCodeProcess(pi.hProcess, &Ret);
Sleep(LOOPTIMEOUT); Sleep(LOOPTIMEOUT);
} while ( Ret == STILL_ACTIVE ); } while ( Ret == STILL_ACTIVE );
CloseHandle (pi.hProcess); CloseHandle (pi.hProcess);
CloseHandle (pi.hThread); CloseHandle (pi.hThread);
return Ret; ExitProcess(Ret);
} }
else else
return STATUS_ILLEGAL_INSTRUCTION; {
} ExitProcess(STATUS_ILLEGAL_INSTRUCTION);
}
return 0; // dummy
}