ndbm.c revision 1.2 1 1.1 cgd /*-
2 1.2 cgd * Copyright (c) 1990, 1993
3 1.2 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Margo Seltzer.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.2 cgd static char sccsid[] = "@(#)ndbm.c 8.1 (Berkeley) 6/4/93";
39 1.1 cgd #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.1 cgd /*
42 1.2 cgd * This package provides a dbm compatible interface to the new hashing
43 1.2 cgd * package described in db(3).
44 1.2 cgd */
45 1.1 cgd
46 1.1 cgd #include <sys/param.h>
47 1.2 cgd
48 1.1 cgd #include <ndbm.h>
49 1.1 cgd #include <stdio.h>
50 1.2 cgd #include <string.h>
51 1.2 cgd
52 1.1 cgd #include "hash.h"
53 1.1 cgd
54 1.1 cgd /*
55 1.2 cgd * Returns:
56 1.2 cgd * *DBM on success
57 1.2 cgd * NULL on failure
58 1.2 cgd */
59 1.1 cgd extern DBM *
60 1.2 cgd dbm_open(file, flags, mode)
61 1.2 cgd const char *file;
62 1.2 cgd int flags, mode;
63 1.2 cgd {
64 1.2 cgd HASHINFO info;
65 1.2 cgd char path[MAXPATHLEN];
66 1.2 cgd
67 1.2 cgd info.bsize = 4096;
68 1.2 cgd info.ffactor = 40;
69 1.2 cgd info.nelem = 1;
70 1.2 cgd info.cachesize = NULL;
71 1.2 cgd info.hash = NULL;
72 1.2 cgd info.lorder = 0;
73 1.2 cgd (void)strcpy(path, file);
74 1.2 cgd (void)strcat(path, DBM_SUFFIX);
75 1.2 cgd return ((DBM *)__hash_open(path, flags, mode, &info));
76 1.1 cgd }
77 1.1 cgd
78 1.2 cgd extern void
79 1.1 cgd dbm_close(db)
80 1.2 cgd DBM *db;
81 1.1 cgd {
82 1.2 cgd (void)(db->close)(db);
83 1.2 cgd }
84 1.2 cgd
85 1.2 cgd /*
86 1.2 cgd * Returns:
87 1.2 cgd * DATUM on success
88 1.2 cgd * NULL on failure
89 1.2 cgd */
90 1.2 cgd extern datum
91 1.2 cgd dbm_fetch(db, key)
92 1.2 cgd DBM *db;
93 1.2 cgd datum key;
94 1.2 cgd {
95 1.2 cgd datum retval;
96 1.2 cgd int status;
97 1.2 cgd
98 1.2 cgd status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
99 1.2 cgd if (status) {
100 1.2 cgd retval.dptr = NULL;
101 1.2 cgd retval.dsize = 0;
102 1.2 cgd }
103 1.2 cgd return (retval);
104 1.1 cgd }
105 1.1 cgd
106 1.1 cgd /*
107 1.2 cgd * Returns:
108 1.2 cgd * DATUM on success
109 1.2 cgd * NULL on failure
110 1.2 cgd */
111 1.2 cgd extern datum
112 1.2 cgd dbm_firstkey(db)
113 1.2 cgd DBM *db;
114 1.1 cgd {
115 1.2 cgd int status;
116 1.2 cgd datum retdata, retkey;
117 1.1 cgd
118 1.2 cgd status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
119 1.2 cgd if (status)
120 1.2 cgd retkey.dptr = NULL;
121 1.2 cgd return (retkey);
122 1.1 cgd }
123 1.1 cgd
124 1.1 cgd /*
125 1.2 cgd * Returns:
126 1.2 cgd * DATUM on success
127 1.2 cgd * NULL on failure
128 1.2 cgd */
129 1.2 cgd extern datum
130 1.1 cgd dbm_nextkey(db)
131 1.2 cgd DBM *db;
132 1.1 cgd {
133 1.2 cgd int status;
134 1.2 cgd datum retdata, retkey;
135 1.1 cgd
136 1.2 cgd status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
137 1.2 cgd if (status)
138 1.2 cgd retkey.dptr = NULL;
139 1.2 cgd return (retkey);
140 1.1 cgd }
141 1.1 cgd /*
142 1.2 cgd * Returns:
143 1.2 cgd * 0 on success
144 1.2 cgd * <0 failure
145 1.2 cgd */
146 1.2 cgd extern int
147 1.1 cgd dbm_delete(db, key)
148 1.2 cgd DBM *db;
149 1.2 cgd datum key;
150 1.1 cgd {
151 1.2 cgd int status;
152 1.1 cgd
153 1.2 cgd status = (db->del)(db, (DBT *)&key, 0);
154 1.2 cgd if (status)
155 1.2 cgd return (-1);
156 1.2 cgd else
157 1.2 cgd return (0);
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd /*
161 1.2 cgd * Returns:
162 1.2 cgd * 0 on success
163 1.2 cgd * <0 failure
164 1.2 cgd * 1 if DBM_INSERT and entry exists
165 1.2 cgd */
166 1.2 cgd extern int
167 1.1 cgd dbm_store(db, key, content, flags)
168 1.2 cgd DBM *db;
169 1.2 cgd datum key, content;
170 1.2 cgd int flags;
171 1.1 cgd {
172 1.2 cgd return ((db->put)(db, (DBT *)&key, (DBT *)&content,
173 1.2 cgd (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
174 1.1 cgd }
175 1.1 cgd
176 1.1 cgd extern int
177 1.1 cgd dbm_error(db)
178 1.2 cgd DBM *db;
179 1.1 cgd {
180 1.2 cgd HTAB *hp;
181 1.1 cgd
182 1.2 cgd hp = (HTAB *)db->internal;
183 1.2 cgd return (hp->errno);
184 1.1 cgd }
185 1.1 cgd
186 1.1 cgd extern int
187 1.1 cgd dbm_clearerr(db)
188 1.2 cgd DBM *db;
189 1.1 cgd {
190 1.2 cgd HTAB *hp;
191 1.2 cgd
192 1.2 cgd hp = (HTAB *)db->internal;
193 1.2 cgd hp->errno = 0;
194 1.2 cgd return (0);
195 1.2 cgd }
196 1.1 cgd
197 1.2 cgd extern int
198 1.2 cgd dbm_dirfno(db)
199 1.2 cgd DBM *db;
200 1.2 cgd {
201 1.2 cgd return(((HTAB *)db->internal)->fp);
202 1.1 cgd }
203