prepare move old cruft
[reactos.git] / reactos / lib / crtdll / misc / gccmain.c
1 /*
2 * gccmain.c
3 *
4 * A separate version of __main, __do_global_ctors and __do_global_dtors for
5 * Mingw32 for use with Cygwin32 b19. Hopefully this object file will only
6 * be linked if the libgcc.a doesn't include __main, __do_global_dtors and
7 * __do_global_ctors.
8 *
9 * This file is part of the Mingw32 package.
10 *
11 * Contributors:
12 * Code supplied by Stan Cox <scox@cygnus.com>
13 *
14 * $Revision: 1.3 $
15 * $Author$
16 * $Date$
17 *
18 */
19
20 /* Needed for the atexit prototype. */
21 #include <msvcrt/stdlib.h>
22
23
24 typedef void (*func_ptr) (void);
25 extern func_ptr __CTOR_LIST__[];
26 extern func_ptr __DTOR_LIST__[];
27
28 void __do_global_dtors(void)
29 {
30 static func_ptr* p = __DTOR_LIST__ + 1;
31
32 /*
33 * Call each destructor in the destructor list until a null pointer
34 * is encountered.
35 */
36 while (*p)
37 {
38 (*(p)) ();
39 p++;
40 }
41 }
42
43 void __do_global_ctors(void)
44 {
45 unsigned long nptrs = (unsigned long)__CTOR_LIST__[0];
46 unsigned i;
47
48 /*
49 * If the first entry in the constructor list is -1 then the list
50 * is terminated with a null entry. Otherwise the first entry was
51 * the number of pointers in the list.
52 */
53 if (nptrs == -1) {
54 for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
55 ;
56 }
57
58 /*
59 * Go through the list backwards calling constructors.
60 */
61 for (i = nptrs; i >= 1; i--) {
62 __CTOR_LIST__[i] ();
63 }
64
65 /*
66 * Register the destructors for processing on exit.
67 */
68 atexit(__do_global_dtors);
69 }
70
71 static int initialized = 0;
72
73 void __main(void)
74 {
75 if (!initialized) {
76 initialized = 1;
77 __do_global_ctors ();
78 }
79 }
80