summaryrefslogtreecommitdiff
path: root/src/libstddjb/cdb_init.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2021-07-23 16:43:57 +0000
committerLaurent Bercot <ska@appnovation.com>2021-07-23 16:43:57 +0000
commitdd6bb6c6b8298ebeff2d1882becb36580b969d6f (patch)
tree3d922a5791e7e34e2b041ea5f3489360bfa798e1 /src/libstddjb/cdb_init.c
parent122f9363682e5de8ce4056c4c05c1eaf8935cf19 (diff)
downloadskalibs-dd6bb6c6b8298ebeff2d1882becb36580b969d6f.tar.xz
New 2.11.0.0 branch with several modifications
- libbiguint removed - cdb_make changed to cdbmake (because different ui) - cdb redesigned Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb/cdb_init.c')
-rw-r--r--src/libstddjb/cdb_init.c22
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 ;
}