summaryrefslogtreecommitdiff
path: root/cat.c
diff options
context:
space:
mode:
Diffstat (limited to 'cat.c')
-rw-r--r--cat.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/cat.c b/cat.c
new file mode 100644
index 0000000..6f8ea55
--- /dev/null
+++ b/cat.c
@@ -0,0 +1,41 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "panic.h"
+
+
+void cat(int fd, char * name)
+{
+ char buffer[4096];
+ size_t length;
+ while (0 < (length=read(fd, buffer, sizeof(buffer))))
+ if (length != write(1, buffer, length))
+ panic("write: %s: %s", name, strerror(errno));
+ if (0 > length)
+ panic("read: %s: %s", name, strerror(errno));
+}
+
+
+int main(int argc, char * argv[])
+{
+ int i;
+ int fd;
+ if (1 == argc)
+ cat(0, "<stdin>");
+ else for (i = 1; i < argc; ++i)
+ {
+ fd = open(argv[i], O_RDONLY);
+ if (0 > fd)
+ panic("open: %s: %s", argv[i], strerror(errno));
+ else
+ {
+ cat(fd, argv[i]);
+ close(fd);
+ }
+ }
+}