1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Margo Seltzer. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)ndbm.c 5.2 (Berkeley) 02/14/91"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 /* 16 This package provides a dbm compatible interface to the new hashing 17 package described in db(3) 18 */ 19 20 #include <sys/param.h> 21 #include <ndbm.h> 22 #include <stddef.h> 23 #include "hash.h" 24 25 /* 26 return *DBM on success 27 NULL on failure 28 */ 29 extern DBM * 30 dbm_open( file, flags, mode ) 31 char *file; 32 int flags; 33 int mode; 34 { 35 HASHINFO info; 36 char path[MAXPATHLEN]; 37 38 info.bsize = 1024; 39 info.ffactor = 5; 40 info.nelem = 1; 41 info.ncached = NULL; 42 info.hash = NULL; 43 info.lorder = 0; 44 (void)sprintf(path, "%s%s", file, DBM_SUFFIX); 45 return( hash_open ( path, flags, mode, &info ) ); 46 } 47 48 extern void 49 dbm_close(db) 50 DBM *db; 51 { 52 (void)(db->close) (db); 53 } 54 55 /* 56 Returns DATUM on success 57 NULL on failure 58 */ 59 extern datum 60 dbm_fetch( db, key ) 61 DBM *db; 62 datum key; 63 { 64 int status; 65 datum retval; 66 67 status = (db->get) ( db, (DBT *)&key, (DBT *)&retval ); 68 if ( status ) { 69 retval.dptr = NULL; 70 } 71 return(retval); 72 } 73 74 /* 75 Returns DATUM on success 76 NULL on failure 77 */ 78 extern datum 79 dbm_firstkey(db) 80 DBM *db; 81 { 82 int status; 83 datum retkey; 84 datum retdata; 85 86 status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST ); 87 if ( status ) { 88 retkey.dptr = NULL; 89 } 90 return(retkey); 91 } 92 /* 93 Returns DATUM on success 94 NULL on failure 95 */ 96 extern datum 97 dbm_nextkey(db) 98 DBM *db; 99 { 100 int status; 101 datum retkey; 102 datum retdata; 103 104 status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT ); 105 if ( status ) { 106 retkey.dptr = NULL; 107 } 108 return(retkey); 109 } 110 111 /* 112 0 on success 113 <0 failure 114 */ 115 extern int 116 dbm_delete(db, key) 117 DBM *db; 118 datum key; 119 { 120 int status; 121 122 status = (db->delete)( db, (DBT *)&key ); 123 if ( status ) { 124 return(-1); 125 } else { 126 return(0); 127 } 128 } 129 130 /* 131 0 on success 132 <0 failure 133 1 if DBM_INSERT and entry exists 134 */ 135 extern int 136 dbm_store(db, key, content, flags) 137 DBM *db; 138 datum key; 139 datum content; 140 int flags; 141 { 142 return ((db->put)( db, (DBT *)&key, (DBT *)&content, 143 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0 )); 144 } 145 146 extern int 147 dbm_error(db) 148 DBM *db; 149 { 150 HTAB *hp; 151 152 hp = (HTAB *)db->internal; 153 return ( hp->errno ); 154 } 155 156 extern int 157 dbm_clearerr(db) 158 DBM *db; 159 { 160 HTAB *hp; 161 162 hp = (HTAB *)db->internal; 163 hp->errno = 0; 164 return ( 0 ); 165 } 166