diff options
author | midipix <writeonce@midipix.org> | 2024-04-29 02:39:30 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2024-05-07 20:07:25 +0000 |
commit | 02e59c946d51faf976a85610e44d7b0ccf4a6e59 (patch) | |
tree | 660056e01b242e947e27c470b9989c7b1ae6b2b7 /src/driver/tbnf_map_input.c | |
parent | e9098c5591f439cdf8a923c9298b8fc7ff28a3ac (diff) | |
download | treebnf-02e59c946d51faf976a85610e44d7b0ccf4a6e59.tar.bz2 treebnf-02e59c946d51faf976a85610e44d7b0ccf4a6e59.tar.xz |
driver: created program skeleton.
Diffstat (limited to 'src/driver/tbnf_map_input.c')
-rw-r--r-- | src/driver/tbnf_map_input.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/driver/tbnf_map_input.c b/src/driver/tbnf_map_input.c new file mode 100644 index 0000000..629672a --- /dev/null +++ b/src/driver/tbnf_map_input.c @@ -0,0 +1,66 @@ +/**************************************************************/ +/* treebnf: a tree oriented bnf library */ +/* Copyright (C) 2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */ +/**************************************************************/ + +#include <stdint.h> +#include <stdbool.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <treebnf/treebnf.h> +#include "treebnf_driver_impl.h" +#include "treebnf_errinfo_impl.h" + +int tbnf_lib_map_raw_input( + const struct tbnf_driver_ctx * dctx, + int fd, + const char * path, + int prot, + struct tbnf_raw_input * map) +{ + int ret; + struct stat st; + bool fnew; + int fdcwd; + + fdcwd = tbnf_driver_fdcwd(dctx); + + if ((fnew = (fd < 0))) + fd = openat(fdcwd,path,O_RDONLY | O_CLOEXEC); + + if (fd < 0) + return TBNF_SYSTEM_ERROR(dctx,path); + + if ((ret = fstat(fd,&st) < 0) && fnew) + close(fd); + + else if ((st.st_size == 0) && fnew) + close(fd); + + if (ret < 0) + return TBNF_SYSTEM_ERROR(dctx,path); + + else if (st.st_size == 0) + return TBNF_CUSTOM_ERROR( + dctx,TBNF_ERR_IMAGE_SIZE_ZERO); + + map->map_size = st.st_size; + map->map_addr = mmap(0,map->map_size,prot,MAP_PRIVATE,fd,0); + + if (fnew) + close(fd); + + return (map->map_addr == MAP_FAILED) + ? TBNF_SYSTEM_ERROR(dctx,0) + : 0; +} + +int tbnf_lib_unmap_raw_input(struct tbnf_raw_input * map) +{ + return munmap(map->map_addr,map->map_size); +} |