migrate substitution keywords to SVN
[reactos.git] / reactos / 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: 1.5 $
23 * $Author$
24 * $Date$
25 *
26 */
27
28 #include "precomp.h"
29 #include <msvcrt/stdlib.h>
30 #include <msvcrt/stdio.h>
31 #include <msvcrt/io.h>
32 #include <msvcrt/fcntl.h>
33 #include <msvcrt/process.h>
34 #include <msvcrt/float.h>
35
36
37 /* NOTE: The code for initializing the _argv, _argc, and environ variables
38 * has been moved to a separate .c file which is included in both
39 * crt1.c and dllcrt1.c. This means changes in the code don't have to
40 * be manually synchronized, but it does lead to this not-generally-
41 * a-good-idea use of include. */
42 #include "init.c"
43
44 extern int main(int, char**, char**);
45
46 /*
47 * Setup the default file handles to have the _CRT_fmode mode, as well as
48 * any new files created by the user.
49 */
50 extern unsigned int _CRT_fmode;
51
52 void
53 _mingw32_init_fmode (void)
54 {
55 /* Don't set the file mode if the user hasn't set any value for it. */
56 if (_CRT_fmode)
57 {
58 _fmode = _CRT_fmode;
59
60 /*
61 * This overrides the default file mode settings for stdin,
62 * stdout and stderr. At first I thought you would have to
63 * test with isatty, but it seems that the DOS console at
64 * least is smart enough to handle _O_BINARY stdout and
65 * still display correctly.
66 */
67 if (stdin)
68 {
69 _setmode (_fileno(stdin), _CRT_fmode);
70 }
71 if (stdout)
72 {
73 _setmode (_fileno(stdout), _CRT_fmode);
74 }
75 if (stderr)
76 {
77 _setmode (_fileno(stderr), _CRT_fmode);
78 }
79 }
80 }
81
82
83 /*
84 * The function mainCRTStartup is the entry point for all console programs.
85 */
86 int
87 mainCRTStartup (void)
88 {
89 int nRet;
90
91 /*
92 * I have been told that this is the correct thing to do. You
93 * have to uncomment the prototype of SetUnhandledExceptionFilter
94 * in the GNU Win32 API headers for this to work. The type it
95 * expects is a pointer to a function of the same type as
96 * UnhandledExceptionFilter, which is prototyped just above
97 * (see Functions.h).
98 */
99 //SetUnhandledExceptionFilter (NULL);
100
101 /*
102 * Initialize floating point unit.
103 */
104 _fpreset (); /* Supplied by the runtime library. */
105
106 /*
107 * Set up __argc, __argv and _environ.
108 */
109 _mingw32_init_mainargs();
110
111 /*
112 * Sets the default file mode for stdin, stdout and stderr, as well
113 * as files later opened by the user, to _CRT_fmode.
114 * NOTE: DLLs don't do this because that would be rude!
115 */
116 _mingw32_init_fmode();
117
118 /*
119 * Call the main function. If the user does not supply one
120 * the one in the 'libmingw32.a' library will be linked in, and
121 * that one calls WinMain. See main.c in the 'lib' dir
122 * for more details.
123 */
124 nRet = main(_argc, _argv, _environ);
125
126 /*
127 * Perform exit processing for the C library. This means
128 * flushing output and calling 'atexit' registered functions.
129 */
130 _cexit();
131
132 ExitProcess (nRet);
133
134 return 0;
135 }
136
137 /*
138 * For now the GUI startup function is the same as the console one.
139 * This simply gets rid of the annoying warning about not being able
140 * to find WinMainCRTStartup when linking GUI applications.
141 */
142 int
143 WinMainCRTStartup (void)
144 {
145 return mainCRTStartup();
146 }
147
148 /* With the EGCS build from Mumit Khan (or apparently b19 from Cygnus) this
149 * is no longer necessary. */
150 #ifdef __GNUC__
151 /*
152 * This section terminates the list of imports under GCC. If you do not
153 * include this then you will have problems when linking with DLLs.
154 *
155 */
156 asm (".section .idata$3\n" ".long 0,0,0,0,0,0,0,0");
157 #endif
158