diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1999-02-12 04:46:29 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1999-02-12 04:46:29 +0000 |
commit | 9d0a9f0f3779340824f23a553aa65b053f43e755 (patch) | |
tree | 732fbd108813afef7ffe6079bebd7e305257e062 | |
parent | 5d3f7faacac9b67fd57dbe6d02a13c890a84d563 (diff) |
add old dbm-compatible interface from db-1.86 with minor changes by me (needs a man page)
-rw-r--r-- | include/Makefile | 4 | ||||
-rw-r--r-- | include/dbm.h | 43 | ||||
-rw-r--r-- | lib/libc/db/hash/ndbm.c | 148 |
3 files changed, 175 insertions, 20 deletions
diff --git a/include/Makefile b/include/Makefile index 106c1cfaa13..5fabbc903b7 100644 --- a/include/Makefile +++ b/include/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.58 1999/02/01 22:59:15 d Exp $ +# $OpenBSD: Makefile,v 1.59 1999/02/12 04:46:27 millert Exp $ # $NetBSD: Makefile,v 1.59 1996/05/15 21:36:43 jtc Exp $ # @(#)Makefile 5.45.1.1 (Berkeley) 5/6/91 @@ -9,7 +9,7 @@ # Missing: mp.h FILES= a.out.h ar.h assert.h bitstring.h blf.h bm.h cast.h cpio.h ctype.h \ - curses.h db.h des.h dirent.h disktab.h elf_abi.h err.h fnmatch.h \ + curses.h db.h dbm.h des.h dirent.h disktab.h elf_abi.h err.h fnmatch.h \ fstab.h fts.h glob.h grp.h ieeefp.h inttypes.h iso646.h kvm.h \ langinfo.h libgen.h limits.h locale.h malloc.h math.h md4.h md5.h \ memory.h mpool.h ndbm.h netdb.h netgroup.h nlist.h nl_types.h \ diff --git a/include/dbm.h b/include/dbm.h new file mode 100644 index 00000000000..d1422292f24 --- /dev/null +++ b/include/dbm.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 1999 Todd C. Miller <Todd.Miller@courtesan.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DBM_H_ +#define _DBM_H_ + +#include <ndbm.h> + +__BEGIN_DECLS +int dbmclose __P((void)); +int dbminit __P((char *)); +int delete __P((datum)); +datum fetch __P((datum)); +datum firstkey __P((void)); +datum nextkey __P((datum)); +int store __P((datum, datum)); +__END_DECLS + +#endif /* _DBM_H_ */ 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; { |