From 907dece88d929ffb8096019cb8ac09396524eb66 Mon Sep 17 00:00:00 2001 From: Ray Gardner Date: Sat, 10 Aug 2024 20:08:07 -0600 Subject: [PATCH] make srand() POSIX.2024 compliant POSIX now says srand() must set the rand() seed value to "seconds since the Epoch". Also, use 1 as default initial seed to agree with gawk and some other implementations. --- toys/pending/awk.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/toys/pending/awk.c b/toys/pending/awk.c index f41db24c..70806910 100644 --- a/toys/pending/awk.c +++ b/toys/pending/awk.c @@ -3549,13 +3549,6 @@ static void gsub(int opcode, int nargs, int parmbase) if (field_num >= 0) fixup_fields(field_num); } -static long millinow(void) -{ - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - return ts.tv_sec*1000+ts.tv_nsec/1000000; -} - // Initially set stackp_needmore at MIN_STACK_LEFT before limit. // When stackp > stackp_needmore, then expand and reset stackp_needmore static void add_stack(struct zvalue **stackp_needmore) @@ -4319,7 +4312,7 @@ static int interpx(int start, int *status) nargs = *ip++; if (nargs == 1) { STKP->num = seedrand(to_num(STKP)); - } else push_int_val(seedrand(millinow())); + } else push_int_val(seedrand(time(0))); break; case tkcos: case tksin: case tkexp: case tklog: case tksqrt: case tkint: nargs = *ip++; @@ -4483,7 +4476,7 @@ static void run(int optind, int argc, char **argv, char *sepstring, new_file("/dev/stdout", stdout, 'w', 1, 1); TT.zstdout = TT.zfiles; new_file("/dev/stderr", stderr, 'w', 1, 1); - seedrand(123); + seedrand(1); int status = -1, r = 0; if (TT.cgl.first_begin) r = interp(TT.cgl.first_begin, &status); if (r != tkexit) -- 2.39.2