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