home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / GCC / MAIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.3 KB  |  58 lines

  1. /* main.c (emx+gcc) -- Copyright (c) 1992-1993 by Eberhard Mattes */
  2. /*                     Copyright (c) 1992-1993 by Kai Uwe Rommel */
  3.  
  4. #include <stdlib.h>
  5.  
  6. extern int *_ctor_ptr;
  7. extern int *_dtor_ptr;
  8.  
  9. #if defined (__MT__)
  10. #define MAX_MODULES     64
  11. #else
  12. #define MAX_MODULES     1
  13. #endif
  14.  
  15. static int *dtor_tab[MAX_MODULES];
  16. static int dtor_count = 0;
  17.  
  18. static void call_tors (int *ptr)
  19. {
  20.   int n;
  21.   void (**ppf)();
  22.  
  23.   if (*ptr == -2)                 /* emxomf */
  24.     for (ppf = (void (**)()) (ptr + 1); *ppf != NULL; ++ppf)
  25.       (**ppf)();
  26.   else
  27.     {
  28.       if (*ptr == -1) --ptr;      /* Fix GNU ld bug */
  29.       n = *ptr++;                 /* Get size of vector */
  30.       if (*ptr == -1)             /* First element must be -1, see crt0.s */
  31.         for (ppf = (void (**)()) (ptr + 1); n > 1; ++ppf, --n)
  32.           (**ppf)();
  33.     }
  34. }
  35.  
  36.  
  37. static void call_dtors (void)
  38. {
  39.   while (dtor_count > 0)
  40.     {
  41.       --dtor_count;
  42.       call_tors (dtor_tab[dtor_count]);
  43.     }
  44. }
  45.  
  46.  
  47. void __main (void)
  48. {
  49.   int *tmp;
  50.  
  51.   tmp = _dtor_ptr;              /* Maybe a ctor calls DosLoadModule... */
  52.   call_tors (_ctor_ptr);
  53.   if (dtor_count == 0)
  54.     atexit (call_dtors);
  55.   if (dtor_count < MAX_MODULES)
  56.     dtor_tab[dtor_count++] = tmp;
  57. }
  58.