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