-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrt0_process.cpp
More file actions
65 lines (50 loc) · 1.09 KB
/
crt0_process.cpp
File metadata and controls
65 lines (50 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "crt0_common.hpp"
#include <Windows.h>
#include <process.h>
void(*crt0_abort_hook)() = nullptr;
CRT_API void CRT_CALL abort()
{
if (crt0_abort_hook)
crt0_abort_hook();
TerminateProcess(GetCurrentProcess(), 3);
for (;;) {}
}
CRT_API void CRT_CALL exit(int code)
{
ExitProcess(static_cast<UINT>(code));
}
CRT_API int CRT_CALL system(const char *command)
{
if (!command)
return 1;
STARTUPINFOA si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
char cmdline[1024];
strncpy(cmdline, command, sizeof(cmdline));
BOOL ok = CreateProcessA(
nullptr,
cmdline,
nullptr,
nullptr,
FALSE,
CREATE_NO_WINDOW,
nullptr,
nullptr,
&si,
&pi
);
if (!ok)
return -1;
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exit_code = 0;
GetExitCodeProcess(pi.hProcess, &exit_code);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return static_cast<int>(exit_code);
}
CRT_API char *CRT_CALL getenv(const char *name)
{
static char buffer[512];
DWORD len = GetEnvironmentVariableA(name, buffer, sizeof(buffer));
return (len > 0 && len < sizeof(buffer)) ? buffer : nullptr;
}