#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-1)) > 0){
		buf[n] = '\0';
		pm_dwrite(buf);
	}
	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);		
}
