6 #include <wchar.h> // Watcom bug: winnt.h assumes string.h defines wchar_t
\r
8 #include <_PDCLIB_io.h>
\r
9 #include <_PDCLIB_locale.h>
\r
10 #include <_PDCLIB_clocale.h>
\r
12 static char ** argvToAnsi( wchar_t ** wargv, int argc )
\r
14 char ** argv = malloc( sizeof( *argv ) * argc );
\r
15 for ( int i = 0; i != argc; ++i ) {
\r
16 int sz = WideCharToMultiByte( CP_ACP, WC_COMPOSITECHECK | WC_SEPCHARS,
\r
17 wargv[i], -1, NULL, 0, NULL, NULL );
\r
18 if(!(argv[i] = malloc(sz))) {
\r
19 fputs("Error in C runtime initialization: "
\r
20 "unable to allocate buffer for argument", stderr);
\r
24 int rv = WideCharToMultiByte( CP_ACP, WC_COMPOSITECHECK | WC_SEPCHARS,
\r
25 wargv[i], -1, argv[i], sz, NULL, NULL );
\r
28 fputs("Error in C runtime initialization: "
\r
29 "size mismatch during character set conversion", stderr);
\r
37 static wchar_t * cl;
\r
38 static wchar_t ** wargv;
\r
39 static char ** argv;
\r
41 static void freeArgs( void )
\r
43 for(int i = 0; i != argc; i++) {
\r
50 extern void (*_PDCLIB_sigfpe)( int );
\r
51 extern void (*_PDCLIB_sigill)( int );
\r
52 extern void (*_PDCLIB_sigsegv)( int );
\r
54 static LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
\r
55 static LONG CALLBACK sehExceptionFilter( EXCEPTION_POINTERS * exInfo )
\r
58 void (*handler)( int );
\r
60 switch( exInfo->ExceptionRecord->ExceptionCode ) {
\r
61 case EXCEPTION_ACCESS_VIOLATION:
\r
62 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
\r
63 case EXCEPTION_DATATYPE_MISALIGNMENT:
\r
64 case EXCEPTION_GUARD_PAGE:
\r
65 case EXCEPTION_IN_PAGE_ERROR:
\r
66 case EXCEPTION_STACK_OVERFLOW:
\r
68 handler = _PDCLIB_sigsegv;
\r
71 case EXCEPTION_ILLEGAL_INSTRUCTION:
\r
72 case EXCEPTION_PRIV_INSTRUCTION:
\r
74 handler = _PDCLIB_sigill;
\r
77 case EXCEPTION_INT_DIVIDE_BY_ZERO:
\r
78 case EXCEPTION_INT_OVERFLOW:
\r
79 case EXCEPTION_FLT_DENORMAL_OPERAND:
\r
80 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
\r
81 case EXCEPTION_FLT_INEXACT_RESULT:
\r
82 case EXCEPTION_FLT_INVALID_OPERATION:
\r
83 case EXCEPTION_FLT_OVERFLOW:
\r
84 case EXCEPTION_FLT_STACK_CHECK:
\r
85 case EXCEPTION_FLT_UNDERFLOW:
\r
87 handler = _PDCLIB_sigfpe;
\r
91 return oldFilter ? oldFilter( exInfo ) : EXCEPTION_EXECUTE_HANDLER;
\r
94 if(handler == SIG_DFL) {
\r
95 return EXCEPTION_EXECUTE_HANDLER;
\r
96 } else if(handler == SIG_IGN) {
\r
97 return EXCEPTION_CONTINUE_EXECUTION;
\r
100 return EXCEPTION_CONTINUE_EXECUTION;
\r
104 extern int main( int argc, char ** argv, char ** envp );
\r
106 void __cdecl mainCRTStartup( void );
\r
108 void __cdecl mainCRTStartup( void )
\r
110 stdin->handle.pointer = GetStdHandle(STD_INPUT_HANDLE);
\r
111 stdout->handle.pointer = GetStdHandle(STD_OUTPUT_HANDLE);
\r
112 stderr->handle.pointer = GetStdHandle(STD_ERROR_HANDLE);
\r
114 oldFilter = SetUnhandledExceptionFilter( sehExceptionFilter );
\r
116 cl = GetCommandLineW();
\r
117 wargv = CommandLineToArgvW(cl, &argc);
\r
118 argv = argvToAnsi(wargv, argc);
\r
120 _PDCLIB_initclocale( &_PDCLIB_global_locale );
\r
122 if(tss_create(&_PDCLIB_locale_tss, (tss_dtor_t) freelocale)
\r
124 fputs( "Error during C runtime initialization: "
\r
125 "Unable to allocate locale TLS", stderr );
\r
126 exit( EXIT_FAILURE );
\r
129 if( mtx_init(&stdin->lock, mtx_recursive) != thrd_success
\r
130 || mtx_init(&stdout->lock, mtx_recursive) != thrd_success
\r
131 || mtx_init(&stderr->lock, mtx_recursive) != thrd_success ) {
\r
132 fputs( "Error during C runtime initialization: "
\r
133 "Unable to allocate stdio mutex", stderr );
\r
134 exit( EXIT_FAILURE );
\r
139 int exitStatus = main(argc, argv, NULL);
\r