db: avoid use of errno field name
[nvi.git] / db.1.85 / hash / ndbm.c
blob5963d3c10cc632967c41e4ebec6497282cbcbd49
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
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.
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.
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
39 #endif /* LIBC_SCCS and not lint */
42 * This package provides a dbm compatible interface to the new hashing
43 * package described in db(3).
46 #include <sys/param.h>
48 #include <stdio.h>
49 #include <string.h>
51 #include <ndbm.h>
52 #include "hash.h"
55 * Returns:
56 * *DBM on success
57 * NULL on failure
59 extern DBM *
60 dbm_open(file, flags, mode)
61 const char *file;
62 int flags, mode;
64 HASHINFO info;
65 char path[MAXPATHLEN];
67 info.bsize = 4096;
68 info.ffactor = 40;
69 info.nelem = 1;
70 info.cachesize = 0;
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));
78 extern void
79 dbm_close(db)
80 DBM *db;
82 (void)(db->close)(db);
86 * Returns:
87 * DATUM on success
88 * NULL on failure
90 extern datum
91 dbm_fetch(db, key)
92 DBM *db;
93 datum key;
95 datum retval;
96 int status;
98 status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
99 if (status) {
100 retval.dptr = NULL;
101 retval.dsize = 0;
103 return (retval);
107 * Returns:
108 * DATUM on success
109 * NULL on failure
111 extern datum
112 dbm_firstkey(db)
113 DBM *db;
115 int status;
116 datum retdata, retkey;
118 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
119 if (status)
120 retkey.dptr = NULL;
121 return (retkey);
125 * Returns:
126 * DATUM on success
127 * NULL on failure
129 extern datum
130 dbm_nextkey(db)
131 DBM *db;
133 int status;
134 datum retdata, retkey;
136 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
137 if (status)
138 retkey.dptr = NULL;
139 return (retkey);
142 * Returns:
143 * 0 on success
144 * <0 failure
146 extern int
147 dbm_delete(db, key)
148 DBM *db;
149 datum key;
151 int status;
153 status = (db->del)(db, (DBT *)&key, 0);
154 if (status)
155 return (-1);
156 else
157 return (0);
161 * Returns:
162 * 0 on success
163 * <0 failure
164 * 1 if DBM_INSERT and entry exists
166 extern int
167 dbm_store(db, key, content, flags)
168 DBM *db;
169 datum key, content;
170 int flags;
172 return ((db->put)(db, (DBT *)&key, (DBT *)&content,
173 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
176 extern int
177 dbm_error(db)
178 DBM *db;
180 HTAB *hp;
182 hp = (HTAB *)db->internal;
183 return (hp->_errno);
186 extern int
187 dbm_clearerr(db)
188 DBM *db;
190 HTAB *hp;
192 hp = (HTAB *)db->internal;
193 hp->_errno = 0;
194 return (0);
197 extern int
198 dbm_dirfno(db)
199 DBM *db;
201 return(((HTAB *)db->internal)->fp);