[net.bugs.4bsd] 4.2 awk difference

z@rocksvax.UUCP (06/08/84)

DESCRIPTION:
	4.2 awk fails to modify $0 when one of $1, $2, ... is modified.  The awk
distributed with 4.1 handles the case below as documented.

REPEAT BY:
	In the file awk1:
	{ $1 = $2 ; print $1 , $0 }

	Then run:
echo a b | awk -f awk1

	Output will be:
b a b
	in 4.1 was:
b b b

POSSIBLE SOLUTION:
	The action of 'print $0' can be performed by 'for (i=1;i<=NF;i++)
printf "%s " $i;'.  Thus one still has the full capability of the old awk.
Since the documentation is slightly unclear about $0 when changed as a
side-effect it might be better to just delete this feature from awk.  In other
words simply change the documentation.
-- 
//Z\\
James M. Ziobro
Ziobro.Henr@Xerox.ARPA
{rochester,amd70,sunybcs}!rocksvax!z

guy@rlgvax.UUCP (Guy Harris) (06/09/84)

> DESCRIPTION:
> 	4.2 awk fails to modify $0 when one of $1, $2, ... is modified.  The awk
> distributed with 4.1 handles the case below as documented.

> POSSIBLE SOLUTION:
> 	The action of 'print $0' can be performed by 'for (i=1;i<=NF;i++)
> printf "%s " $i;'.  Thus one still has the full capability of the old awk.
> Since the documentation is slightly unclear about $0 when changed as a
> side-effect it might be better to just delete this feature from awk.  In other
> words simply change the documentation.

If I remember correctly, this was not a feature, but a bug.  It was introduced
when "awk" was changed not to try dereferencing NULL pointers.  Bill Shannon
posted a fix; here's the changes to "awk.def" and "tran.c":

*** /tmp/,RCSt1018196	Sat Jun  9 04:03:41 1984
--- awk.def	Tue May 22 21:07:58 1984
***************
*** 5,10
  #define	xfree(a)	{ if(a!=NULL) { yfree(a); a=NULL;} }
  #define	strfree(a)	{ if(a!=NULL && a!=EMPTY) { yfree(a);} a=EMPTY; }
  #define yfree free
  #ifdef	DEBUG
  #	define	dprintf	if(dbg)printf
  #else

--- 5,11 -----
  #define	xfree(a)	{ if(a!=NULL) { yfree(a); a=NULL;} }
  #define	strfree(a)	{ if(a!=NULL && a!=EMPTY) { yfree(a);} a=EMPTY; }
  #define yfree free
+ #define isnull(x)	((x) == EMPTY || (x) == NULL)
  #ifdef	DEBUG
  #	define	dprintf	if(dbg)printf
  #else

*** /tmp/,RCSt1018201	Sat Jun  9 04:03:54 1984
--- tran.c	Tue May 22 21:11:06 1984
***************
*** 136,142
  		error(FATAL, "can't set $0");
  	vp->tval &= ~STR;	/* mark string invalid */
  	vp->tval |= NUM;	/* mark number ok */
! 	if ((vp->tval & FLD) && vp->nval == 0)
  		donerec = 0;
  	return(vp->fval = f);
  }

--- 136,142 -----
  		error(FATAL, "can't set $0");
  	vp->tval &= ~STR;	/* mark string invalid */
  	vp->tval |= NUM;	/* mark number ok */
! 	if ((vp->tval & FLD) && isnull(vp->nval))
  		donerec = 0;
  	return(vp->fval = f);
  }
***************
*** 151,157
  		error(FATAL, "can't set $0");
  	vp->tval &= ~NUM;
  	vp->tval |= STR;
! 	if ((vp->tval & FLD) && vp->nval == 0)
  		donerec = 0;
  	if (!(vp->tval&FLD))
  		strfree(vp->sval);

--- 151,157 -----
  		error(FATAL, "can't set $0");
  	vp->tval &= ~NUM;
  	vp->tval |= STR;
! 	if ((vp->tval & FLD) && isnull(vp->nval))
  		donerec = 0;
  	if (!(vp->tval&FLD))
  		strfree(vp->sval);

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy

rcd@opus.UUCP (Dick Dunn) (06/12/84)

>DESCRIPTION:
>	4.2 awk fails to modify $0 when one of $1, $2, ... is modified.  The awk
>distributed with 4.1 handles the case below as documented.
>...
>POSSIBLE SOLUTION:
>	The action of 'print $0' can be performed by 'for (i=1;i<=NF;i++)
>printf "%s " $i;'.  Thus one still has the full capability of the old awk.
>Since the documentation is slightly unclear about $0 when changed as a
>side-effect it might be better to just delete this feature from awk.  In other
>words simply change the documentation.

The documentation isn't at all unclear.  See the cover page of the awk
document in Vol 2A - it illustrates with:
	{$1 = log($1); print}
which, it states, "replaces the first field of each line by its algorithm".

The 4.2 behavior is definitely a bug which needs fixing.
-- 
Dick Dunn	{hao,ucbvax,allegra}!nbires!rcd		(303)444-5710 x3086
	...Cerebus for dictator!

kar@ritcv.UUCP (Kenneth A. Reek) (06/12/84)

Regarding the problem with 4.2 awk not changing $0 when any of $1, $2, etc.
are changed:

	We have fixed this problem at RIT and will gladly send diff's to
anyone who wants them.  Our diffs also include changes to implement two
other versions of awk, one that uses double precision floating point, and
one that uses long integers.

	I had sent a note about this out a couple of months ago.  Send
inquiries to:

	Ken Reek, Rochester Institute of Technology
	{allegra,seismo}!rochester!ritcv!kar
-- 
	Ken Reek, Rochester Institute of Technology
	{allegra,seismo}!rochester!ritcv!kar