aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/cat/cat.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/cat/cat.c')
-rw-r--r--sys/cmd/cat/cat.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/sys/cmd/cat/cat.c b/sys/cmd/cat/cat.c
new file mode 100644
index 0000000..847ec0c
--- /dev/null
+++ b/sys/cmd/cat/cat.c
@@ -0,0 +1,40 @@
+#include <u.h>
+#include <libn.h>
+
+static
+void
+cat(Stream *f, byte *name)
+{
+ long n;
+ byte buf[8192];
+
+ while(n = io·read(f, 1, arrlen(buf), buf), n > 0) {
+ if (io·write(stdout, 1, n, buf) != n) {
+ panicf("failed to write while copying stream '%s'", name);
+ }
+ }
+
+ if (n < 0) {
+ panicf("failed to read from buffer '%s'", name);
+ }
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+ Stream *f;
+
+ if (argc == 1) {
+ f = stdin;
+ cat(f, "<stdin>");
+ } else for (i = 1; i < argc; i++) {
+ if (f = io·open(argv[i], "r"), f == nil)
+ panicf("can't open %s", argv[i]);
+ cat(f, argv[i]);
+ io·close(f);
+ }
+
+ exit(0);
+}