-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
40 lines (37 loc) · 1.02 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "minify.h"
int main(int argc, const char **argv) {
const char *suffix = SUFFIX;
size_t length_suffix = strlen(suffix);
for(int i=1; i<argc; i++) {
size_t length_arg = strlen(argv[i]);
char name[length_arg + length_suffix + 1];
memset(name, 0, sizeof(name));
bool state = strcmp(suffix, argv[i] + length_arg - length_suffix);
if(state) {
sprintf(name, "%s%s", argv[i], suffix);
} else {
strncpy(name, argv[i], length_arg - length_suffix);
}
FILE *in = fopen(argv[i], "rb");
if(!errno) {
FILE *out = fopen(name, "wb");
printf("%s: %s\n", state ? "compressing" : "decompressing", name);
clock_t start = clock();
size_t rounds = action(out, in, state);
clock_t interval = clock() - start;
printf("time elapsed: %lf seconds, %ld rounds\n",
interval / (CLOCKS_PER_SEC / 1.0), rounds
);
fclose(out);
} else {
printf("Error: (%d) %s\n", errno, strerror(errno));
}
fclose(in);
continue;
}
return 0;
}