> >Meh. The current solution seems to have been good enough to survive for
> >a whole decade - also, I think using arc4random() is a good thing (TM).
> >The use case where the values produced are predictable (when srand() is
> >called with an argument) continues to be.
> 
> what i find hard to live with is that, for $0 = 9 (for instance) and $1
> = 9, srand($0) behaves differently than srand($1)... but i won't push my
> personal views on anyone. ;) if you think that's the best, i'm fine with
> leaving things as they are.
I think srand($0) should seed to whatever the content of $0 is, and srand
w/o argument should enable arc4random(). See the diff below.

        $ echo 1|./obj/awk '{srand ; print rand}'     
        0.353398
        $ echo 1|./obj/awk '{srand($0) ; print rand}'
        0.840188
        $ echo 1|./obj/awk '{srand($1) ; print rand}' 
        0.840188
        $ echo 1|./obj/awk '{srand(1) ; print rand}'  
        0.840188

ok? Anyone with a large collection of awk scripts want to test?

cheers,
natano


diff --git a/usr.bin/awk/awkgram.y b/usr.bin/awk/awkgram.y
index 4eb0310..0f54ac5 100644
--- a/usr.bin/awk/awkgram.y
+++ b/usr.bin/awk/awkgram.y
@@ -360,9 +360,9 @@ term:
        | '-' term %prec UMINUS         { $$ = op1(UMINUS, $2); }
        | '+' term %prec UMINUS         { $$ = $2; }
        | NOT term %prec UMINUS         { $$ = op1(NOT, notnull($2)); }
-       | BLTIN '(' ')'                 { $$ = op2(BLTIN, itonp($1), 
rectonode()); }
+       | BLTIN '(' ')'                 { $$ = op2(BLTIN, itonp($1), NIL); }
        | BLTIN '(' patlist ')'         { $$ = op2(BLTIN, itonp($1), $3); }
-       | BLTIN                         { $$ = op2(BLTIN, itonp($1), 
rectonode()); }
+       | BLTIN                         { $$ = op2(BLTIN, itonp($1), NIL); }
        | CALL '(' ')'                  { $$ = op2(CALL, celltonode($1,CVAR), 
NIL); }
        | CALL '(' patlist ')'          { $$ = op2(CALL, celltonode($1,CVAR), 
$3); }
        | CLOSE term                    { $$ = op1(CLOSE, $2); }
diff --git a/usr.bin/awk/run.c b/usr.bin/awk/run.c
index b798da5..640f6ee 100644
--- a/usr.bin/awk/run.c
+++ b/usr.bin/awk/run.c
@@ -35,7 +35,7 @@ THIS SOFTWARE.
 #include "awk.h"
 #include "ytab.h"
 
-#define tempfree(x)    if (istemp(x)) tfree(x); else
+#define tempfree(x)    do {if (istemp(x)) tfree(x);} while (0)
 
 /*
 #undef tempfree
@@ -1487,8 +1487,13 @@ Cell *bltin(Node **a, int n)     /* builtin functions. 
a[0] is type, a[1] is arg lis
        FILE *fp;
 
        t = ptoi(a[0]);
-       x = execute(a[1]);
-       nextarg = a[1]->nnext;
+       if (a[1] == NULL) {
+               x = (t == FSRAND) ? NULL : execute(rectonode());
+               nextarg = NULL;
+       } else {
+               x = execute(a[1]);
+               nextarg = a[1]->nnext;
+       }
        switch (t) {
        case FLENGTH:
                if (isarr(x))
@@ -1588,10 +1593,11 @@ Cell *bltin(Node **a, int n)    /* builtin functions. 
a[0] is type, a[1] is arg lis
                        u = (Awkfloat)arc4random() / 0xffffffff;
                break;
        case FSRAND:
-               if (isrec(x)) { /* no argument provided, want arc4random() */
+               if (x == NULL) {        /* no argument provided, want 
arc4random() */
                        use_srandom = 0;
                        u = srand_seed;
                } else {
+                       x = execute(a[1]);
                        use_srandom = 1;
                        u = getfval(x);
                        tmp = u;
@@ -1630,7 +1636,8 @@ Cell *bltin(Node **a, int n)      /* builtin functions. 
a[0] is type, a[1] is arg lis
                FATAL("illegal function type %d", t);
                break;
        }
-       tempfree(x);
+       if (x != NULL)
+               tempfree(x);
        x = gettemp();
        setfval(x, u);
        if (nextarg != 0) {

Reply via email to