summaryrefslogtreecommitdiff
path: root/src/main.c
blob: e51d863170c30eeee23336812597539a92a24e6c (plain)
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
#include <stdlib.h>
#include <inttypes.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

#include "e2e.h"

uint32_t u32le(const char*);

int main(int argc, char **argv)
{
	int rv = 0;
	char *inpath;
	int fd;
	struct stat st;
	void *input;

	if (argc < 2) {
		fprintf(stderr, "%s [FILE]\n", argv[0]);
		return 1;
	}

	inpath = argv[1];

	fd = open(inpath, O_RDONLY);
	if (fd == -1) {
		perror("open");
		return 1;
	}

	if (fstat(fd, &st) == -1) {
		perror("fstat");
		rv = 1;
		goto error_mmap;
	}

	input = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (!input) {
		perror("mmap");
		rv = 1;
		goto error_mmap;
	}

	e2e_read(input, input + st.st_size);

	munmap(input, st.st_size);
error_mmap:
	close(fd);
	return rv;
}