aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: b3724e3086873008bddd3b35ce6eb26dfb5990f2 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>

#include "lib.h"

#define INVERT_T "\x1b[7m"
#define UINVERT_T "\x1b[27m"

int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("usage: catclone <FILE>\n");
    die("args");
  }

  FILE *fp = fopen(argv[1], "r+b");

  if (fp == NULL)
    die("fopen");

  printf("%s%s%s\r\n", INVERT_T, argv[1], UINVERT_T);

  int bufsize = 4;
  char *buf;

  buf = malloc(bufsize);
  if (buf == NULL)
    die("malloc");

  double fsize = 0;
  int offset = 0;
  char c;
  while (fread(&c, sizeof(char), 1, fp) > 0) {
    if (fsize == bufsize - 1) {
      bufsize *= 2;

      char *new_buf = realloc(buf, bufsize);
      if (buf == NULL) {
        free(buf);
        die("realloc");
      }

      buf = new_buf;
    }

    buf[offset++] = c;
    fsize++;
  }

  printf("\r\n");

  char *format = formatBytes(&fsize);

  printf("%s%.2f %s%s\r\n", INVERT_T, fsize, format, UINVERT_T);

  fclose(fp);

  return 0;
}