[net.sources.bugs] new 'echo'

grady@ucbvax.ARPA (Steven Grady) (04/16/85)

static char *sccsid = "@(#)echo.c	4.1 (Berkeley) 10/1/80";
#include <stdio.h>

main(argc, argv)
int argc;
char *argv[];
{
	register int i, nflg, qflg, rflg;

	srandom(getpid());
	nflg = qflg = rflg = 0;
	while (argc > 1 && argv[1][0] == '-' ){
		i = 0;
		while(argv[1][++i]){
			switch (argv[1][i]){
				case 'n': nflg++;
				case 'q': qflg++;
				case 'r': rflg++;
				}
			}
		argc--;
		argv++;
		}
	for(i=1; i<argc; i++) {
		if ((! qflg) && ((! rflg) || (rflg && (random()&08)))) {
			fputs(argv[i], stdout);
			if (i < argc-1)
				putchar(' ');
		}
	}
	if(! nflg)
		putchar('\n');
	exit(0);
}

grady@ucbvax.ARPA (Steven Grady) (04/16/85)

.TH ECHO 1 "15 April 1985"
.SH NAME
echo \- echo arguments
.SH SYNOPSIS
.B echo
[
.B \-nqr
]
[ arg ] ...
.SH DESCRIPTION
.I Echo
writes its arguments separated by blanks and terminated by
a newline on the standard output.  If the flag
.B \-n
is used, no newline is added to the output.
If the flag
.B \-q
is used, the
.I echo
runs quietly (arguments are not echoed).
.B \-r
is used to echo arguments randomly.
.PP
.I Echo
is useful for producing diagnostics in
shell programs and for writing constant data on pipes.
To send diagnostics to the standard error file, do `echo ... 1>&2'.
.I Echo
with the 
.B \-q
or
.B \-r
option is not useful.

ark@alice.UUCP (Andrew Koenig) (04/16/85)

>	for(i=1; i<argc; i++) {
>		if ((! qflg) && ((! rflg) || (rflg && (random()&08)))) {
>			fputs(argv[i], stdout);
>			if (i < argc-1)
>				putchar(' ');
>		}

random()&08?  Is this some new kind of octal digit?

john@moncol.UUCP (John Ruschmeyer) (04/18/85)

As posted, the new 'echo' program has several bugs all
stemming from missing 'break' statements in the loop which
parses the arguments. The only visible bug, however, is
that the '-n' option will also select the '-q' option and
nothing will be printed.

Also, the '-q' option should cause the '-n' option to be
selected by default. This allows the program to be used to
create an empty file, as in:

	echo -q foo >newfile

The following diff shows the corrections which will take
care of both problems:

16,18c16,18
< 				case 'n': nflg++;
< 				case 'q': qflg++;
< 				case 'r': rflg++;
---
> 				case 'n': nflg++; break;
> 				case 'q': qflg++; nflg++; break;
> 				case 'r': rflg++; break;


-- 
Name:		John Ruschmeyer
US Mail:	Monmouth College, W. Long Branch, NJ 07764
Phone:		(201) 222-6600 x366
UUCP:		...!vax135!petsd!moncol!john	...!princeton!moncol!john
						   ...!pesnta!moncol!john
Silly Quote:
		"Ah, but what is a dream but reality without a backbone?"

mroddy@enmasse.UUCP (Mark Roddy) (04/18/85)

[obsolete but impressive net bug feeder]

> random()&08?  Is this some new kind of octal digit?

"The digits 8 and 9 have octal value 10 and 11 respectively."
{K&R ppg 180 sec 2.4.1}

Don't ask me I just work here.

*** ACE IS IN WITH OUR MESS ***
-- 
						Mark Roddy
						Net working,
						Just reading the news.

					(harvard!talcott!panda!enmasse!mroddy)

piet@mcvax.UUCP (Piet Beertema) (04/19/85)

		>if ((! qflg) && ((! rflg) || (rflg && (random()&08)))) {
	>random()&08?  Is this some new kind of octal digit?
Isn't "8" the only "octal" digit really worth it's name?

-- 
	Piet Beertema, CWI, Amsterdam
	...{seismo,okstate,garfield,decvax,philabs}!mcvax!piet

dave@circadia.UUCP (David Messer) (04/25/85)

> >	for(i=1; i<argc; i++) {
> >		if ((! qflg) && ((! rflg) || (rflg && (random()&08)))) {
> >			fputs(argv[i], stdout);
> >			if (i < argc-1)
> >				putchar(' ');
> >		}
> 
> random()&08?  Is this some new kind of octal digit?

Yes!  The new standard for octal is:  00-17(8) == 00-15(10).  This
allows for a greater range of octal constants and is seen as a new
improvement to ADA.
:-)

-- 

Dave Messer   ...ihnp4!stolaf!umn-cs!circadia!dave