-
Notifications
You must be signed in to change notification settings - Fork 7
/
common_main.h
53 lines (42 loc) · 894 Bytes
/
common_main.h
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
#pragma once
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
options_t *options = parse_options(argc, argv);
int fd = -1;
if (options->input == NULL || options->output == NULL) {
usage();
goto failed;
}
fd = open(options->input, O_RDONLY);
if (fd == -1) {
perror("open");
goto failed;
}
const size_t filesize = fsize(options->input);
void *data = mmap(0, filesize, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
perror("mmap");
goto failed;
}
close(fd);
fd = -1;
do_work(options, data, filesize);
int ret = munmap(data, filesize);
if (ret == -1) {
perror("munmap");
goto failed;
}
free_options(options);
return EXIT_SUCCESS;
failed:
if (options != NULL)
free_options(options);
if (fd != -1)
close(fd);
exit(EXIT_FAILURE);
}