Update.
[glibc.git] / db / hash / ndbm.c
blob83aa766c389e7129edccd98e30d9e1f0674c26c8
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>
50 #include <stdlib.h>
52 #include <ndbm.h>
53 #include "hash.h"
56 * Returns:
57 * *DBM on success
58 * NULL on failure
60 extern DBM *
61 dbm_open(file, flags, mode)
62 const char *file;
63 int flags, mode;
65 DBM *db;
66 HASHINFO info;
67 const size_t len = strlen(file) + sizeof (DBM_SUFFIX);
68 #ifdef __GNUC__
69 char path[len];
70 #else
71 char *path = malloc(len);
72 if (path == NULL)
73 return NULL;
74 #endif
76 info.bsize = 4096;
77 info.ffactor = 40;
78 info.nelem = 1;
79 info.cachesize = 0;
80 info.hash = NULL;
81 info.lorder = 0;
82 (void)strcpy(path, file);
83 (void)strcat(path, DBM_SUFFIX);
84 db = (DBM *)__hash_open(path, flags, mode, &info, 0);
85 #ifndef __GNUC__
86 free(path);
87 #endif
88 return db;
91 extern void
92 dbm_close(db)
93 DBM *db;
95 (void)(db->close)(db);
99 * Returns:
100 * DATUM on success
101 * NULL on failure
103 extern datum
104 dbm_fetch(db, key)
105 DBM *db;
106 datum key;
108 datum retval;
109 int status;
111 status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
112 if (status) {
113 retval.dptr = NULL;
114 retval.dsize = 0;
116 return (retval);
120 * Returns:
121 * DATUM on success
122 * NULL on failure
124 extern datum
125 dbm_firstkey(db)
126 DBM *db;
128 int status;
129 datum retdata, retkey;
131 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
132 if (status)
133 retkey.dptr = NULL;
134 return (retkey);
138 * Returns:
139 * DATUM on success
140 * NULL on failure
142 extern datum
143 dbm_nextkey(db)
144 DBM *db;
146 int status;
147 datum retdata, retkey;
149 status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
150 if (status)
151 retkey.dptr = NULL;
152 return (retkey);
155 * Returns:
156 * 0 on success
157 * <0 failure
159 extern int
160 dbm_delete(db, key)
161 DBM *db;
162 datum key;
164 int status;
166 status = (db->del)(db, (DBT *)&key, 0);
167 if (status)
168 return (-1);
169 else
170 return (0);
174 * Returns:
175 * 0 on success
176 * <0 failure
177 * 1 if DBM_INSERT and entry exists
179 extern int
180 dbm_store(db, key, content, flags)
181 DBM *db;
182 datum key, content;
183 int flags;
185 return ((db->put)(db, (DBT *)&key, (DBT *)&content,
186 (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
189 extern int
190 dbm_error(db)
191 DBM *db;
193 HTAB *hp;
195 hp = (HTAB *)db->internal;
196 return (hp->errnum);
199 extern int
200 dbm_clearerr(db)
201 DBM *db;
203 HTAB *hp;
205 hp = (HTAB *)db->internal;
206 hp->errnum = 0;
207 return (0);
210 extern int
211 dbm_dirfno(db)
212 DBM *db;
214 return(((HTAB *)db->internal)->fp);