Apparently, the One True awk had some features in it that Plan 9 awk did not: length(array) returns the number of subscripts in array fflush() or fflush("") flushes all open files I added them back in, and updated the manual page. - Pietro Reference: /n/sources/patch/applied/one-true-awk-adjust Date: Thu Jul 3 15:48:01 CES 2008 Signed-off-by: pietro10@mac.com --- /sys/src/cmd/awk/run.c Thu Jul 3 15:46:56 2008 +++ /sys/src/cmd/awk/run.c Thu Jul 3 15:46:53 2008 @@ -1454,14 +1454,20 @@ char mbc[50]; Node *nextarg; FILE *fp; + void flush_all(void); t = ptoi(a[0]); x = execute(a[1]); nextarg = a[1]->nnext; switch (t) { case FLENGTH: - p = getsval(x); - u = (Awkfloat) countposn(p, strlen(p)); break; + if (isarr(x)) + u = ((Array *) x->sval)->nelem; /* GROT. should be function*/ + else { + p = getsval(x); + u = (Awkfloat) countposn(p, strlen(p)); + } + break; case FLOG: u = errcheck(log(getfval(x)), "log"); break; case FINT: @@ -1518,7 +1524,10 @@ free(buf); return x; case FFLUSH: - if ((fp = openfile(FFLUSH, getsval(x))) == NULL) + if (isrec(x) || strlen(getsval(x)) == 0) { + flush_all(); /* fflush() or fflush("") -> all */ + u = 0; + } else if ((fp = openfile(FFLUSH, getsval(x))) == NULL) u = EOF; else u = fflush(fp); @@ -1711,6 +1720,15 @@ if (stat == EOF) WARNING( "i/o error occurred while closing %s", files[i].fname ); } +} + +void flush_all(void) +{ + int i; + + for (i = 0; i < FOPEN_MAX; i++) + if (files[i].fp) + fflush(files[i].fp); } void backsub(char **pb_ptr, char **sptr_ptr); --- /sys/man/1/awk Thu Jul 3 15:46:58 2008 +++ /sys/man/1/awk Thu Jul 3 15:46:57 2008 @@ -204,6 +204,9 @@ .BI fflush( expr ) flushes any buffered output for the file or pipe .IR expr . +If +.IR expr +is omitted or is a null string, all open files are flushed. .PP The mathematical functions .BR exp , @@ -218,11 +221,11 @@ .TF length .TP .B length -the length of its argument -taken as a string, -or of +If its argument is a string, the string's length is returned. +If its argument is an array, the number of subscripts in the array is returned. +If no argument, the length of .B $0 -if no argument. +is returned. .TP .B rand random number on (0,1)