From 2ec07684d20c540a6edda1fa4e6062ab6a66df8d Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Sat, 10 Aug 2019 00:35:16 +0300 Subject: [PATCH] Get OS name from uname() This implies that OS names have changed. Unix-like OS names are now capitalized. "macos" is now "Darwin". Windows is now all-lowecase "windows". "win32" is gone. "win64" was not used in the original code despite a reference to it. --- c/env.h | 1 + c/env_unix.c | 17 +++++++++++++++++ c/env_windows.c | 1 + c/flisp.c | 17 +++-------------- scheme-core/system.scm | 4 +--- scripts/build.sh | 3 ++- 6 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 c/env.h create mode 100644 c/env_unix.c create mode 100644 c/env_windows.c diff --git a/c/env.h b/c/env.h new file mode 100644 index 0000000..1608e04 --- /dev/null +++ b/c/env.h @@ -0,0 +1 @@ +const char *env_get_os_name(void); diff --git a/c/env_unix.c b/c/env_unix.c new file mode 100644 index 0000000..fa9e0f1 --- /dev/null +++ b/c/env_unix.c @@ -0,0 +1,17 @@ +#include + +#include + +static const struct utsname *get_global_uname(void) +{ + static struct utsname buf; + + if (!buf.sysname[0]) { + if (uname(&buf) == -1) { + memset(&buf, 0, sizeof(buf)); + } + } + return &buf; +} + +const char *env_get_os_name(void) { return get_global_uname()->sysname; } diff --git a/c/env_windows.c b/c/env_windows.c new file mode 100644 index 0000000..c31ac76 --- /dev/null +++ b/c/env_windows.c @@ -0,0 +1 @@ +const char *env_get_os_name(void) { return "windows"; } diff --git a/c/flisp.c b/c/flisp.c index f94e135..07bf019 100644 --- a/c/flisp.c +++ b/c/flisp.c @@ -63,8 +63,9 @@ #include "flisp.h" -#include "opcodes.h" #include "argcount.h" +#include "env.h" +#include "opcodes.h" static char *builtin_names[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, @@ -2622,19 +2623,7 @@ static void lisp_init(size_t initial_heapsize) setc(symbol("procedure?"), builtin(OP_FUNCTIONP)); setc(symbol("top-level-bound?"), builtin(OP_BOUNDP)); -#ifdef LINUX - set(symbol("*os-name*"), symbol("linux")); -#elif defined(_WIN32) - set(symbol("*os-name*"), symbol("win32")); -#elif defined(MACOSX) - set(symbol("*os-name*"), symbol("macos")); -#elif defined(OPENBSD) - set(symbol("*os-name*"), symbol("openbsd")); -#elif defined(FREEBSD) - set(symbol("*os-name*"), symbol("freebsd")); -#else - set(symbol("*os-name*"), symbol("unknown")); -#endif + set(symbol("*os-name*"), symbol(env_get_os_name())); the_empty_vector = tagptr(alloc_words(1), TAG_VECTOR); vector_setsize(the_empty_vector, 0); diff --git a/scheme-core/system.scm b/scheme-core/system.scm index 3da726e..d456283 100644 --- a/scheme-core/system.scm +++ b/scheme-core/system.scm @@ -1056,9 +1056,7 @@ ; initialize globals that need to be set at load time (define (__init_globals) - (if (or (eq? *os-name* 'win32) - (eq? *os-name* 'win64) - (eq? *os-name* 'windows)) + (if (eq? *os-name* 'windows) (begin (set! *directory-separator* "\\") (set! *linefeed* "\r\n")) (begin (set! *directory-separator* "/") diff --git a/scripts/build.sh b/scripts/build.sh index 1c82093..b12f00b 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -21,6 +21,7 @@ $CC $CFLAGS -c ../c/bitvector-ops.c $CC $CFLAGS -c ../c/bitvector.c $CC $CFLAGS -c ../c/builtins.c $CC $CFLAGS -c ../c/dump.c +$CC $CFLAGS -c ../c/env_unix.c $CC $CFLAGS -c ../c/equalhash.c $CC $CFLAGS -c ../c/flisp.c $CC $CFLAGS -c ../c/flmain.c @@ -40,7 +41,7 @@ $CC $CFLAGS -c ../c/table.c $CC $CFLAGS -c ../c/time_unix.c $CC $CFLAGS -c ../c/utf8.c $CC $LFLAGS -o flisp -lm \ - bitvector-ops.o bitvector.o builtins.o dump.o \ + bitvector-ops.o bitvector.o builtins.o dump.o env_unix.o \ equalhash.o flisp.o flmain.o fs_"$os".o fs_unix.o \ hashing.o htable.o int2str.o \ ios.o iostream.o lltinit.o ptrhash.o random.o socket.o \