diff options
Diffstat (limited to 'src/libstddjb/cdb_init.c')
-rw-r--r-- | src/libstddjb/cdb_init.c | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/libstddjb/cdb_init.c b/src/libstddjb/cdb_init.c index 8b56c1a..7a6c2f6 100644 --- a/src/libstddjb/cdb_init.c +++ b/src/libstddjb/cdb_init.c @@ -1,23 +1,31 @@ /* ISC license. */ -#include <skalibs/bsdsnowflake.h> - #include <sys/stat.h> #include <sys/mman.h> #include <stdint.h> #include <errno.h> +#include <skalibs/djbunix.h> #include <skalibs/cdb.h> -int cdb_init (struct cdb *c, int fd) +int cdb_init (cdb *c, char const *file) { - struct stat st ; char *map ; - if (fstat(fd, &st) < 0) return -1 ; - if (st.st_size > UINT32_MAX) return (errno = EOVERFLOW, -1) ; + struct stat st ; + int fd = openc_read(file) ; + if (fd < 0) return 0 ; + if (fstat(fd, &st) < 0) goto err ; + if (st.st_size > UINT32_MAX) goto eoverf ; map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0) ; - if (map == MAP_FAILED) return -1 ; + if (map == MAP_FAILED) goto err ; c->map = map ; c->size = st.st_size ; + fd_close(fd) ; + return 1 ; + + eoverf: + errno = EOVERFLOW ; + err: + fd_close(fd) ; return 0 ; } |