/* 3grep.c - extract only records that have nominated 1st 3 chars */

/* ufilter2.c - prototype filter program - line (actually record) oriented */
#include <stdio.h>
#include <algol68.h>
#define BUFSIZ 4096
char buffer[BUFSIZ];
extern int $$ferr;

helpless()
BEGIN
	puts("usage: 3GREP abc <infile >outfile");
	puts("\twill only pass records beginning with abc");
	exit();
END

main(argc,argv)
int	argc;
char **argv;
BEGIN
register int	howlong;	/* char length of this record */
register char *	v;		/* the key we match against */
	IF	(argc!=2 || strlen(argv[1])!=3)
	THEN	helpless();
	FI
	v = argv[1];
	WHILE	(!feof(stdin))
	DO	howlong = fget(buffer,BUFSIZ,stdin);
		IF	(ferror(stdin))
		THEN	error("read failure $$ferr=%oo\7\n",$$ferr);
		FI
		IF	(feof(stdin))
		THEN	exit();
		FI
		IF	(buffer[0]==v[0] && buffer[1]==v[1] && buffer[2]==v[2])
		THEN	fput(buffer,howlong,stdout);
			IF	(ferror(stdout))
			THEN	error("write failed $$ferr=%oo\7\n",$$ferr);
			FI
		FI
	OD
END
/* end: ufilter2.c */

/* end: 3grep.c */
