diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1999-04-18 17:08:11 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1999-04-18 17:08:11 +0000 |
commit | b835de039b7bd67bb933abfe4d90444ba26ca99e (patch) | |
tree | afa214c1486bb083dbb31848772368afdba36e46 /lib/libc | |
parent | 44df8b77706890435e485c323ff5be8bec119bcb (diff) |
o Opening a zero-length hash file should not be an error
o Correct odbm support
o Add man links for ndbm and odbm
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/db/hash/hash.c | 9 | ||||
-rw-r--r-- | lib/libc/db/hash/ndbm.c | 114 | ||||
-rw-r--r-- | lib/libc/db/man/Makefile.inc | 8 | ||||
-rw-r--r-- | lib/libc/db/man/dbm.3 | 32 | ||||
-rw-r--r-- | lib/libc/db/man/ndbm.3 | 4 |
5 files changed, 114 insertions, 53 deletions
diff --git a/lib/libc/db/hash/hash.c b/lib/libc/db/hash/hash.c index bfa79e61546..f3635ed7772 100644 --- a/lib/libc/db/hash/hash.c +++ b/lib/libc/db/hash/hash.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hash.c,v 1.7 1999/02/15 05:11:23 millert Exp $ */ +/* $OpenBSD: hash.c,v 1.8 1999/04/18 17:08:07 millert Exp $ */ /*- * Copyright (c) 1990, 1993, 1994 @@ -40,7 +40,7 @@ #if 0 static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94"; #else -static char rcsid[] = "$OpenBSD: hash.c,v 1.7 1999/02/15 05:11:23 millert Exp $"; +static char rcsid[] = "$OpenBSD: hash.c,v 1.8 1999/04/18 17:08:07 millert Exp $"; #endif #endif /* LIBC_SCCS and not lint */ @@ -108,7 +108,7 @@ __hash_open(file, flags, mode, info, dflags) HTAB *hashp; struct stat statbuf; DB *dbp; - int bpages, hdrsize, new_table, nsegs, save_errno; + int bpages, hdrsize, new_table, nsegs, save_errno, rv; if ((flags & O_ACCMODE) == O_WRONLY) { errno = EINVAL; @@ -129,7 +129,8 @@ __hash_open(file, flags, mode, info, dflags) new_table = 0; if (!file || (flags & O_TRUNC) || - (stat(file, &statbuf) && (errno == ENOENT))) { + ((rv = stat(file, &statbuf)) && errno == ENOENT) || + (rv == 0 && statbuf.st_size == 0)) { if (errno == ENOENT) errno = 0; /* Just in case someone looks at errno */ new_table = 1; diff --git a/lib/libc/db/hash/ndbm.c b/lib/libc/db/hash/ndbm.c index ef1bceaf804..2d54848ad78 100644 --- a/lib/libc/db/hash/ndbm.c +++ b/lib/libc/db/hash/ndbm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ndbm.c,v 1.10 1999/02/16 21:57:53 millert Exp $ */ +/* $OpenBSD: ndbm.c,v 1.11 1999/04/18 17:08:07 millert Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -40,7 +40,7 @@ #if 0 static char sccsid[] = "@(#)dbm.c 8.6 (Berkeley) 11/7/95"; #else -static char rcsid[] = "$OpenBSD: ndbm.c,v 1.10 1999/02/16 21:57:53 millert Exp $"; +static char rcsid[] = "$OpenBSD: ndbm.c,v 1.11 1999/04/18 17:08:07 millert Exp $"; #endif #endif /* LIBC_SCCS and not lint */ @@ -62,30 +62,49 @@ static char rcsid[] = "$OpenBSD: ndbm.c,v 1.10 1999/02/16 21:57:53 millert Exp $ */ static DBM *__cur_db; -static void no_open_db __P((void)); +static DBM *_dbm_open __P((const char *, const char *, int, int)); +/* + * Returns: + * 0 on success + * <0 on failure + */ int dbminit(file) const char *file; { + if (__cur_db != NULL) (void)dbm_close(__cur_db); - if ((__cur_db = dbm_open(file, O_RDWR, 0)) != NULL) + if ((__cur_db = _dbm_open(file, ".pag", O_RDWR, 0)) != NULL) return (0); - if ((__cur_db = dbm_open(file, O_RDONLY, 0)) != NULL) + if ((__cur_db = _dbm_open(file, ".pag", O_RDONLY, 0)) != NULL) return (0); return (-1); } +/* + * Returns: + * 0 on success + * <0 on failure + */ int dbmclose() { + int rval; - if (__cur_db != NULL) - return ((__cur_db->close)(__cur_db)); - return (-1); + if (__cur_db == NULL) + return (-1); + rval = (__cur_db->close)(__cur_db); + __cur_db = NULL; + return (rval); } +/* + * Returns: + * DATUM on success + * NULL on failure + */ datum fetch(key) datum key; @@ -93,26 +112,36 @@ fetch(key) datum item; if (__cur_db == NULL) { - no_open_db(); - item.dptr = 0; + item.dptr = NULL; + item.dsize = 0; return (item); } return (dbm_fetch(__cur_db, key)); } +/* + * Returns: + * DATUM on success + * NULL on failure + */ datum firstkey() { datum item; if (__cur_db == NULL) { - no_open_db(); - item.dptr = 0; + item.dptr = NULL; + item.dsize = 0; return (item); } return (dbm_firstkey(__cur_db)); } +/* + * Returns: + * DATUM on success + * NULL on failure + */ datum nextkey(key) datum key; @@ -120,59 +149,58 @@ nextkey(key) datum item; if (__cur_db == NULL) { - no_open_db(); - item.dptr = 0; + item.dptr = NULL; + item.dsize = 0; return (item); } return (dbm_nextkey(__cur_db)); } +/* + * Returns: + * 0 on success + * <0 on failure + */ int delete(key) datum key; { - if (__cur_db == NULL) { - no_open_db(); - return (-1); - } - if (dbm_rdonly(__cur_db)) + + if (__cur_db == NULL || dbm_rdonly(__cur_db)) return (-1); return (dbm_delete(__cur_db, key)); } +/* + * Returns: + * 0 on success + * <0 on failure + */ int store(key, dat) datum key, dat; { - if (__cur_db == NULL) { - no_open_db(); - return (-1); - } - if (dbm_rdonly(__cur_db)) + + if (__cur_db == NULL || 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 */ -DBM * -dbm_open(file, flags, mode) +static DBM * +_dbm_open(file, suff, flags, mode) const char *file; + const char *suff; int flags, mode; { HASHINFO info; char path[MAXPATHLEN]; - if (strlen(file) + strlen(DBM_SUFFIX) > sizeof(path) - 1) { + if (strlen(file) + strlen(suff) > sizeof(path) - 1) { errno = ENAMETOOLONG; return (NULL); } @@ -183,12 +211,26 @@ dbm_open(file, flags, mode) info.hash = NULL; info.lorder = 0; (void)strcpy(path, file); - (void)strcat(path, DBM_SUFFIX); + (void)strcat(path, suff); return ((DBM *)__hash_open(path, flags, mode, &info, 0)); } /* * Returns: + * *DBM on success + * NULL on failure + */ +DBM * +dbm_open(file, flags, mode) + const char *file; + int flags, mode; +{ + + return(_dbm_open(file, DBM_SUFFIX, flags, mode)); +} + +/* + * Returns: * Nothing. */ void @@ -270,7 +312,7 @@ dbm_nextkey(db) /* * Returns: * 0 on success - * <0 failure + * <0 on failure */ int dbm_delete(db, key) @@ -292,7 +334,7 @@ dbm_delete(db, key) /* * Returns: * 0 on success - * <0 failure + * <0 on failure * 1 if DBM_INSERT and entry exists */ int diff --git a/lib/libc/db/man/Makefile.inc b/lib/libc/db/man/Makefile.inc index efd058ce94d..ef58ed8fcce 100644 --- a/lib/libc/db/man/Makefile.inc +++ b/lib/libc/db/man/Makefile.inc @@ -1,6 +1,12 @@ -# $OpenBSD: Makefile.inc,v 1.8 1999/02/15 05:11:24 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.9 1999/04/18 17:08:10 millert Exp $ .PATH: ${LIBCSRCDIR}/db/man MAN+= btree.3 dbm.3 dbopen.3 hash.3 mpool.3 ndbm.3 recno.3 MLINKS+= dbopen.3 db.3 +MLINKS+= dbm.3 dbminit.3 dbm.3 dbmclose.3 dbm.3 fetch.3 dbm.3 store.3 +MLINKS+= dbm.3 delete.3 dbm.3 firstkey.3 dbm.3 nextkey.3 +MLINKS+= ndbm.3 dbm_clearerr.3 ndbm.3 dbm_close.3 ndbm.3 dbm_delete.3 +MLINKS+= ndbm.3 dbm_dirfno.3 ndbm.3 dbm_error.3 ndbm.3 dbm_fetch.3 +MLINKS+= ndbm.3 dbm_firstkey.3 ndbm.3 dbm_nextkey.3 ndbm.3 dbm_open.3 +MLINKS+= ndbm.3 dbm_pagfno.3 ndbm.3 dbm_store.3 diff --git a/lib/libc/db/man/dbm.3 b/lib/libc/db/man/dbm.3 index 799170cb12e..be00a141ae8 100644 --- a/lib/libc/db/man/dbm.3 +++ b/lib/libc/db/man/dbm.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: dbm.3,v 1.1 1999/02/15 05:11:24 millert Exp $ +.\" $OpenBSD: dbm.3,v 1.2 1999/04/18 17:08:10 millert Exp $ .\" .\" Copyright (c) 1999 Todd C. Miller <Todd.Miller@courtesan.com> .\" All rights reserved. @@ -72,12 +72,14 @@ typedef struct { .Pp The .Fn dbminit -function is used to open a database in the file named by -.Fa file , -suffixed with -.Dv DBM_SUFFIX -.Pq Sq Pa .db . -The file is created if it does not already exist. +function is used to open a database. Before the call to +.Fn dbminit , +the files +.Fa file.pag +and +.Fa file.dir +must exist. The user is responsible for creating the zero-length +\&.pag and .dir files. .Pp Once the database is open, .Fn fetch @@ -137,9 +139,19 @@ Functions that return a .Ft datum indicate errors by setting the .Va dptr -field set to +field to .Dv NULL . .Sh SEE ALSO .Xr db 3 , -.Xr ndbm 3 , -.Xr hash 3 +.Xr hash 3 , +.Xr ndbm 3 +.Sh BUGS +Because the +.Nm dbm +routines are implemented on top of the +.Xr db 3 , +only a single file, +.Ar file.pag , +is used to actually store the database. The references to +.Ar file.dir +are purely for backwards compatibility with historic implementations. diff --git a/lib/libc/db/man/ndbm.3 b/lib/libc/db/man/ndbm.3 index e28fc7eb111..7219527a9a7 100644 --- a/lib/libc/db/man/ndbm.3 +++ b/lib/libc/db/man/ndbm.3 @@ -1,5 +1,5 @@ .\" David Leonard, 1998. Placed in the public domain. -.\" $OpenBSD: ndbm.3,v 1.4 1999/02/15 05:11:25 millert Exp $ +.\" $OpenBSD: ndbm.3,v 1.5 1999/04/18 17:08:10 millert Exp $ .Dd May 13, 1998 .Os OpenBSD .Dt NDBM 3 @@ -158,7 +158,7 @@ Routines that return a .Ft datum indicate errors by setting the .Va dptr -field set to +field to .Dv NULL . .Pp The |