[comp.os.minix] Another echo

housel@en.ecn.purdue.edu (Peter S. Housel) (09/23/89)

	Here is echo.c, with another fix applied. Thanks to Ronald
Lamprecht for noting the buffering bug.

-Peter S. Housel	housel@ecn.purdue.edu		...!pur-ee!housel

echo 'x - echo.c'
sed 's/^X//' <<'**-echo.c-EOF-**' >echo.c
X/* echo - echo arguments	Author: Andy Tanenbaum */
X
X#define SIZE 1024
Xchar buf[SIZE];
Xint count;
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X  register int i, nflag;
X
X  nflag = 0;
X  if(argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n') {
X	nflag++;
X	argc--;
X	argv++;
X  }
X
X  for(i = 1; i < argc; i++) {
X	collect(argv[i]);
X	if (i < argc - 1) collect(" ");
X  }
X
X  if (nflag == 0) collect("\n");
X
X  /* Print accumulated output. */
X  if (count > 0) write(1, buf, count);
X  exit(0);
X}
X
Xcollect(s)
Xchar *s;
X{
X/* Collect characters.  For efficiency, write them in large chunks. */
X  char c;
X
X  while ( (c = *s++) != 0) {
X	if (count == SIZE) {write(1, buf, count); count = 0;}
X	buf[count++] = c;
X  }
X}
**-echo.c-EOF-**