ref: 7fded5d9435a01292e4187d949edbc45615cd770
parent: 9e0e429f73a9fe4373d64d4ece0394e62141f5ac
author: qwx <qwx@sciops.net>
date: Thu Apr 10 02:53:33 EDT 2025
awk: fix srand not returning previous seed this was contrary to the manpage and other awk implementations. the patch aligns the code with onetrueawk's.
--- a/sys/src/cmd/awk/main.c
+++ b/sys/src/cmd/awk/main.c
@@ -37,6 +37,7 @@
Biobuf stderr;
int dbg = 0;
+Awkfloat srand_seed = 1;
char *cmdname; /* gets argv[0] for error messages */
extern Biobuf *yyin; /* lex input file */
char *lexprog; /* points to program argument if it exists */
@@ -67,6 +68,7 @@
}
atnotify(handler, 1);
+ srand((unsigned long) srand_seed);
yyin = nil;
symtab = makesymtab(NSYMTAB);
while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
--- a/sys/src/cmd/awk/run.c
+++ b/sys/src/cmd/awk/run.c
@@ -31,6 +31,7 @@
jmp_buf env;
extern int pairstack[];
+extern Awkfloat srand_seed;
Node *winner = nil; /* root of parse tree */
Cell *tmps; /* free temporary cells for execution */
@@ -1536,7 +1537,7 @@
Cell *bltin(Node **a, int) /* builtin functions. a[0] is type, a[1] is arg list */
{
Cell *x, *y;
- Awkfloat u;
+ Awkfloat u, tmp;
int t;
Rune wc;
char *p, *buf;
@@ -1590,10 +1591,12 @@
break;
case FSRAND:
if (isrec(x)) /* no argument provided */
- u = (Awkfloat) (truerand() >> 1);
+ tmp = (Awkfloat) (truerand() >> 1);
else
- u = getfval(x);
- srand((unsigned int) u);
+ tmp = getfval(x);
+ srand((unsigned long) tmp);
+ u = srand_seed;
+ srand_seed = tmp;
break;
case FTOUPPER:
case FTOLOWER:
--
⑨