summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1999-02-12 04:46:29 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1999-02-12 04:46:29 +0000
commit9d0a9f0f3779340824f23a553aa65b053f43e755 (patch)
tree732fbd108813afef7ffe6079bebd7e305257e062 /lib
parent5d3f7faacac9b67fd57dbe6d02a13c890a84d563 (diff)
add old dbm-compatible interface from db-1.86 with minor changes by me (needs a man page)
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/db/hash/ndbm.c148
1 files changed, 130 insertions, 18 deletions
diff --git a/lib/libc/db/hash/ndbm.c b/lib/libc/db/hash/ndbm.c
index e7a9d1c1cf5..545d0fa5c9f 100644
--- a/lib/libc/db/hash/ndbm.c
+++ b/lib/libc/db/hash/ndbm.c
@@ -1,3 +1,5 @@
+/* $OpenBSD: ndbm.c,v 1.6 1999/02/12 04:46:28 millert Exp $ */
+
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -35,29 +37,134 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: ndbm.c,v 1.5 1997/02/16 11:08:16 deraadt Exp $";
+#if 0
+static char sccsid[] = "@(#)dbm.c 8.6 (Berkeley) 11/7/95";
+#else
+static char rcsid[] = "$OpenBSD: ndbm.c,v 1.6 1999/02/12 04:46:28 millert Exp $";
+#endif
#endif /* LIBC_SCCS and not lint */
-/*
- * This package provides a dbm compatible interface to the new hashing
- * package described in db(3).
- */
-
#include <sys/param.h>
+#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <string.h>
-#include <errno.h>
#include <ndbm.h>
#include "hash.h"
/*
+ *
+ * This package provides dbm and ndbm compatible interfaces to DB.
+ * First are the DBM routines, which call the NDBM routines, and
+ * the NDBM routines, which call the DB routines.
+ */
+static DBM *__cur_db;
+
+static void no_open_db __P((void));
+
+int
+dbminit(file)
+ char *file;
+{
+ if (__cur_db != NULL)
+ (void)dbm_close(__cur_db);
+ if ((__cur_db = dbm_open(file, O_RDWR, 0)) != NULL)
+ return (0);
+ if ((__cur_db = dbm_open(file, O_RDONLY, 0)) != NULL)
+ return (0);
+ return (-1);
+}
+
+int
+dbmclose()
+{
+
+ if (__cur_db != NULL)
+ return (dbm_close(__cur_db));
+ return (-1);
+}
+
+datum
+fetch(key)
+ datum key;
+{
+ datum item;
+
+ if (__cur_db == NULL) {
+ no_open_db();
+ item.dptr = 0;
+ return (item);
+ }
+ return (dbm_fetch(__cur_db, key));
+}
+
+datum
+firstkey()
+{
+ datum item;
+
+ if (__cur_db == NULL) {
+ no_open_db();
+ item.dptr = 0;
+ return (item);
+ }
+ return (dbm_firstkey(__cur_db));
+}
+
+datum
+nextkey(key)
+ datum key;
+{
+ datum item;
+
+ if (__cur_db == NULL) {
+ no_open_db();
+ item.dptr = 0;
+ return (item);
+ }
+ return (dbm_nextkey(__cur_db));
+}
+
+int
+delete(key)
+ datum key;
+{
+ if (__cur_db == NULL) {
+ no_open_db();
+ return (-1);
+ }
+ if (dbm_rdonly(__cur_db))
+ return (-1);
+ return (dbm_delete(__cur_db, key));
+}
+
+int
+store(key, dat)
+ datum key, dat;
+{
+ if (__cur_db == NULL) {
+ no_open_db();
+ return (-1);
+ }
+ if (dbm_rdonly(__cur_db))
+ return (-1);
+ return (dbm_store(__cur_db, key, dat, DBM_REPLACE));
+}
+
+static void
+no_open_db()
+{
+ (void)fprintf(stderr, "dbm: no open database.\n");
+}
+
+/*
* Returns:
* *DBM on success
* NULL on failure
*/
-extern DBM *
+DBM *
dbm_open(file, flags, mode)
const char *file;
int flags, mode;
@@ -65,7 +172,7 @@ dbm_open(file, flags, mode)
HASHINFO info;
char path[MAXPATHLEN];
- if (strlen(file) + strlen(DBM_SUFFIX) > sizeof(path)-1) {
+ if (strlen(file) + strlen(DBM_SUFFIX) > sizeof(path) - 1) {
errno = ENAMETOOLONG;
return (NULL);
}
@@ -80,10 +187,15 @@ dbm_open(file, flags, mode)
return ((DBM *)__hash_open(path, flags, mode, &info, 0));
}
-extern void
+/*
+ * Returns:
+ * Nothing.
+ */
+void
dbm_close(db)
DBM *db;
{
+
(void)(db->close)(db);
}
@@ -92,7 +204,7 @@ dbm_close(db)
* DATUM on success
* NULL on failure
*/
-extern datum
+datum
dbm_fetch(db, key)
DBM *db;
datum key;
@@ -118,7 +230,7 @@ dbm_fetch(db, key)
* DATUM on success
* NULL on failure
*/
-extern datum
+datum
dbm_firstkey(db)
DBM *db;
{
@@ -139,7 +251,7 @@ dbm_firstkey(db)
* DATUM on success
* NULL on failure
*/
-extern datum
+datum
dbm_nextkey(db)
DBM *db;
{
@@ -160,7 +272,7 @@ dbm_nextkey(db)
* 0 on success
* <0 failure
*/
-extern int
+int
dbm_delete(db, key)
DBM *db;
datum key;
@@ -183,7 +295,7 @@ dbm_delete(db, key)
* <0 failure
* 1 if DBM_INSERT and entry exists
*/
-extern int
+int
dbm_store(db, key, data, flags)
DBM *db;
datum key, data;
@@ -199,7 +311,7 @@ dbm_store(db, key, data, flags)
(flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
}
-extern int
+int
dbm_error(db)
DBM *db;
{
@@ -209,7 +321,7 @@ dbm_error(db)
return (hp->errno);
}
-extern int
+int
dbm_clearerr(db)
DBM *db;
{
@@ -220,7 +332,7 @@ dbm_clearerr(db)
return (0);
}
-extern int
+int
dbm_dirfno(db)
DBM *db;
{