Change stdio to use atexit() rather than the internal __clean variable; this
authorDavid Given <dg@cowlark.com>
Sat, 23 Jun 2018 16:35:45 +0000 (18:35 +0200)
committerDavid Given <dg@cowlark.com>
Sat, 23 Jun 2018 16:35:45 +0000 (18:35 +0200)
breaks the dependency between exit/atexit and stdio. Buffers are no longer
flushed on abort() (because it's pretty risky). Move the relevant functions
into sys/core.

13 files changed:
lang/cem/libcc.ansi/build.lua
lang/cem/libcc.ansi/core/misc/abort.c [moved from lang/cem/libcc.ansi/stdlib/abort.c with 70% similarity]
lang/cem/libcc.ansi/core/misc/raise.c [moved from lang/cem/libcc.ansi/signal/raise.c with 85% similarity]
lang/cem/libcc.ansi/headers/ack/config.h
lang/cem/libcc.ansi/signal/LIST [deleted file]
lang/cem/libcc.ansi/signal/Makefile [deleted file]
lang/cem/libcc.ansi/stdio/fflush.c
lang/cem/libcc.ansi/stdio/flushbuf.c
lang/cem/libcc.ansi/stdio/loc_incl.h
lang/cem/libcc.ansi/stdio/setvbuf.c
lang/cem/libcc.ansi/sys/exit/atexit.c [moved from lang/cem/libcc.ansi/stdlib/atexit.c with 64% similarity]
lang/cem/libcc.ansi/sys/exit/atexits.h [new file with mode: 0644]
lang/cem/libcc.ansi/sys/exit/exit.c [moved from lang/cem/libcc.ansi/stdlib/exit.c with 62% similarity]

index 38fba6b..3427826 100644 (file)
@@ -44,7 +44,7 @@ for _, plat in ipairs(vars.plats) do
                        "./core/ctype/*.c",
                        "./core/misc/*.c",
                        "./sys/malloc/*.c",
-                       "./signal/*.c",
+                       "./sys/exit/*.c",
                        "./assert/*.c",
                        "./stdio/*.c",
                        "./stdlib/*.c",
similarity index 70%
rename from lang/cem/libcc.ansi/stdlib/abort.c
rename to lang/cem/libcc.ansi/core/misc/abort.c
index afad36b..d596241 100644 (file)
@@ -4,17 +4,11 @@
  */
 /* $Id$ */
 
-#if defined(_POSIX_SOURCE)
+#include <stdlib.h>
 #include <sys/types.h>
-#endif
 #include <signal.h>
-#include <stdlib.h>
-
-extern void (*_clean)(void);
 
 void abort(void)
 {
-       if (_clean)
-               _clean(); /* flush all output files */
        raise(SIGABRT);
 }
similarity index 85%
rename from lang/cem/libcc.ansi/signal/raise.c
rename to lang/cem/libcc.ansi/core/misc/raise.c
index e1aedaa..e501931 100644 (file)
@@ -4,14 +4,17 @@
  */
 /* $Id$ */
 
-#if defined(_POSIX_SOURCE)
+#include <stdlib.h>
 #include <sys/types.h>
-#endif
 #include <signal.h>
 
+#if ACKCONF_WANT_EMULATED_RAISE
+
 int raise(int sig)
 {
        if (sig < 0 || sig > _NSIG)
                return -1;
        return kill(getpid(), sig);
 }
+
+#endif
index ae397db..593767d 100644 (file)
 #define ACKCONF_WANT_TERMIOS 0
 #endif
 
+#ifndef ACKCONF_WANT_EMULATED_RAISE
+/* Implement raise() in terms of kill() and getpid(). */
+#define ACKCONF_WANT_EMULATED_RAISE 1
+#endif
+
+#ifndef ACKCONF_WANT_MALLOC
+/* Uses sbrk() to get memory from the system. */
+#define ACKCONF_WANT_MALLOC 1
+#endif
+
 #endif
diff --git a/lang/cem/libcc.ansi/signal/LIST b/lang/cem/libcc.ansi/signal/LIST
deleted file mode 100644 (file)
index ff77f12..0000000
+++ /dev/null
@@ -1 +0,0 @@
-raise.c
diff --git a/lang/cem/libcc.ansi/signal/Makefile b/lang/cem/libcc.ansi/signal/Makefile
deleted file mode 100644 (file)
index c9578ae..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-clean:
-       rm -f raise.o OLIST
index f5ef6c3..24baa6e 100644 (file)
@@ -68,7 +68,7 @@ int fflush(FILE* stream)
        return EOF;
 }
 
