summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2013-09-24 20:27:46 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2013-09-24 20:27:46 +0800
commit00a6e9deefd5ab444f4ce72bb9b531c03c926737 (patch)
treef5d9af4c544fabb00dbbfeb4540bdf261fae473c
parent5b031370eb823ec0beb2c9addd63d36815a2e1b3 (diff)
downloadpttbbs-00a6e9deefd5ab444f4ce72bb9b531c03c926737.tar
pttbbs-00a6e9deefd5ab444f4ce72bb9b531c03c926737.tar.gz
pttbbs-00a6e9deefd5ab444f4ce72bb9b531c03c926737.tar.bz2
pttbbs-00a6e9deefd5ab444f4ce72bb9b531c03c926737.tar.lz
pttbbs-00a6e9deefd5ab444f4ce72bb9b531c03c926737.tar.xz
pttbbs-00a6e9deefd5ab444f4ce72bb9b531c03c926737.tar.zst
pttbbs-00a6e9deefd5ab444f4ce72bb9b531c03c926737.zip
Add bsearch_record.
I'm not sure if we need it or not :) git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5880 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-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 {