about summary refs log tree commit diff
path: root/cat.c
blob: 92a19b7e74137fc9b48b1e2db74483dd2e8e3685 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}