diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-08-11 19:51:32 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-08-11 19:51:32 -0400 |
commit | d6127ae58ff3e823773a158a1e79d5999fa7f995 (patch) | |
tree | d530dfc9921d566919acf770c4b1ceb0a401a6bf /src/stdlib | |
parent | 5d9519b799ac6fb1e2d5bdb3daaf123c1326c0f1 (diff) |
move stdlib internals to _stdlib.h and struct __stdlib
Diffstat (limited to 'src/stdlib')
-rw-r--r-- | src/stdlib/__stdlib.c | 3 | ||||
-rw-r--r-- | src/stdlib/_rand.h | 4 | ||||
-rw-r--r-- | src/stdlib/_stdlib.h | 22 | ||||
-rw-r--r-- | src/stdlib/atexit.c | 4 | ||||
-rw-r--r-- | src/stdlib/exit.c | 3 | ||||
-rw-r--r-- | src/stdlib/rand.c | 5 | ||||
-rw-r--r-- | src/stdlib/srand.c | 4 |
7 files changed, 33 insertions, 12 deletions
diff --git a/src/stdlib/__stdlib.c b/src/stdlib/__stdlib.c new file mode 100644 index 00000000..bfc601d9 --- /dev/null +++ b/src/stdlib/__stdlib.c @@ -0,0 +1,3 @@ +#include "_stdlib.h" + +struct __stdlib __stdlib; diff --git a/src/stdlib/_rand.h b/src/stdlib/_rand.h deleted file mode 100644 index 7f7102ef..00000000 --- a/src/stdlib/_rand.h +++ /dev/null @@ -1,4 +0,0 @@ -#include <limits.h> - -#define _rand(_n) \ - (((_n) = (_n) * 1103515245 + 12345) ? (_n) / UINT_MAX % RAND_MAX : 0) diff --git a/src/stdlib/_stdlib.h b/src/stdlib/_stdlib.h new file mode 100644 index 00000000..b0aa3cd2 --- /dev/null +++ b/src/stdlib/_stdlib.h @@ -0,0 +1,22 @@ +#ifndef ___STDLIB_H__ +#define ___STDLIB_H__ + +#include <stdlib.h> +#include <limits.h> + +#define _rand(_n) \ + (((_n) = (_n) * 1103515245 + 12345) ? (_n) / UINT_MAX % RAND_MAX : 0) + +struct __stdlib { + struct atexit { + int nfns; + void (*fns[32])(void); + struct atexit *next; + struct atexit *prev; + } atexit; + unsigned int rand; +}; + +extern struct __stdlib __stdlib; + +#endif diff --git a/src/stdlib/atexit.c b/src/stdlib/atexit.c index aeb9172c..37f0ef72 100644 --- a/src/stdlib/atexit.c +++ b/src/stdlib/atexit.c @@ -1,12 +1,12 @@ #include <stdlib.h> #include "errno.h" -#include "nonstd/lib.h" +#include "_stdlib.h" /** register a function to run at program exit **/ int atexit(void (*func)(void)) { - struct atexit *ae = __libc(ATEXIT); + struct atexit *ae = &(__stdlib.atexit); while (ae->nfns == sizeof(ae->fns) / sizeof(ae->fns[0])) { if (ae->next == NULL) { ae->next = calloc(1, sizeof(*ae->next)); diff --git a/src/stdlib/exit.c b/src/stdlib/exit.c index 501131e4..28f87242 100644 --- a/src/stdlib/exit.c +++ b/src/stdlib/exit.c @@ -2,12 +2,13 @@ #include "limits.h" #include "stddef.h" #include "nonstd/syscall.h" +#include "_stdlib.h" /** cause normal program termination **/ _Noreturn void exit(int status) { long scno = __lookup("exit"); - struct atexit *ae = __libc(ATEXIT); + struct atexit *ae = &(__stdlib.atexit); /* execute all atexit() registered functions in reverse order */ while (ae) { diff --git a/src/stdlib/rand.c b/src/stdlib/rand.c index f1a3e3e1..d8f3fcff 100644 --- a/src/stdlib/rand.c +++ b/src/stdlib/rand.c @@ -1,11 +1,10 @@ #include <stdlib.h> -#include "nonstd/lib.h" -#include "_rand.h" +#include "_stdlib.h" /** get a pseudo-random number **/ int rand(void) { - return _rand(*(unsigned*)__libc(RAND)); + return (int)_rand(__stdlib.rand); } /*** diff --git a/src/stdlib/srand.c b/src/stdlib/srand.c index 97ed2fec..af61570b 100644 --- a/src/stdlib/srand.c +++ b/src/stdlib/srand.c @@ -1,11 +1,11 @@ #include <stdlib.h> -#include "nonstd/lib.h" +#include "_stdlib.h" /** seed the pseudo-random number generator **/ void srand(unsigned int seed) { - *((int*)__libc(RAND)) = seed; + __stdlib.rand = seed; } /*** |