/* main.c (emx+gcc) -- Copyright (c) 1992-1994 by Eberhard Mattes */
/*                     Copyright (c) 1992-1994 by Kai Uwe Rommel */

#include <stdlib.h>

extern void __ctordtorInit (void);
extern void __ctordtorTerm (void);

/* This module is always statically linked in order to get a
   statically linked __ctordtorInit() which uses the __CTOR_LIST__ and
   __DTOR_LIST__ of the executable. */

void __main (void)
{
  __ctordtorInit ();
  atexit (__ctordtorTerm);
}

#if defined (_EMXLIBC_DLL)

/* Keep old code in the DLL for compatibility with old executables. */

#define MAX_MODULES     64

extern void __ctordtorInit1 (int *ptr);
extern void __ctordtorTerm1 (int *ptr);
extern int *_ctor_ptr;
extern int *_dtor_ptr;

static int *dtor_tab[MAX_MODULES];
static int dtor_count = 0;

static void call_dtor_lists (void)
{
  while (dtor_count > 0)
    {
      --dtor_count;
      __ctordtorTerm1 (dtor_tab[dtor_count]);
    }
}

void __main_08f (void)
{
  int *tmp;

  tmp = _dtor_ptr;              /* Maybe a ctor calls DosLoadModule... */
  __ctordtorInit1 (_ctor_ptr);
  if (dtor_count == 0)
    atexit (call_dtor_lists);
  if (dtor_count < MAX_MODULES)
    dtor_tab[dtor_count++] = tmp;
}

void __main_08h (void)
{
  __ctordtorInit ();
  atexit (__ctordtorTerm);
}

#endif
