Blame src/logic/sfrt_map_input.c

f46039
#include <stdint.h>
f46039
#include <stdbool.h>
f46039
#include <unistd.h>
f46039
#include <fcntl.h>
f46039
#include <sys/mman.h>
f46039
#include <sys/types.h>
f46039
#include <sys/stat.h>
f46039
f46039
#include <sofort/sofort.h>
f46039
f46039
int sfrt_map_input(
f46039
	int			fd,
f46039
	const char *		path,
f46039
	int			prot,
f46039
	struct sfrt_input *	map)
f46039
{
f46039
	struct stat	stat;
f46039
	bool		fnew;
f46039
	int		ret;
f46039
f46039
	if ((fnew = (fd < 0)))
f46039
		fd  = open(path,O_RDONLY | O_CLOEXEC);
f46039
f46039
	if (fd < 0)
f46039
		return -1;
f46039
f46039
	if ((ret = fstat(fd,&stat) < 0) && fnew)
f46039
		close(fd);
f46039
f46039
	if (ret < 0)
f46039
		return -1;
f46039
f46039
	map->size = stat.st_size;
f46039
	map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
f46039
f46039
	if (fnew)
f46039
		close(fd);
f46039
f46039
	return (map->addr == MAP_FAILED) ? -1 : 0;
f46039
}
f46039
f46039
int sfrt_unmap_input(struct sfrt_input * map)
f46039
{
f46039
	return munmap(map->addr,map->size);
f46039
}