xref: /dflybsd-src/lib/libc/db/hash/ndbm.c (revision ee61f228c1c38342493a01fe774b83cffc0f4be9)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#)ndbm.c	8.4 (Berkeley) 7/21/94
33  * $DragonFly: src/lib/libc/db/hash/ndbm.c,v 1.3 2005/09/19 09:20:37 asmodai Exp $
34  */
35 
36 /*
37  * This package provides a dbm compatible interface to the new hashing
38  * package described in db(3).
39  */
40 
41 #include <sys/param.h>
42 
43 #include <stdio.h>
44 #include <string.h>
45 #include <errno.h>
46 
47 #include <ndbm.h>
48 #include "hash.h"
49 
50 /*
51  * Returns:
52  * 	*DBM on success
53  *	 NULL on failure
54  */
55 extern DBM *
56 dbm_open(file, flags, mode)
57 	const char *file;
58 	int flags, mode;
59 {
60 	HASHINFO info;
61 	char path[MAXPATHLEN];
62 
63 	info.bsize = 4096;
64 	info.ffactor = 40;
65 	info.nelem = 1;
66 	info.cachesize = 0;
67 	info.hash = NULL;
68 	info.lorder = 0;
69 
70 	if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
71 		errno = ENAMETOOLONG;
72 		return(NULL);
73 	}
74 	(void)strcpy(path, file);
75 	(void)strcat(path, DBM_SUFFIX);
76 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
77 }
78 
79 extern void
80 dbm_close(db)
81 	DBM *db;
82 {
83 	(void)(db->close)(db);
84 }
85 
86 /*
87  * Returns:
88  *	DATUM on success
89  *	NULL on failure
90  */
91 extern datum
92 dbm_fetch(db, key)
93 	DBM *db;
94 	datum key;
95 {
96 	datum retdata;
97 	int status;
98 	DBT dbtkey, dbtretdata;
99 
100 	dbtkey.data = key.dptr;
101 	dbtkey.size = key.dsize;
102 	status = (db->get)(db, &dbtkey, &dbtretdata, 0);
103 	if (status) {
104 		dbtretdata.data = NULL;
105 		dbtretdata.size = 0;
106 	}
107 	retdata.dptr = dbtretdata.data;
108 	retdata.dsize = dbtretdata.size;
109 	return (retdata);
110 }
111 
112 /*
113  * Returns:
114  *	DATUM on success
115  *	NULL on failure
116  */
117 extern datum
118 dbm_firstkey(db)
119 	DBM *db;
120 {
121 	int status;
122 	datum retkey;
123 	DBT dbtretkey, dbtretdata;
124 
125 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
126 	if (status)
127 		dbtretkey.data = NULL;
128 	retkey.dptr = dbtretkey.data;
129 	retkey.dsize = dbtretkey.size;
130 	return (retkey);
131 }
132 
133 /*
134  * Returns:
135  *	DATUM on success
136  *	NULL on failure
137  */
138 extern datum
139 dbm_nextkey(db)
140 	DBM *db;
141 {
142 	int status;
143 	datum retkey;
144 	DBT dbtretkey, dbtretdata;
145 
146 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
147 	if (status)
148 		dbtretkey.data = NULL;
149 	retkey.dptr = dbtretkey.data;
150 	retkey.dsize = dbtretkey.size;
151 	return (retkey);
152 }
153 
154 /*
155  * Returns:
156  *	 0 on success
157  *	<0 failure
158  */
159 extern int
160 dbm_delete(db, key)
161 	DBM *db;
162 	datum key;
163 {
164 	int status;
165 	DBT dbtkey;
166 
167 	dbtkey.data = key.dptr;
168 	dbtkey.size = key.dsize;
169 	status = (db->del)(db, &dbtkey, 0);
170 	if (status)
171 		return (-1);
172 	else
173 		return (0);
174 }
175 
176 /*
177  * Returns:
178  *	 0 on success
179  *	<0 failure
180  *	 1 if DBM_INSERT and entry exists
181  */
182 extern int
183 dbm_store(db, key, data, flags)
184 	DBM *db;
185 	datum key, data;
186 	int flags;
187 {
188 	DBT dbtkey, dbtdata;
189 
190 	dbtkey.data = key.dptr;
191 	dbtkey.size = key.dsize;
192 	dbtdata.data = data.dptr;
193 	dbtdata.size = data.dsize;
194 	return ((db->put)(db, &dbtkey, &dbtdata,
195 	    (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
196 }
197 
198 extern int
199 dbm_error(db)
200 	DBM *db;
201 {
202 	HTAB *hp;
203 
204 	hp = (HTAB *)db->internal;
205 	return (hp->error);
206 }
207 
208 extern int
209 dbm_clearerr(db)
210 	DBM *db;
211 {
212 	HTAB *hp;
213 
214 	hp = (HTAB *)db->internal;
215 	hp->error = 0;
216 	return (0);
217 }
218 
219 extern int
220 dbm_dirfno(db)
221 	DBM *db;
222 {
223 	return(((HTAB *)db->internal)->fp);
224 }
225