home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / __main.c next >
C/C++ Source or Header  |  1991-04-08  |  1KB  |  69 lines

  1.  
  2. #include <sys/loader.h>
  3. #include <libc.h>
  4. #include <stdlib.h>
  5.  
  6. static void __do_global_init (void)
  7. {
  8.   struct mach_header **header = getmachheaders ();
  9.   unsigned int i;
  10.   
  11.   if (header == NULL)
  12.     return;
  13.   
  14.   for (i = 0; header[i] != NULL; i++)
  15.     {
  16.       const struct section *section;
  17.       void (**constructor) (void);
  18.       unsigned int n, j;
  19.       
  20.       section = getsectbynamefromheader (header[i],
  21.                      "__TEXT",
  22.                      "__constructor");
  23.       
  24.       if (section == NULL)
  25.         continue;
  26.       
  27.       constructor = (void (**) (void)) section->addr;
  28.       n = section->size / sizeof (long);
  29.       
  30.       for (j = 0; j < n; j++)
  31.     (*constructor[j]) ();
  32.     }
  33. }
  34.  
  35. static void __do_global_cleanup (void)
  36. {
  37.   struct mach_header **header = getmachheaders ();
  38.   unsigned int i;
  39.   
  40.   if (header == NULL)
  41.     return;
  42.   
  43.   for (i = 0; header[i] != NULL; i++)
  44.     {
  45.       const struct section *section;
  46.       void (**destructor) (void);
  47.       unsigned int n, j;
  48.       
  49.       section = getsectbynamefromheader (header[i],
  50.                      "__TEXT",
  51.                      "__destructor");
  52.       
  53.       if (section == NULL)
  54.         continue;
  55.       
  56.       destructor = (void (**) (void)) section->addr;
  57.       n = section->size / sizeof (long);
  58.       
  59.       for (j = 0; j < n; j++)
  60.     (*destructor[j]) ();
  61.     }
  62. }
  63.  
  64. void __main (void)
  65. {
  66.   atexit (__do_global_cleanup);
  67.   __do_global_init ();
  68. }
  69.