home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / src / mingw-runtime-19991107 / mingw / gccmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-30  |  1.6 KB  |  86 lines

  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.2 $
  15.  * $Author: khan $
  16.  * $Date: 1998/09/03 16:31:17 $
  17.  *
  18.  */
  19.  
  20. /* Needed for the atexit prototype. */
  21. #include <stdlib.h>
  22.  
  23. typedef void (*func_ptr) (void);
  24. extern func_ptr __CTOR_LIST__[];
  25. extern func_ptr __DTOR_LIST__[];
  26.  
  27. void
  28. __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
  44. __do_global_ctors (void)
  45. {
  46.   unsigned long nptrs = (unsigned long) __CTOR_LIST__[0];
  47.   unsigned i;
  48.  
  49.   /*
  50.    * If the first entry in the constructor list is -1 then the list
  51.    * is terminated with a null entry. Otherwise the first entry was
  52.    * the number of pointers in the list.
  53.    */
  54.   if (nptrs == -1)
  55.     {
  56.       for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
  57.     ;
  58.     }
  59.  
  60.   /* 
  61.    * Go through the list backwards calling constructors.
  62.    */
  63.   for (i = nptrs; i >= 1; i--)
  64.     {
  65.       __CTOR_LIST__[i] ();
  66.     }
  67.  
  68.   /*
  69.    * Register the destructors for processing on exit.
  70.    */
  71.   atexit (__do_global_dtors);
  72. }
  73.  
  74. static int initialized = 0;
  75.  
  76. void
  77. __main (void)
  78. {
  79.   if (!initialized)
  80.     {
  81.       initialized = 1;
  82.       __do_global_ctors ();
  83.     }
  84. }
  85.  
  86.