1 /*=============================================================================
4 Copyright © 2008 Bruno Santos <nayart3@gmail.com>
5 =============================================================================*/
14 ///////////////////////////////////////////////////////////////////////////////
15 static unsigned int get_command_line(char** dst
)
17 wchar_t* cmd
= GetCommandLineW();
20 len
= WideCharToMultiByte(CP_UTF8
, 0, cmd
, -1, NULL
, 0, NULL
, NULL
);
22 return WideCharToMultiByte(CP_UTF8
, 0, cmd
, -1, *dst
, len
, NULL
, NULL
);
25 static char** init_argv(int* argc
)
35 len
= get_command_line(&cmd
);
37 * - 2n backslashes followed by a quotation mark produce n backslashes
38 * followed by a quotation mark.
39 * - (2n) + 1 backslashes followed by a quotation mark again produce n
40 * backslashes followed by a quotation mark.
41 * - n backslashes not followed by a quotation mark simply produce n
47 for (i
= 0; i
< len
; ++i
) {
52 } else if (cmd
[i
] == '\"') {
54 bslash
-= (bslash
/ 2);
55 memmove(&cmd
[i
- bslash
], &cmd
[i
], len
- i
);
60 cmd
[quote
- 1] = '\0';
63 if (cmd
[i
- 1] != '\0') {
70 } else if (cmd
[i
] == ' ' && !quote
) {
72 if (i
&& cmd
[i
- 1] != '\0')
75 } else if (cmd
[i
] == '\0') {
76 if (i
&& cmd
[i
- 1] != '\0')
82 argv
= realloc(cmd
, ((argn
+ 1) * sizeof(char*)) + i
);
83 cmd
= memmove(argv
+ argn
, argv
, len
);
84 for (i
= 0; i
< argn
; ++i
) {
97 static char** init_env()
102 ///////////////////////////////////////////////////////////////////////////////
103 void mainCRTStartup(void)
110 g_process_heap
= GetProcessHeap();
112 argv
= init_argv(&argc
);
114 mainret
= main(argc
, argv
, env
);
122 if (g_tcrt_sigaction_slot
[SIGABRT
].sa_handler
!= SIG_DFL
)
123 g_tcrt_sigaction_slot
[SIGABRT
].sa_handler(SIGABRT
);
126 TerminateProcess(GetCurrentProcess(), EXIT_FAILURE
);
131 void exit(int status
)
138 long _InterlockedCompareExchange(long volatile*, long, long);
140 __int64
_InterlockedCompareExchange64(__int64
volatile*, __int64
, __int64
);
143 static atexit_entry
* atomic_cmpxchg(atexit_entry
** dst
, atexit_entry
* value
, atexit_entry
* cmp
)
146 return (atexit_entry
*) _InterlockedCompareExchange((long volatile*) dst
, (long) value
, (long) cmp
);
148 return (atexit_entry
*) _InterlockedCompareExchange((__int64
volatile*) dst
, (__int64
) value
, (__int64
) cmp
);
152 int atexit(void (*function
)(void))
157 entry
= (atexit_entry
*) malloc(sizeof(atexit_entry
));
163 entry
->fn
= function
;
167 } while (atomic_cmpxchg(&g_atexit_list
, entry
, old
) != old
);
178 // EOF ////////////////////////////////////////////////////////////////////////