From c9a32c1a43d2bdded07eaa45732c3a6e195a5442 Mon Sep 17 00:00:00 2001 From: Nicholas Date: Sat, 20 Nov 2021 17:07:23 -0800 Subject: Chore: cleaned up the exit code to cleanly interface with libc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We use weak linking to ensure we clean up at exit time correctly. If libc is linked, then we call our cleanup function by registering an atexit callback with the library. If libc is not linked, we have a weak symbol that results in a noop. Similarly, if we call rt·exit while linked with libc, this immediately calls libc's exit (which will call our cleanup as we registered it). If we are not linked to libc, exit() is given as a weak link to a noop function. --- src/base/bufio/internal.h | 2 ++ src/base/fmt/test.c | 9 +++++++++ src/base/gz/seek.c | 2 +- src/base/io/interface.c | 2 +- src/base/io/seek.c | 4 ++-- src/rules.mk | 8 ++++---- 6 files changed, 19 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/base/bufio/internal.h b/src/base/bufio/internal.h index a1a006a..a59e787 100644 --- a/src/base/bufio/internal.h +++ b/src/base/bufio/internal.h @@ -1,2 +1,4 @@ #include #include + +#define header(io) ((io·Header*)(io)) diff --git a/src/base/fmt/test.c b/src/base/fmt/test.c index 73f5e7c..3f7f070 100644 --- a/src/base/fmt/test.c +++ b/src/base/fmt/test.c @@ -15,9 +15,18 @@ Xfmt(fmt·State *io) return fmt·write(io, "(real=%g,imag=%g)", c.r, c.i); } +static void +saygoodbye(void *arg) +{ + intptr n; + sys·write(1, 9, "goodbye\n", &n); +} + int main(int argc, char *argv[]) { + rt·atexit(saygoodbye, nil); + fmt·print("basic tests\n"); fmt·print("\tx: %x\n", 0x87654321); fmt·print("\tu: %u\n", 0x87654321); diff --git a/src/base/gz/seek.c b/src/base/gz/seek.c index 328886d..8230a79 100644 --- a/src/base/gz/seek.c +++ b/src/base/gz/seek.c @@ -1,7 +1,7 @@ #include "internal.h" int -gz·seek(gz·Stream *s, long off, enum SeekPos whence) +gz·seek(gz·Stream *s, long off, int whence) { return gzseek(s, off, whence); } diff --git a/src/base/io/interface.c b/src/base/io/interface.c index bc9d5ff..80469bf 100644 --- a/src/base/io/interface.c +++ b/src/base/io/interface.c @@ -44,7 +44,7 @@ int static int -·seek(void *skr, long off, enum SeekPos whence) +·seek(void *skr, long off, int whence) { return io·seek((io·Stream *)skr, off, whence); } diff --git a/src/base/io/seek.c b/src/base/io/seek.c index d0e7488..1be4ee7 100644 --- a/src/base/io/seek.c +++ b/src/base/io/seek.c @@ -1,7 +1,7 @@ #include "internal.h" int -io·seek(io·Stream *s, long off, enum SeekPos origin) +io·seek(io·Stream *s, long off, int whence) { - return fseek(s, off, origin); + return fseek(s, off, whence); } diff --git a/src/rules.mk b/src/rules.mk index 8e8594c..368479c 100644 --- a/src/rules.mk +++ b/src/rules.mk @@ -8,10 +8,10 @@ include $(DIR)/rules.mk DIR := $(d)/base include $(DIR)/rules.mk -# DIR := $(d)/libmath -# include $(DIR)/rules.mk +DIR := $(d)/libmath +include $(DIR)/rules.mk -# DIR := $(d)/libbio -# include $(DIR)/rules.mk +DIR := $(d)/libbio +include $(DIR)/rules.mk include share/pop.mk -- cgit v1.2.1