-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.c
70 lines (62 loc) · 1.18 KB
/
entry.c
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
66
67
68
69
70
#include "minicrt.h"
#ifdef WIN32
#include <Windows.h>
#endif
extern int main(int argc, char *argv[]); //outside function "main"
void exit(int);
static void crt_fatal_error(const char * msg)
{
exit(1);
}
void mini_crt_entry(void)
{
int ret;
#ifdef WIN32
int flag = 0;
int argc = 0;
char* argv[16]; //most 16 paraments
char* cl = GetCommandLineA();
//resolve command line
argv[0] = cl;
argc++;
while(*cl){
if(*cl == '\"')
if(flag == 0) flag = 1;
else flag = 0;
else if(*cl == ' ' && flag == 0){
if(*(cl + 1)){
argv[argc] = cl + 1; //fill in argv[]
argc++;
}
*cl = '\0';
}
cl++
}
#else
int argc;
char** argv;
char* ebp_reg = 0;
// ebp_reg = %ebp
asm("movl %%ebp,%0 \n":"=r"(ebp_reg));
argc = *(int *)(ebp_reg+4);
argv = (char **)(ebp_reg+8);
#endif
if(!mini_crt_heap_init())
crt_fatal_error("heap initialize failed");
if(!mini_crt_io_init())
crt_fatal_error("IO initialize failed");
ret = main(argc,argv);
exit(ret);
}
void exit(int exitCode)
{
//mini_crt_call_exit_routine();
#ifdef WIN32
ExitProcess(exitCode);
#else
asm("movl %0,%%ebx \n\t"
"movl $1,%%eax \n\t"
"int $0x80 \n\t"
"hlt \n\t"::"m"(exitCode));
#endif
}