xref: /csrg-svn/old/libndbm/dbm.c (revision 38117)
1 /*
2  * Copyright (c) 1985 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)dbm.c	5.4 (Berkeley) 05/24/89";
20 #endif /* not lint */
21 
22 #include	"dbm.h"
23 
24 #define	NODB	((DBM *)0)
25 
26 static DBM *cur_db = NODB;
27 
28 static char no_db[] = "dbm: no open database\n";
29 
30 dbminit(file)
31 	char *file;
32 {
33 	if (cur_db != NODB)
34 		dbm_close(cur_db);
35 
36 	cur_db = dbm_open(file, 2, 0);
37 	if (cur_db == NODB) {
38 		cur_db = dbm_open(file, 0, 0);
39 		if (cur_db == NODB)
40 			return (-1);
41 	}
42 	return (0);
43 }
44 
45 long
46 forder(key)
47 datum key;
48 {
49 	if (cur_db == NODB) {
50 		printf(no_db);
51 		return (0L);
52 	}
53 	return (dbm_forder(cur_db, key));
54 }
55 
56 datum
57 fetch(key)
58 datum key;
59 {
60 	datum item;
61 
62 	if (cur_db == NODB) {
63 		printf(no_db);
64 		item.dptr = 0;
65 		return (item);
66 	}
67 	return (dbm_fetch(cur_db, key));
68 }
69 
70 delete(key)
71 datum key;
72 {
73 	if (cur_db == NODB) {
74 		printf(no_db);
75 		return (-1);
76 	}
77 	if (dbm_rdonly(cur_db))
78 		return (-1);
79 	return (dbm_delete(cur_db, key));
80 }
81 
82 store(key, dat)
83 datum key, dat;
84 {
85 	if (cur_db == NODB) {
86 		printf(no_db);
87 		return (-1);
88 	}
89 	if (dbm_rdonly(cur_db))
90 		return (-1);
91 
92 	return (dbm_store(cur_db, key, dat, DBM_REPLACE));
93 }
94 
95 datum
96 firstkey()
97 {
98 	datum item;
99 
100 	if (cur_db == NODB) {
101 		printf(no_db);
102 		item.dptr = 0;
103 		return (item);
104 	}
105 	return (dbm_firstkey(cur_db));
106 }
107 
108 datum
109 nextkey(key)
110 datum key;
111 {
112 	datum item;
113 
114 	if (cur_db == NODB) {
115 		printf(no_db);
116 		item.dptr = 0;
117 		return (item);
118 	}
119 	return (dbm_nextkey(cur_db, key));
120 }
121