shithub: kandr

ref: 91fc7321c2b7df934036f7b88ffdd5d6c826e941
dir: kandr/ex/ex1-16.c

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>

/* 
Print the length of arbitraryily long input lines
	and as much as possible of the text.
*/
void
main(int, char*[])
{
	Biobuf	*in, *out;
	in = Bfdopen(0, OREAD);
	out = Bfdopen(1, OWRITE);

	u32int len = 0;
	while(1){
		long r;
		int n;

		// Read
		r = Bgetrune(in);
		if(r == Beof)
			break;
		if(r < 0)
			sysfatal("err: could not read rune → %r\n");

		len++;
		if(r == L'\n'){
			Bprint(out, "\n» Line length = %uld\n", len);
			len = 0;
		}

		// Write
		n = Bputrune(out, r);
		if(n < 0)
			sysfatal("err: could not write rune → %r\n");
	}

	Bterm(in);
	Bterm(out);

	exits(nil);
}