xref: /freebsd/lib/libc/db/hash/ndbm.c (revision 9034852c84a13f0e3b5527e1c886ca94b2863b2b)
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  * 4. 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 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)ndbm.c	8.4 (Berkeley) 7/21/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 /*
40  * This package provides a dbm compatible interface to the new hashing
41  * package described in db(3).
42  */
43 
44 #include <sys/param.h>
45 
46 #include <stdio.h>
47 #include <string.h>
48 #include <errno.h>
49 
50 #include <ndbm.h>
51 #include "hash.h"
52 
53 /*
54  * Returns:
55  * 	*DBM on success
56  *	 NULL on failure
57  */
58 extern DBM *
59 dbm_open(const char *file, int flags, int mode)
60 {
61 	HASHINFO info;
62 	char path[MAXPATHLEN];
63 
64 	info.bsize = 4096;
65 	info.ffactor = 40;
66 	info.nelem = 1;
67 	info.cachesize = 0;
68 	info.hash = NULL;
69 	info.lorder = 0;
70 
71 	if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
72 		errno = ENAMETOOLONG;
73 		return(NULL);
74 	}
75 	(void)strcpy(path, file);
76 	(void)strcat(path, DBM_SUFFIX);
77 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
78 }
79 
80 extern void
81 dbm_close(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(DBM *db, datum key)
93 {
94 	datum retdata;
95 	int status;
96 	DBT dbtkey, dbtretdata;
97 
98 	dbtkey.data = key.dptr;
99 	dbtkey.size = key.dsize;
100 	status = (db->get)(db, &dbtkey, &dbtretdata, 0);
101 	if (status) {
102 		dbtretdata.data = NULL;
103 		dbtretdata.size = 0;
104 	}
105 	retdata.dptr = dbtretdata.data;
106 	retdata.dsize = dbtretdata.size;
107 	return (retdata);
108 }
109 
110 /*
111  * Returns:
112  *	DATUM on success
113  *	NULL on failure
114  */
115 extern datum
116 dbm_firstkey(DBM *db)
117 {
118 	int status;
119 	datum retkey;
120 	DBT dbtretkey, dbtretdata;
121 
122 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
123 	if (status)
124 		dbtretkey.data = NULL;
125 	retkey.dptr = dbtretkey.data;
126 	retkey.dsize = dbtretkey.size;
127 	return (retkey);
128 }
129 
130 /*
131  * Returns:
132  *	DATUM on success
133  *	NULL on failure
134  */
135 extern datum
136 dbm_nextkey(DBM *db)
137 {
138 	int status;
139 	datum retkey;
140 	DBT dbtretkey, dbtretdata;
141 
142 	status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
143 	if (status)
144 		dbtretkey.data = NULL;
145 	retkey.dptr = dbtretkey.data;
146 	retkey.dsize = dbtretkey.size;
147 	return (retkey);
148 }
149 
150 /*
151  * Returns:
152  *	 0 on success
153  *	<0 failure
154  */
155 extern int
156 dbm_delete(DBM *db, datum key)
157 {
158 	int status;
159 	DBT dbtkey;
160 
161 	dbtkey.data = key.dptr;
162 	dbtkey.size = key.dsize;
163 	status = (db->del)(db, &dbtkey, 0);
164 	if (status)
165 		return (-1);
166 	else
167 		return (0);
168 }
169 
170 /*
171  * Returns:
172  *	 0 on success
173  *	<0 failure
174  *	 1 if DBM_INSERT and entry exists
175  */
176 extern int
177 dbm_store(DBM *db, datum key, datum data, int flags)
178 {
179 	DBT dbtkey, dbtdata;
180 
181 	dbtkey.data = key.dptr;
182 	dbtkey.size = key.dsize;
183 	dbtdata.data = data.dptr;
184 	dbtdata.size = data.dsize;
185 	return ((db->put)(db, &dbtkey, &dbtdata,
186 	    (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
187 }
188 
189 extern int
190 dbm_error(DBM *db)
191 {
192 	HTAB *hp;
193 
194 	hp = (HTAB *)db->internal;
195 	return (hp->error);
196 }
197 
198 extern int
199 dbm_clearerr(DBM *db)
200 {
201 	HTAB *hp;
202 
203 	hp = (HTAB *)db->internal;
204 	hp->error = 0;
205 	return (0);
206 }
207 
208 extern int
209 dbm_dirfno(DBM *db)
210 {
211 	return(((HTAB *)db->internal)->fp);
212 }
213