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