lat/src/main.c

59 lines
948 B
C
Raw Normal View History

2023-04-10 04:58:48 +00:00
#include <stdio.h>
#include <stdlib.h>
#include "lib.h"
2023-04-10 16:51:42 +00:00
#define INVERT_T "\x1b[7m"
#define UINVERT_T "\x1b[27m"
2023-04-10 04:58:48 +00:00
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("usage: catclone <FILE>\n");
die("args");
}
2023-04-10 16:51:42 +00:00
FILE *fp = fopen(argv[1], "r+b");
2023-04-10 04:58:48 +00:00
2023-04-10 16:51:42 +00:00
if (fp == NULL)
2023-04-10 04:58:48 +00:00
die("fopen");
2023-04-10 16:51:42 +00:00
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++;
2023-04-10 04:58:48 +00:00
}
2023-04-10 16:51:42 +00:00
printf("\r\n");
char *format = formatBytes(&fsize);
printf("%s%.2f %s%s\r\n", INVERT_T, fsize, format, UINVERT_T);
2023-04-10 04:58:48 +00:00
fclose(fp);
2023-04-09 17:42:44 +00:00
return 0;
}