Index: branches/libc-0.6/src/emx/src/lib/startup/cleanup.c
===================================================================
--- branches/libc-0.6/src/emx/src/lib/startup/cleanup.c	(revision 2782)
+++ 	(revision )
@@ -1,13 +1,0 @@
-/* cleanup.c (emx+gcc) */
-
-#include <stdio.h>
-#include <emx/startup.h>
-#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_INITTERM
-#include <InnoTekLIBC/logstrict.h>
-
-void _CRT_term(void)
-{
-    LIBCLOG_ENTER("\n");
-    __ctordtorTerm1(&__crtexit1__);
-    LIBCLOG_RETURN_VOID();
-}
Index: branches/libc-0.6/src/emx/src/lib/startup/startup.c
===================================================================
--- branches/libc-0.6/src/emx/src/lib/startup/startup.c	(revision 2782)
+++ branches/libc-0.6/src/emx/src/lib/startup/startup.c	(revision 2784)
@@ -1,11 +1,36 @@
-/* startup.c (emx+gcc) -- Copyright (c) 1990-1998 by Eberhard Mattes */
+/** $Id: $ */
+/** @file
+ *
+ * kLIBC - CRT init and termination code.
+ *
+ * Copyright (c) 1990-1998 by Eberhard Mattes
+ * Copyright (c) 2004-2006 knut st. osmundsen <bird@innotek.de>
+ *
+ *
+ * This file is part of kLIBC.
+ *
+ * kLIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * kLIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with kLIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
+
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
 #include <errno.h>
-#include <sys/ioctl.h>
-#include <emx/io.h>
-#include <emx/syscalls.h>
+#include <sys/builtin.h>
 #include <emx/startup.h>
 #define __LIBC_LOG_GROUP    __LIBC_LOG_GRP_INITTERM
@@ -13,30 +38,71 @@
 
 
-/* Initialize the C run-time library.  This function is called from
-   crt0.s. */
-int _CRT_init (void)
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+/** The balance between _CRT_init and _CRT_term calls. */
+static volatile int32_t gcCRTReferences = 0;
+
+
+/**
+ * Initializes the C runtime library.
+ *
+ * A _CRT_init() call should be matched by a _CRT_term() call as we are
+ * keeping a count of the number of C runtime users.
+ *
+ * This function is normally called from _DLL_InitTerm and crt0.s.
+ *
+ * @returns 0 on success. -1 on failure.
+ */
+int _CRT_init(void)
 {
-  LIBCLOG_ENTER("\n");
-  static int startup_flag;
+    LIBCLOG_ENTER("\n");
 
-  /* Protect against multiple calls (for instance, by _CRT_init() in a
-     _DLL_InitTerm() and by the application program's startup code). */
-  if (startup_flag)
+    /*
+     * On initialize once.
+     */
+    int32_t cRefs = __atomic_increment_s32(&gcCRTReferences);
+    if (cRefs != 1)
+        LIBCLOG_RETURN_MSG(0, "ret 0 (cRefs=%d)\n", cRefs);
+
+    /*
+     * Call the weak initializers.
+     */
+    __ctordtorInit1(&__crtinit1__);
+
+    /*
+     * ANSI X3.159-1989, 4.1.3: "The value of errno is zero at program startup..."
+     *
+     * The above code usually sets errno to EBADF, therefore we reset errno
+     * to zero before calling main().
+     */
+    errno = 0;
     LIBCLOG_RETURN_INT(0);
-  startup_flag = 1;
-
-  /* Initialize streams etc. if required. */
-  __ctordtorInit1 (&__crtinit1__);
-
-  /* ANSI X3.159-1989, 4.1.3: "The value of errno is zero at program
-     startup..."
-
-     The above code usually sets errno to EBADF, therefore we reset
-     errno to zero before calling main(). */
-  errno = 0;
-
-  LIBCLOG_RETURN_INT(0);
 }
 
+
+/**
+ * Terminates the C runtime library.
+ *
+ * A _CRT_term() call must be paired with a _CRT_init() call as we're
+ * counting the number of C runtime users.
+ *
+ * This function is normally called from _DLL_InitTerm and crt0.s.
+ */
+void _CRT_term(void)
+{
+    LIBCLOG_ENTER("\n");
+    int32_t cRefs = __atomic_decrement_s32(&gcCRTReferences);
+    if (cRefs == 0)
+    {
+        /*
+         * Call the weak terminators.
+         */
+        __ctordtorTerm1(&__crtexit1__);
+    }
+    else if (cRefs < 0)
+        LIBCLOG_ERROR_RETURN_MSG_VOID("cRefs=%d\n", cRefs);
+    LIBCLOG_RETURN_VOID();
+}
 
 
@@ -118,2 +184,3 @@
  * @todo update this with fork and performance changes!
  */
+
