Blame src/logic/amgc_map_input.c

383aa6
/**********************************************************/
383aa6
/*  apimagic: cparser-based API normalization utility     */
383aa6
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
383aa6
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
383aa6
/**********************************************************/
383aa6
383aa6
#include <stdint.h>
383aa6
#include <stdbool.h>
383aa6
#include <unistd.h>
383aa6
#include <fcntl.h>
383aa6
#include <sys/mman.h>
383aa6
#include <sys/types.h>
383aa6
#include <sys/stat.h>
383aa6
383aa6
#include <apimagic/apimagic.h>
383aa6
383aa6
int amgc_map_input(
383aa6
	int			fd,
383aa6
	const char *		path,
383aa6
	int			prot,
383aa6
	struct amgc_input *	map)
383aa6
{
7ac2f6
	struct stat	st;
383aa6
	bool		fnew;
383aa6
	int		ret;
383aa6
383aa6
	if ((fnew = (fd < 0)))
383aa6
		fd  = open(path,O_RDONLY | O_CLOEXEC);
383aa6
383aa6
	if (fd < 0)
383aa6
		return -1;
383aa6
7ac2f6
	if ((ret = fstat(fd,&st) < 0) && fnew)
383aa6
		close(fd);
383aa6
383aa6
	if (ret < 0)
383aa6
		return -1;
383aa6
7ac2f6
	map->size = st.st_size;
383aa6
	map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
383aa6
383aa6
	if (fnew)
383aa6
		close(fd);
383aa6
383aa6
	return (map->addr == MAP_FAILED) ? -1 : 0;
383aa6
}
383aa6
383aa6
int amgc_unmap_input(struct amgc_input * map)
383aa6
{
383aa6
	return munmap(map->addr,map->size);
383aa6
}