#include <sys/mman.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "utf8.h"

int
main(int argc, char **argv)
{
	int fd;
	void *buf;
	size_t len;
	struct stat st;
	int ret;

	if (argc != 2) {
		fputs("usage: utf8test inputfile\n", stderr);
		return EINVAL;
	}

	fd = open(argv[1], O_RDONLY);
	if (fd < 0)
		return errno;

	if (fstat(fd, &st) < 0) {
		ret = errno;
		close(fd);
		return ret;
	}

	len = st.st_size;

	buf = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
	if (buf == MAP_FAILED) {
		ret = errno;
		close(fd);
		return ret;
	}

	close(fd);

	ret = (utf8_valid(buf, len) == len) ? 0 : EBADMSG;

	munmap(buf, len);

	return ret;
}

