forked from vmware-labs/webassembly-language-runtimes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
87 lines (67 loc) · 1.75 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <string.h>
#include <uuid/uuid.h>
#include <zlib.h>
static char *USAGE_FMT =
"Usage: %s [compress|decompress] INPUT_FILE OUTPUT_FILE\n"
" OUTPUT_FILE always gets overwritten.\n"
"\n"
"\n"
"Usage: %s [genuuid]\n"
" Generates UUID and prints it to STDOUT\n"
"\n"
"\n"
"Program exits on any system error!\n"
"\n";
static char BUF[1024];
int decompress_impl(char *input_file, char *ouput_file)
{
gzFile in = gzopen(input_file, "rb");
FILE *out = fopen(ouput_file, "wb");
if (!in || !out)
return -1;
int num_read = 0;
while ((num_read = gzread(in, BUF, sizeof(BUF))) > 0)
fwrite(BUF, 1, num_read, out);
gzclose(in);
fclose(out);
return 0;
}
int compress_impl(char *input_file, char *ouput_file)
{
FILE *in = fopen(input_file, "rb");
gzFile out = gzopen(ouput_file, "wb");
if (!in || !out)
return -1;
int num_read = 0;
while ((num_read = fread(BUF, 1, sizeof(BUF), in)) > 0)
gzwrite(out, BUF, num_read);
fclose(in);
gzclose(out);
return 0;
}
int genuuid_impl()
{
uuid_t generated;
uuid_generate(generated);
char buffer[64];
uuid_unparse_lower(generated, buffer);
fprintf(stdout, "%s\n", buffer);
return 0;
}
int main(int argc, char **argv)
{
if (0 == strcmp(argv[1], "genuuid") && argc == 2)
return genuuid_impl();
if (argc != 4)
{
fprintf(stderr, USAGE_FMT, argv[0], argv[0]);
return -1;
}
if (0 == strcmp(argv[1], "compress"))
return compress_impl(argv[2], argv[3]);
if (0 == strcmp(argv[1], "decompress"))
return decompress_impl(argv[2], argv[3]);
fprintf(stderr, USAGE_FMT, argv[0], argv[0]);
return -1;
}