summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pttbbs/common/sys/record.c26
-rw-r--r--pttbbs/include/cmsys.h3
2 files changed, 29 insertions, 0 deletions
diff --git a/pttbbs/common/sys/record.c b/pttbbs/common/sys/record.c
index df78f699..7d908676 100644
--- a/pttbbs/common/sys/record.c
+++ b/pttbbs/common/sys/record.c
@@ -1,6 +1,8 @@
/* $Id$ */
#include <sys/stat.h>
+#include <sys/mman.h>
+#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
@@ -307,3 +309,27 @@ apply_record(const char *fpath, int (*fptr) (void *item, void *optarg),
return 0;
}
+
+int bsearch_record(const char *fpath, const void *key,
+ int (*compar)(const void *item1, const void *item2),
+ size_t size, void *buffer)
+{
+ int fd;
+ size_t sz = dashs(fpath);
+ void *addr = NULL, *found;
+
+ if((fd = open(fpath, O_RDONLY, 0)) < 0)
+ return -1;
+ addr = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
+ if (!addr) {
+ close(fd);
+ return -1;
+ }
+ found = bsearch(key, addr, sz / size, size, compar);
+ if (buffer && found)
+ memcpy(buffer, found, size);
+
+ munmap(addr, sz);
+ close(fd);
+ return found ? (found - addr) / size : -1;
+}
diff --git a/pttbbs/include/cmsys.h b/pttbbs/include/cmsys.h
index b9082763..82936638 100644
--- a/pttbbs/include/cmsys.h
+++ b/pttbbs/include/cmsys.h
@@ -175,6 +175,9 @@ int substitute_record2(const char *fpath,
record_callback_t cb_can_substitue);
int delete_record2(const char *fpath, const void *rptr, size_t size,
int id, record_callback_t cb_can_substitue);
+int bsearch_record(const char *fpath, const void *key,
+ int (*compar)(const void *item1, const void *item2),
+ size_t size, void *buffer);
/* vector.c */
struct Vector {