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