about summary refs log tree commit diff
path: root/cat.c
diff options
context:
space:
mode:
authorPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2021-04-17 10:28:11 +0200
committerPatryk Niedźwiedziński <pniedzwiedzinski19@gmail.com>2021-04-17 10:28:11 +0200
commitb1ddaa81f28b8bbc62d6d50526e0935b173099eb (patch)
treee606c194f8eecdedf3f710ed097cadfe97d60122 /cat.c
downloadcat-cosmopolitan-master.tar.gz
cat-cosmopolitan-master.zip
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;
+}