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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <inflate.h>
void truncate_suffix(char *fname) {
char *dot = strrchr(fname, '.');
const char *suffix = ".deflate";
for (int i = 0; i < 8; i++) {
if (dot == NULL || *(dot + i) != suffix[i]) {
fprintf(stderr, "error: must be a .deflate file\n");
exit(1);
}
}
*dot = '\0';
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: inflate [file]\n");
return 1;
}
char *fname = argv[1];
FILE *fp = fopen(fname, "rb");
if (!fp) {
fprintf(stderr, "error: file not found\n");
return 1;
}
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
truncate_suffix(fname);
inflate(fp, size, fname);
fclose(fp);
return 0;
}