shithub: riscv

Download patch

ref: 722bfb73f6d5f5cbcf5b551c51db552f535e2c9c
parent: 924a6155c2f559aa34a546b8b651bd49dc0bb8b2
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:
--