#include <9pm/u.h>
#include <9pm/libc.h>

void
cat(int fd, char *f)
{
	char buf[8192];
	int n;
	
	while((n = read(fd, buf, sizeof buf)) > 0)
		if(write(1, buf, n) != n)
			sysfatal("cat: write failed: %r");
	if(n < 0)
		sysfatal("cat: read failed: %r");
}

void
main(int argc, char **argv)
{
	int i, fd;

	if(argc == 1){
		cat(0, "<stdin>");
		exits(nil);
	}
	for(i=1; i<argc; i++){
		if((fd = open(argv[i], OREAD)) < 0)
			sysfatal("open %s: %r", argv[i]);
		cat(fd, argv[i]);
		close(fd);
	}
	exits(nil);		
}
