xref: /csrg-svn/old/libndbm/dbm.c (revision 24292)
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.3 (Berkeley) 85/08/15";
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 		if (cur_db == NODB)
29 			return (-1);
30 	}
31 	return (0);
32 }
33 
34 long
35 forder(key)
36 datum key;
37 {
38 	if (cur_db == NODB) {
39 		printf(no_db);
40 		return (0L);
41 	}
42 	return (dbm_forder(cur_db, key));
43 }
44 
45 datum
46 fetch(key)
47 datum key;
48 {
49 	datum item;
50 
51 	if (cur_db == NODB) {
52 		printf(no_db);
53 		item.dptr = 0;
54 		return (item);
55 	}
56 	return (dbm_fetch(cur_db, key));
57 }
58 
59 delete(key)
60 datum key;
61 {
62 	if (cur_db == NODB) {
63 		printf(no_db);
64 		return (-1);
65 	}
66 	if (dbm_rdonly(cur_db))
67 		return (-1);
68 	return (dbm_delete(cur_db, key));
69 }
70 
71 store(key, dat)
72 datum key, dat;
73 {
74 	if (cur_db == NODB) {
75 		printf(no_db);
76 		return (-1);
77 	}
78 	if (dbm_rdonly(cur_db))
79 		return (-1);
80 
81 	return (dbm_store(cur_db, key, dat, DBM_REPLACE));
82 }
83 
84 datum
85 firstkey()
86 {
87 	datum item;
88 
89 	if (cur_db == NODB) {
90 		printf(no_db);
91 		item.dptr = 0;
92 		return (item);
93 	}
94 	return (dbm_firstkey(cur_db));
95 }
96 
97 datum
98 nextkey(key)
99 datum key;
100 {
101 	datum item;
102 
103 	if (cur_db == NODB) {
104 		printf(no_db);
105 		item.dptr = 0;
106 		return (item);
107 	}
108 	return (dbm_nextkey(cur_db, key));
109 }
110