about summary refs log tree commit diff
path: root/cat.c
diff options
context:
space:
mode:
Diffstat (limited to 'cat.c')
-rw-r--r--cat.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/cat.c b/cat.c
new file mode 100644
index 0000000..92a19b7
--- /dev/null
+++ b/cat.c
@@ -0,0 +1,34 @@
+#include "stdio/stdio.h"
+
+int cat(char* filename) {
+  FILE *f;
+  f = fopen(filename, "r");
+  if (f == NULL) {
+    printf("Cannot open file '%s'\n", filename);
+    return 1;
+  }
+
+  char *line = NULL;
+  size_t linecap = 0;
+  ssize_t linelen;
+  while ((linelen = getline(&line, &linecap, f)) != -1) {
+    printf("%s", line);
+  }
+  free(line);
+  fclose(f);
+  return 0;
+}
+
+int main(int argc, char *argv[]) {
+  int err = 0;
+
+  if (argc > 1) {
+    for (int i = 1; i < argc; i++) {
+      cat(argv[i]);
+    }
+  } else {
+    printf("Usage: cat FILE [FILE] ...\n");
+  }
+
+  return 0;
+}