1 /* 2 * Copyright (c) 1985 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)dbm.c 5.5 (Berkeley) 06/01/90"; 10 #endif /* not lint */ 11 12 #include "dbm.h" 13 14 #define NODB ((DBM *)0) 15 16 static DBM *cur_db = NODB; 17 18 static char no_db[] = "dbm: no open database\n"; 19 20 dbminit(file) 21 char *file; 22 { 23 if (cur_db != NODB) 24 dbm_close(cur_db); 25 26 cur_db = dbm_open(file, 2, 0); 27 if (cur_db == NODB) { 28 cur_db = dbm_open(file, 0, 0); 29 if (cur_db == NODB) 30 return (-1); 31 } 32 return (0); 33 } 34 35 long 36 forder(key) 37 datum key; 38 { 39 if (cur_db == NODB) { 40 printf(no_db); 41 return (0L); 42 } 43 return (dbm_forder(cur_db, key)); 44 } 45 46 datum 47 fetch(key) 48 datum key; 49 { 50 datum item; 51 52 if (cur_db == NODB) { 53 printf(no_db); 54 item.dptr = 0; 55 return (item); 56 } 57 return (dbm_fetch(cur_db, key)); 58 } 59 60 delete(key) 61 datum key; 62 { 63 if (cur_db == NODB) { 64 printf(no_db); 65 return (-1); 66 } 67 if (dbm_rdonly(cur_db)) 68 return (-1); 69 return (dbm_delete(cur_db, key)); 70 } 71 72 store(key, dat) 73 datum key, dat; 74 { 75 if (cur_db == NODB) { 76 printf(no_db); 77 return (-1); 78 } 79 if (dbm_rdonly(cur_db)) 80 return (-1); 81 82 return (dbm_store(cur_db, key, dat, DBM_REPLACE)); 83 } 84 85 datum 86 firstkey() 87 { 88 datum item; 89 90 if (cur_db == NODB) { 91 printf(no_db); 92 item.dptr = 0; 93 return (item); 94 } 95 return (dbm_firstkey(cur_db)); 96 } 97 98 datum 99 nextkey(key) 100 datum key; 101 { 102 datum item; 103 104 if (cur_db == NODB) { 105 printf(no_db); 106 item.dptr = 0; 107 return (item); 108 } 109 return (dbm_nextkey(cur_db, key)); 110 } 111