diff options
Diffstat (limited to 'src/cmd/core/cat.c')
-rw-r--r-- | src/cmd/core/cat.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/cmd/core/cat.c b/src/cmd/core/cat.c new file mode 100644 index 0000000..4f0929c --- /dev/null +++ b/src/cmd/core/cat.c @@ -0,0 +1,52 @@ +#include <u.h> +#include <base.h> +#include <libfmt.h> + +static void +usage(void) +{ + fmt·panic("usage: %s [-u] [file ...]\n", argv0); + exits("usage"); +} + +static void +cat(int fd, char *s) +{ + long n; + char buf[8192]; + + while((n=read(fd, buf, sizeof(buf)))>0){ + if(write(1, buf, n) != n) + fmt·panic("write error copying %s: %r", s); + } + + if(n<0) + fmt·panic("error reading %s: %r", s); +} + +int +main(int argc, char *argv[]) +{ + int fd; + + ARGBEGIN{ + case 'u': /* ignore */ break; + default: + usage(); + }ARGEND; + + if(!argc){ + cat(0, "<stdin>"); + exits(0); + } + + while(argc-- > 0){ + if((fd = open(*argv, O_RDONLY))<0) + fmt·panic("can't open %s: %r", *argv); + + cat(fd, *argv); + close(fd); + + argv++; + } +} |