-
Notifications
You must be signed in to change notification settings - Fork 0
/
40image.c
44 lines (38 loc) · 1.39 KB
/
40image.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
41
42
43
44
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "assert.h"
#include "compress40.h"
static void (*compress_or_decompress)(FILE *input) = compress40;
int main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-c") == 0) {
compress_or_decompress = compress40;
} else if (strcmp(argv[i], "-d") == 0) {
compress_or_decompress = decompress40;
} else if (*argv[i] == '-') {
fprintf(stderr, "%s: unknown option '%s'\n",
argv[0], argv[i]);
exit(1);
} else if (argc - i > 2) {
fprintf(stderr, "Usage: %s -d [filename]\n"
" %s -c [filename]\n",
argv[0], argv[0]);
exit(1);
} else {
break;
}
}
assert(argc - i <= 1); /* at most one file on command line */
if (i < argc) {
FILE *fp = fopen(argv[i], "r");
assert(fp != NULL);
compress_or_decompress(fp);
fclose(fp);
} else {
compress_or_decompress(stdin);
}
return EXIT_SUCCESS;
}