:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / crtdll / misc / crt1.c
1 /*
2  * crt1.c
3  *
4  * Source code for the startup proceedures used by all programs. This code
5  * is compiled to make crt0.o, which should be located in the library path.
6  *
7  * This code is part of the Mingw32 package.
8  *
9  * Contributors:
10  *  Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
11  *
12  *  THIS SOFTWARE IS NOT COPYRIGHTED
13  *
14  *  This source code is offered for use in the public domain. You may
15  *  use, modify or distribute it freely.
16  *
17  *  This code is distributed in the hope that it will be useful but
18  *  WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY
19  *  DISCLAMED. This includes but is not limited to warrenties of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  *
22  * $Revision$
23  * $Author$
24  * $Date$
25  *
26  */
27
28 #include <crtdll/stdlib.h>
29 #include <crtdll/stdio.h>
30 #include <crtdll/io.h>
31 #include <crtdll/fcntl.h>
32 #include <crtdll/process.h>
33 #include <crtdll/float.h>
34 #include <windows.h>
35
36 /* NOTE: The code for initializing the _argv, _argc, and environ variables
37  *       has been moved to a separate .c file which is included in both
38  *       crt1.c and dllcrt1.c. This means changes in the code don't have to
39  *       be manually synchronized, but it does lead to this not-generally-
40  *       a-good-idea use of include. */
41 #include "init.c"
42
43 extern int main(int, char**, char**);
44
45 /*
46  * Setup the default file handles to have the _CRT_fmode mode, as well as
47  * any new files created by the user.
48  */
49 extern unsigned int _CRT_fmode;
50
51 void
52 _mingw32_init_fmode (void)
53 {
54         /* Don't set the file mode if the user hasn't set any value for it. */
55         if (_CRT_fmode)
56         {
57                 _fmode = _CRT_fmode;
58
59                 /*
60                  * This overrides the default file mode settings for stdin,
61                  * stdout and stderr. At first I thought you would have to
62                  * test with isatty, but it seems that the DOS console at
63                  * least is smart enough to handle _O_BINARY stdout and
64                  * still display correctly.
65                  */
66                 if (stdin)
67                 {
68                         _setmode (_fileno(stdin), _CRT_fmode);
69                 }
70                 if (stdout)
71                 {
72                         _setmode (_fileno(stdout), _CRT_fmode);
73                 }
74                 if (stderr)
75                 {
76                         _setmode (_fileno(stderr), _CRT_fmode);
77                 }
78         }
79 }
80
81
82 /*
83  * The function mainCRTStartup is the entry point for all console programs.
84  */
85 int
86 mainCRTStartup (void)
87 {
88         int     nRet;
89
90         /*
91          * I have been told that this is the correct thing to do. You
92          * have to uncomment the prototype of SetUnhandledExceptionFilter
93          * in the GNU Win32 API headers for this to work. The type it
94          * expects is a pointer to a function of the same type as
95          * UnhandledExceptionFilter, which is prototyped just above
96          * (see Functions.h).
97          */
98         //SetUnhandledExceptionFilter (NULL);
99
100         /*
101          * Initialize floating point unit.
102          */
103         _fpreset ();    /* Supplied by the runtime library. */
104
105         /*
106          * Set up __argc, __argv and _environ.
107          */
108         _mingw32_init_mainargs();
109
110         /*
111          * Sets the default file mode for stdin, stdout and stderr, as well
112          * as files later opened by the user, to _CRT_fmode.
113          * NOTE: DLLs don't do this because that would be rude!
114          */
115         _mingw32_init_fmode();
116
117         /*
118          * Call the main function. If the user does not supply one
119          * the one in the 'libmingw32.a' library will be linked in, and
120          * that one calls WinMain. See main.c in the 'lib' dir
121          * for more details.
122          */
123         nRet = main(_argc, _argv, _environ);
124
125         /*
126          * Perform exit processing for the C library. This means
127          * flushing output and calling 'atexit' registered functions.
128          */
129         _cexit();
130
131         ExitProcess (nRet);
132
133         return 0;
134 }
135
136 /*
137  * For now the GUI startup function is the same as the console one.
138  * This simply gets rid of the annoying warning about not being able
139  * to find WinMainCRTStartup when linking GUI applications.
140  */
141 int
142 WinMainCRTStartup (void)
143 {
144         return mainCRTStartup();
145 }
146
147 /* With the EGCS build from Mumit Khan (or apparently b19 from Cygnus) this
148  * is no longer necessary. */
149 #ifdef __GNUC__
150 /*
151  * This section terminates the list of imports under GCC. If you do not
152  * include this then you will have problems when linking with DLLs.
153  *
154  */
155 asm (".section .idata$3\n" ".long 0,0,0,0,0,0,0,0");
156 #endif
157