SYN SYN
[p5sagit/p5-mst-13.2.git] / ext / SDBM_File / sdbm / dbm.c
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 notice are
7  * duplicated in all such forms.
8  *
9  * [additional clause stricken -- see below]
10  *
11  * The name of the University may not be used to endorse or promote
12  * products derived from this software without specific prior written
13  * permission.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE.
17  *
18  * This notice previously contained the additional clause:
19  *
20  *   and that any documentation, advertising materials, and other
21  *   materials related to such distribution and use acknowledge that
22  *   the software was developed by the University of California,
23  *   Berkeley.
24  *
25  * Pursuant to the licensing change made by the Office of Technology
26  * Licensing of the University of California, Berkeley on July 22,
27  * 1999 and documented in:
28  *
29  *   ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
30  *
31  * this clause has been stricken and no longer is applicable to this
32  * software.
33  */
34
35 #ifndef lint
36 static char sccsid[] = "@(#)dbm.c    5.4 (Berkeley) 5/24/89";
37 #endif /* not lint */
38
39 #include    "dbm.h"
40
41 #define    NODB    ((DBM *)0)
42
43 static DBM *cur_db = NODB;
44
45 static char no_db[] = "dbm: no open database\n";
46
47 int
48 dbminit(char *file)
49 {
50     if (cur_db != NODB)
51         dbm_close(cur_db);
52
53     cur_db = dbm_open(file, 2, 0);
54     if (cur_db == NODB) {
55         cur_db = dbm_open(file, 0, 0);
56         if (cur_db == NODB)
57             return (-1);
58     }
59     return (0);
60 }
61
62 long
63 forder(datum key)
64 {
65     if (cur_db == NODB) {
66         printf(no_db);
67         return (0L);
68     }
69     return (dbm_forder(cur_db, key));
70 }
71
72 datum
73 fetch(datum key)
74 {
75     datum item;
76
77     if (cur_db == NODB) {
78         printf(no_db);
79         item.dptr = 0;
80         return (item);
81     }
82     return (dbm_fetch(cur_db, key));
83 }
84
85 int
86 delete(datum key)
87 {
88     if (cur_db == NODB) {
89         printf(no_db);
90         return (-1);
91     }
92     if (dbm_rdonly(cur_db))
93         return (-1);
94     return (dbm_delete(cur_db, key));
95 }
96
97 int
98 store(datum key, datum dat)
99 {
100     if (cur_db == NODB) {
101         printf(no_db);
102         return (-1);
103     }
104     if (dbm_rdonly(cur_db))
105         return (-1);
106
107     return (dbm_store(cur_db, key, dat, DBM_REPLACE));
108 }
109
110 datum
111 firstkey(void)
112 {
113     datum item;
114
115     if (cur_db == NODB) {
116         printf(no_db);
117         item.dptr = 0;
118         return (item);
119     }
120     return (dbm_firstkey(cur_db));
121 }
122
123 datum
124 nextkey(datum key)
125 {
126     datum item;
127
128     if (cur_db == NODB) {
129         printf(no_db);
130         item.dptr = 0;
131         return (item);
132     }
133     return (dbm_nextkey(cur_db, key));
134 }