Integrate with Sarathy. perl.h and util.c required manual resolving.
[p5sagit/p5-mst-13.2.git] / ext / SDBM_File / sdbm / dbm.c
CommitLineData
463ee0b2 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
19static char sccsid[] = "@(#)dbm.c 5.4 (Berkeley) 5/24/89";
20#endif /* not lint */
21
22#include "dbm.h"
23
24#define NODB ((DBM *)0)
25
26static DBM *cur_db = NODB;
27
28static char no_db[] = "dbm: no open database\n";
29
ba106d47 30int
31dbminit(char *file)
463ee0b2 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
45long
ba106d47 46forder(datum key)
463ee0b2 47{
48 if (cur_db == NODB) {
49 printf(no_db);
50 return (0L);
51 }
52 return (dbm_forder(cur_db, key));
53}
54
55datum
ba106d47 56fetch(datum key)
463ee0b2 57{
58 datum item;
59
60 if (cur_db == NODB) {
61 printf(no_db);
62 item.dptr = 0;
63 return (item);
64 }
65 return (dbm_fetch(cur_db, key));
66}
67
ba106d47 68int
69delete(datum key)
463ee0b2 70{
71 if (cur_db == NODB) {
72 printf(no_db);
73 return (-1);
74 }
75 if (dbm_rdonly(cur_db))
76 return (-1);
77 return (dbm_delete(cur_db, key));
78}
79
ba106d47 80int
81store(datum key, datum dat)
463ee0b2 82{
83 if (cur_db == NODB) {
84 printf(no_db);
85 return (-1);
86 }
87 if (dbm_rdonly(cur_db))
88 return (-1);
89
90 return (dbm_store(cur_db, key, dat, DBM_REPLACE));
91}
92
93datum
ba106d47 94firstkey(void)
463ee0b2 95{
96 datum item;
97
98 if (cur_db == NODB) {
99 printf(no_db);
100 item.dptr = 0;
101 return (item);
102 }
103 return (dbm_firstkey(cur_db));
104}
105
106datum
ba106d47 107nextkey(datum key)
463ee0b2 108{
109 datum item;
110
111 if (cur_db == NODB) {
112 printf(no_db);
113 item.dptr = 0;
114 return (item);
115 }
116 return (dbm_nextkey(cur_db, key));
117}