/* lump.c - catenate groups of lines */
/* LUMP n <... >... will delete all '\n' except every n'th '\n' */
/* ufilter2.c - prototype filter program - line (actually record) oriented */
#include <stdio.h>
#include <algol68.h>
#define BUFSIZ 4096
char buffer[BUFSIZ];
extern int $$ferr;
main(argc,argv)
char **argv;
BEGIN
register int	howlong;	/* char length of this record		*/
int		lump;		/* keep only every lump'th '\n'		*/
unsigned	howfar;		/* index next free slot in buffer	*/
int		linesleft;	/* how many lines to read before spew	*/

	sscanf(argv[1],"%d",&lump);
	IF	(lump<=1)
	THEN	error("lump=%d",lump);
	FI;

	linesleft = lump;
	howfar = 0;
	WHILE	(!feof(stdin))
	DO	howlong = fget(buffer+howfar,BUFSIZ,stdin);
		IF	(ferror(stdin))
		THEN	error("read failure $$ferr=%oo\7\n",$$ferr);
		FI;
/* FUCK RBD MM "rsx cleverness" in ioget.mac !
   fget puts a free '\n' on the end of the record, and incs bytes read.
   i don't want this
*/
		howlong --;	/* lose '\n' invented by fget() $$get */
		howfar += howlong;
		IF	(!--linesleft)
		THEN	linesleft=lump;
			fput(buffer,howfar,stdout);
			IF	(ferror(stdout))
			THEN	error("write failed $$ferr=%oo\7\n",$$ferr);
			FI;
			howfar = 0;
		FI;
	OD;
END
/* end: ufilter2.c */
/* end: lump.c */