-void __cleanup(void)
+static void cleanup(void)
 {
        register int i;
 
@@ -76,3 +76,13 @@ void __cleanup(void)
                if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
                        (void)fflush(__iotab[i]);
 }
+
+void __register_stdio_cleanup(void)
+{
+       static char registered = 0;
+       if (!registered)
+       {
+               registered = 1;
+               atexit(cleanup);
+       }
+}
index 9518de7..976d82a 100644 (file)
@@ -8,8 +8,6 @@
 #include <unistd.h>
 #include "loc_incl.h"
 
-extern void (*_clean)(void);
-
 static int
 do_write(int d, char* buf, int nbytes)
 {
@@ -28,7 +26,7 @@ do_write(int d, char* buf, int nbytes)
 
 int __flushbuf(int c, FILE* stream)
 {
-       _clean = __cleanup;
+       __register_stdio_cleanup();
        if (fileno(stream) < 0)
                return EOF;
        if (!io_testflag(stream, _IOWRITE))
index 52488e0..b30d327 100644 (file)
@@ -14,7 +14,8 @@ int _doprnt(const char *format, va_list ap, FILE *stream);
 int _doscan(FILE * stream, const char *format, va_list ap);
 char *_i_compute(unsigned long val, int base, char *s, int nrdigits);
 char *_f_print(va_list *ap, int flags, char *s, char c, int precision);
-void __cleanup(void);
+
+extern void __register_stdio_cleanup(void);
 
 FILE *popen(const char *command, const char *type);
 FILE *fdopen(int fd, const char *mode);
index b7571cc..4650db0 100644 (file)
@@ -13,7 +13,7 @@ int setvbuf(register FILE* stream, char* buf, int mode, size_t size)
 {
        int retval = 0;
 
-       _clean = __cleanup;
+       __register_stdio_cleanup();
        if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
                return EOF;
 
similarity index 64%
rename from lang/cem/libcc.ansi/stdlib/atexit.c
rename to lang/cem/libcc.ansi/sys/exit/atexit.c
index c9dc796..2cb16da 100644 (file)
@@ -1,11 +1,7 @@
 /* $Id$ */
 
 #include <stdlib.h>
-
-#define NEXITS 32
-
-extern void (*__functab[NEXITS])(void);
-extern int __funccnt;
+#include "atexits.h"
 
 int atexit(void (*func)(void))
 {
diff --git a/lang/cem/libcc.ansi/sys/exit/atexits.h b/lang/cem/libcc.ansi/sys/exit/atexits.h
new file mode 100644 (file)
index 0000000..3cca599
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef ATEXITS_H
+#define ATEXITS_H
+
+#define NEXITS 32
+
+extern void (*__functab[NEXITS])(void);
+extern int __funccnt;
+
+#endif
similarity index 62%
rename from lang/cem/libcc.ansi/stdlib/exit.c
rename to lang/cem/libcc.ansi/sys/exit/exit.c
index ef8976b..7f86132 100644 (file)
@@ -7,29 +7,16 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
-
-#define NEXITS 32
+#include "atexits.h"
 
 void (*__functab[NEXITS])(void);
 int __funccnt = 0;
 
-/* only flush output buffers when necessary */
-int (*_clean)(void) = NULL;
-
-static void
-_calls(void)
+void exit(int status)
 {
-       register int i = __funccnt;
-
        /* "Called in reversed order of their registration" */
-       while (--i >= 0)
-               (*__functab[i])();
-}
+       while (__funccnt >= 0)
+               (*__functab[__funccnt])();
 
-void exit(int status)
-{
-       _calls();
-       if (_clean)
-               _clean();
        _exit(status);
 }