Hopefully, the definitive spec files commit. Dedicated to Samuel SerapiĆ³n, who report...
[reactos.git] / reactos / lib / 3rdparty / mingw / gccmain.c
1 /*
2 * gccmain.c
3 * This file has no copyright assigned and is placed in the Public Domain.
4 * This file is a part of the mingw-runtime package.
5 * No warranty is given; refer to the file DISCLAIMER within the package.
6 *
7 * A separate version of __main, __do_global_ctors and __do_global_dtors for
8 * Mingw32 for use with Cygwin32 b19. Hopefully this object file will only
9 * be linked if the libgcc.a doesn't include __main, __do_global_dtors and
10 * __do_global_ctors.
11 *
12 */
13
14 /* Needed for the atexit prototype. */
15 #include <stdlib.h>
16 #include <stddef.h>
17
18 typedef void (*func_ptr) (void);
19 extern func_ptr __CTOR_LIST__[];
20 extern func_ptr __DTOR_LIST__[];
21
22 void
23 __do_global_dtors (void)
24 {
25 static func_ptr *p = __DTOR_LIST__ + 1;
26
27 /*
28 * Call each destructor in the destructor list until a null pointer
29 * is encountered.
30 */
31 while (*p)
32 {
33 (*(p)) ();
34 p++;
35 }
36 }
37
38 void
39 __do_global_ctors (void)
40 {
41 unsigned long nptrs = (unsigned long) (ptrdiff_t) __CTOR_LIST__[0];
42 unsigned long i;
43
44 /*
45 * If the first entry in the constructor list is -1 then the list
46 * is terminated with a null entry. Otherwise the first entry was
47 * the number of pointers in the list.
48 */
49 if (nptrs == -1)
50 {
51 for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++);
52 }
53
54 /*
55 * Go through the list backwards calling constructors.
56 */
57 for (i = nptrs; i >= 1; i--)
58 {
59 __CTOR_LIST__[i] ();
60 }
61
62 /*
63 * Register the destructors for processing on exit.
64 */
65 atexit (__do_global_dtors);
66 }
67
68 static int initialized = 0;
69
70 void
71 __main (void)
72 {
73 if (!initialized)
74 {
75 initialized = 1;
76 __do_global_ctors ();
77 }
78 }
79
80