new copyright notice
[unix-history] / usr / src / old / libndbm / dbm.c
CommitLineData
fea1cabb 1/*
59815af2
KB
2 * Copyright (c) 1985 The Regents of the University of California.
3 * All rights reserved.
4 *
f1855e9f 5 * %sccs.include.redist.c%
fea1cabb
JB
6 */
7
06c51eed 8#ifndef lint
f1855e9f 9static char sccsid[] = "@(#)dbm.c 5.5 (Berkeley) %G%";
59815af2 10#endif /* not lint */
06c51eed
RE
11
12#include "dbm.h"
13
14#define NODB ((DBM *)0)
15
16static DBM *cur_db = NODB;
17
18static char no_db[] = "dbm: no open database\n";
19
20dbminit(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);
ac2e7cc8
RE
29 if (cur_db == NODB)
30 return (-1);
06c51eed
RE
31 }
32 return (0);
33}
34
35long
36forder(key)
37datum key;
38{
39 if (cur_db == NODB) {
40 printf(no_db);
41 return (0L);
42 }
43 return (dbm_forder(cur_db, key));
44}
45
46datum
47fetch(key)
48datum 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
60delete(key)
61datum 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
72store(key, dat)
73datum 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
85datum
86firstkey()
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
98datum
99nextkey(key)
100datum 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}