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>
|
|
|
bb5e47 |
#include "apimagic_errinfo_impl.h"
|
|
|
383aa6 |
|
|
|
383aa6 |
int amgc_map_input(
|
|
|
bb5e47 |
const struct amgc_driver_ctx * dctx,
|
|
|
bb5e47 |
int fd,
|
|
|
bb5e47 |
const char * path,
|
|
|
bb5e47 |
int prot,
|
|
|
bb5e47 |
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)
|
|
|
bb5e47 |
return AMGC_SYSTEM_ERROR(dctx);
|
|
|
383aa6 |
|
|
|
7ac2f6 |
if ((ret = fstat(fd,&st) < 0) && fnew)
|
|
|
383aa6 |
close(fd);
|
|
|
383aa6 |
|
|
|
2c6143 |
else if ((st.st_size == 0) && fnew)
|
|
|
2c6143 |
close(fd);
|
|
|
2c6143 |
|
|
|
383aa6 |
if (ret < 0)
|
|
|
bb5e47 |
return AMGC_SYSTEM_ERROR(dctx);
|
|
|
383aa6 |
|
|
|
2c6143 |
else if (st.st_size == 0)
|
|
|
2c6143 |
return AMGC_CUSTOM_ERROR(
|
|
|
2c6143 |
dctx,AMGC_ERR_SOURCE_SIZE_ZERO);
|
|
|
2c6143 |
|
|
|
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 |
|
|
|
bb5e47 |
return (map->addr == MAP_FAILED)
|
|
|
bb5e47 |
? AMGC_SYSTEM_ERROR(dctx)
|
|
|
bb5e47 |
: 0;
|
|
|
383aa6 |
}
|
|
|
383aa6 |
|
|
|
383aa6 |
int amgc_unmap_input(struct amgc_input * map)
|
|
|
383aa6 |
{
|
|
|
383aa6 |
return munmap(map->addr,map->size);
|
|
|
383aa6 |
}
|