From cd6a80d8c9d19034707197126fd193a87a57095b Mon Sep 17 00:00:00 2001 From: Daniel Reinhold Date: Thu, 24 Oct 2002 20:05:09 +0000 Subject: [PATCH] several additions to libroot: added abs.c (abs, labs) added div.c (div, ldiv) added abort() to exit.c git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1636 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/Jamfile | 8 +++- src/kernel/libroot/posix/stdlib/Jamfile | 2 + src/kernel/libroot/posix/stdlib/abs.c | 36 +++++++++++++++ src/kernel/libroot/posix/stdlib/div.c | 58 +++++++++++++++++++++++++ src/kernel/libroot/posix/stdlib/exit.c | 42 +++++++++++++----- 5 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 src/kernel/libroot/posix/stdlib/abs.c create mode 100644 src/kernel/libroot/posix/stdlib/div.c diff --git a/src/kernel/Jamfile b/src/kernel/Jamfile index 97cae58d52..dfbdc83e5a 100644 --- a/src/kernel/Jamfile +++ b/src/kernel/Jamfile @@ -253,9 +253,11 @@ KernelStaticLibraryObjects libc.a : <$(SOURCE_GRIST)!libroot!posix!stdio>wbuf.o <$(SOURCE_GRIST)!libroot!posix!stdio>wsetup.o - <$(SOURCE_GRIST)!libroot!posix!stdlib>assert.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>abs.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>assert.o <$(SOURCE_GRIST)!libroot!posix!stdlib>atoi.o <$(SOURCE_GRIST)!libroot!posix!stdlib>bsearch.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>div.o <$(SOURCE_GRIST)!libroot!posix!stdlib>env.o <$(SOURCE_GRIST)!libroot!posix!stdlib>exit.o <$(SOURCE_GRIST)!libroot!posix!stdlib>heapsort.o @@ -398,9 +400,11 @@ KernelLd libroot.so : <$(SOURCE_GRIST)!libroot!posix!stdio>wbuf.o <$(SOURCE_GRIST)!libroot!posix!stdio>wsetup.o - <$(SOURCE_GRIST)!libroot!posix!stdlib>assert.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>abs.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>assert.o <$(SOURCE_GRIST)!libroot!posix!stdlib>atoi.o <$(SOURCE_GRIST)!libroot!posix!stdlib>bsearch.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>div.o <$(SOURCE_GRIST)!libroot!posix!stdlib>env.o <$(SOURCE_GRIST)!libroot!posix!stdlib>exit.o <$(SOURCE_GRIST)!libroot!posix!stdlib>heapsort.o diff --git a/src/kernel/libroot/posix/stdlib/Jamfile b/src/kernel/libroot/posix/stdlib/Jamfile index 6f5414eabb..77a6b4e216 100644 --- a/src/kernel/libroot/posix/stdlib/Jamfile +++ b/src/kernel/libroot/posix/stdlib/Jamfile @@ -1,9 +1,11 @@ SubDir OBOS_TOP src kernel libroot posix stdlib ; KernelObjects + <$(SOURCE_GRIST)>abs.c <$(SOURCE_GRIST)>assert.c <$(SOURCE_GRIST)>atoi.c <$(SOURCE_GRIST)>bsearch.c + <$(SOURCE_GRIST)>div.c <$(SOURCE_GRIST)>env.c <$(SOURCE_GRIST)>exit.c <$(SOURCE_GRIST)>heapsort.c diff --git a/src/kernel/libroot/posix/stdlib/abs.c b/src/kernel/libroot/posix/stdlib/abs.c new file mode 100644 index 0000000000..7c60ed8491 --- /dev/null +++ b/src/kernel/libroot/posix/stdlib/abs.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2002, OpenBeOS Project. + * All rights reserved. + * Distributed under the terms of the OpenBeOS license. + * + * + * abs.c: + * implements the standard C library functions: + * abs, labs + * + * + * Author(s): + * Daniel Reinhold (danielre@users.sf.net) + * + */ + + +// ToDo: these are supposed to be declared in +// - - - - - - - - - - - - - - - +int abs(int); +long labs(long); +// - - - - - - - - - - - - - - - + + +int +abs(int i) +{ + return ((i < 0) ? -i : i); +} + + +long +labs(long i) +{ + return ((i < 0) ? -i : i); +} \ No newline at end of file diff --git a/src/kernel/libroot/posix/stdlib/div.c b/src/kernel/libroot/posix/stdlib/div.c new file mode 100644 index 0000000000..bbd73d6f91 --- /dev/null +++ b/src/kernel/libroot/posix/stdlib/div.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2002, OpenBeOS Project. + * All rights reserved. + * Distributed under the terms of the OpenBeOS license. + * + * + * div.c: + * implements the standard C library functions: + * div, ldiv + * + * + * Author(s): + * Daniel Reinhold (danielre@users.sf.net) + * + */ + + +// ToDo: these are supposed to be declared in +// - - - - - - - - - - - - - - - +#include +div_t div(int, int); +ldiv_t ldiv(long, long); +// - - - - - - - - - - - - - - - + + +div_t +div(int numerator, int denominator) +{ + div_t val; + + val.quot = numerator / denominator; + val.rem = numerator - denominator * val.quot; + + if ((val.rem > 0) && (val.quot < 0)) { + val.rem -= denominator; + ++val.quot; + } + + return val; +} + + +ldiv_t +ldiv(long numerator, long denominator) +{ + ldiv_t val; + + val.quot = numerator / denominator; + val.rem = numerator - denominator * val.quot; + + if ((val.rem > 0) && (val.quot < 0)) { + val.rem -= denominator; + ++val.quot; + } + + return val; +} + diff --git a/src/kernel/libroot/posix/stdlib/exit.c b/src/kernel/libroot/posix/stdlib/exit.c index 7a5b077c5a..e5ca94d4cd 100644 --- a/src/kernel/libroot/posix/stdlib/exit.c +++ b/src/kernel/libroot/posix/stdlib/exit.c @@ -1,39 +1,59 @@ /* - * Copyright (c) 2002, OpenBeOS Project - * - * This file is part of the OpenBeOS system interface. - * It is distributed under the terms of the OpenBeOS license. + * Copyright (c) 2002, OpenBeOS Project. + * All rights reserved. + * Distributed under the terms of the OpenBeOS license. * * - * Author(s): - * Daniel Reinhold (danielre@users.sf.net) + * exit.c: + * implements the standard C library functions: + * abort, atexit, exit + * + * + * Author(s): + * Daniel Reinhold (danielre@users.sf.net) * */ #include #include #include +#include +#include // ToDo: move this puppy to a more standard location #include "../stdio/local.h" -// ToDo: this is supposed to be declared in +// ToDo: these are supposed to be declared in +// - - - - - - - - - - - - - - - +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 +void abort(void); int atexit(void (*func)(void)); +// - - - - - - - - - - - - - - - -#define _EXIT_STACK_SIZE_ 32 - -static void (*_Exit_Stack[_EXIT_STACK_SIZE_])(void) = {0}; +static void (*_Exit_Stack[ATEXIT_MAX])(void) = {0}; static int _Exit_SP = 0; + +void +abort() +{ + // ToDo: uncomment the raise() call once it is implemented + + //raise(SIGABRT); + exit(EXIT_FAILURE); +} + + int atexit(void (*func)(void)) { // push the function pointer onto the exit stack - if (_Exit_SP < _EXIT_STACK_SIZE_) { + if (_Exit_SP < ATEXIT_MAX) { _Exit_Stack[_Exit_SP++] = func; return 0; }