ndbmdatum.c revision 1.1 1 1.1 christos /* $NetBSD: ndbmdatum.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */
2 1.1 christos /* from: NetBSD: ndbm.c,v 1.18 2004/04/27 20:03:45 kleink Exp */
3 1.1 christos
4 1.1 christos /*-
5 1.1 christos * Copyright (c) 1990, 1993
6 1.1 christos * The Regents of the University of California. All rights reserved.
7 1.1 christos *
8 1.1 christos * This code is derived from software contributed to Berkeley by
9 1.1 christos * Margo Seltzer.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer.
16 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 christos * notice, this list of conditions and the following disclaimer in the
18 1.1 christos * documentation and/or other materials provided with the distribution.
19 1.1 christos * 3. Neither the name of the University nor the names of its contributors
20 1.1 christos * may be used to endorse or promote products derived from this software
21 1.1 christos * without specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 christos * SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos #include <sys/cdefs.h>
37 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
38 1.1 christos #if 0
39 1.1 christos static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
40 1.1 christos #else
41 1.1 christos __RCSID("$NetBSD: ndbmdatum.c,v 1.1 2005/09/13 01:44:09 christos Exp $");
42 1.1 christos #endif
43 1.1 christos #endif /* LIBC_SCCS and not lint */
44 1.1 christos
45 1.1 christos /*
46 1.1 christos * This package provides a dbm compatible interface to the new hashing
47 1.1 christos * package described in db(3).
48 1.1 christos */
49 1.1 christos #include "namespace.h"
50 1.1 christos #include <sys/param.h>
51 1.1 christos
52 1.1 christos #include <fcntl.h>
53 1.1 christos #include <stdio.h>
54 1.1 christos #include <string.h>
55 1.1 christos
56 1.1 christos #include <ndbm.h>
57 1.1 christos #include "hash.h"
58 1.1 christos
59 1.1 christos /*
60 1.1 christos * Returns:
61 1.1 christos * DATUM on success
62 1.1 christos * NULL on failure
63 1.1 christos */
64 1.1 christos extern datum
65 1.1 christos dbm_fetch(db, key)
66 1.1 christos DBM *db;
67 1.1 christos datum key;
68 1.1 christos {
69 1.1 christos datum retdata;
70 1.1 christos int status;
71 1.1 christos DBT dbtkey, dbtretdata;
72 1.1 christos
73 1.1 christos dbtkey.data = key.dptr;
74 1.1 christos dbtkey.size = key.dsize;
75 1.1 christos status = (db->get)(db, &dbtkey, &dbtretdata, 0);
76 1.1 christos if (status) {
77 1.1 christos dbtretdata.data = NULL;
78 1.1 christos dbtretdata.size = 0;
79 1.1 christos }
80 1.1 christos retdata.dptr = dbtretdata.data;
81 1.1 christos retdata.dsize = dbtretdata.size;
82 1.1 christos return (retdata);
83 1.1 christos }
84 1.1 christos
85 1.1 christos /*
86 1.1 christos * Returns:
87 1.1 christos * DATUM on success
88 1.1 christos * NULL on failure
89 1.1 christos */
90 1.1 christos extern datum
91 1.1 christos dbm_firstkey(db)
92 1.1 christos DBM *db;
93 1.1 christos {
94 1.1 christos int status;
95 1.1 christos datum retkey;
96 1.1 christos DBT dbtretkey, dbtretdata;
97 1.1 christos
98 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
99 1.1 christos if (status)
100 1.1 christos dbtretkey.data = NULL;
101 1.1 christos retkey.dptr = dbtretkey.data;
102 1.1 christos retkey.dsize = dbtretkey.size;
103 1.1 christos return (retkey);
104 1.1 christos }
105 1.1 christos
106 1.1 christos /*
107 1.1 christos * Returns:
108 1.1 christos * DATUM on success
109 1.1 christos * NULL on failure
110 1.1 christos */
111 1.1 christos extern datum
112 1.1 christos dbm_nextkey(db)
113 1.1 christos DBM *db;
114 1.1 christos {
115 1.1 christos int status;
116 1.1 christos datum retkey;
117 1.1 christos DBT dbtretkey, dbtretdata;
118 1.1 christos
119 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
120 1.1 christos if (status)
121 1.1 christos dbtretkey.data = NULL;
122 1.1 christos retkey.dptr = dbtretkey.data;
123 1.1 christos retkey.dsize = dbtretkey.size;
124 1.1 christos return (retkey);
125 1.1 christos }
126 1.1 christos
127 1.1 christos /*
128 1.1 christos * Returns:
129 1.1 christos * 0 on success
130 1.1 christos * <0 failure
131 1.1 christos */
132 1.1 christos extern int
133 1.1 christos dbm_delete(db, key)
134 1.1 christos DBM *db;
135 1.1 christos datum key;
136 1.1 christos {
137 1.1 christos int status;
138 1.1 christos DBT dbtkey;
139 1.1 christos
140 1.1 christos dbtkey.data = key.dptr;
141 1.1 christos dbtkey.size = key.dsize;
142 1.1 christos status = (db->del)(db, &dbtkey, 0);
143 1.1 christos if (status)
144 1.1 christos return (-1);
145 1.1 christos else
146 1.1 christos return (0);
147 1.1 christos }
148 1.1 christos
149 1.1 christos /*
150 1.1 christos * Returns:
151 1.1 christos * 0 on success
152 1.1 christos * <0 failure
153 1.1 christos * 1 if DBM_INSERT and entry exists
154 1.1 christos */
155 1.1 christos extern int
156 1.1 christos dbm_store(db, key, data, flags)
157 1.1 christos DBM *db;
158 1.1 christos datum key, data;
159 1.1 christos int flags;
160 1.1 christos {
161 1.1 christos DBT dbtkey, dbtdata;
162 1.1 christos
163 1.1 christos dbtkey.data = key.dptr;
164 1.1 christos dbtkey.size = key.dsize;
165 1.1 christos dbtdata.data = data.dptr;
166 1.1 christos dbtdata.size = data.dsize;
167 1.1 christos return ((db->put)(db, &dbtkey, &dbtdata,
168 1.1 christos (u_int)((flags == DBM_INSERT) ? R_NOOVERWRITE : 0)));
169 1.1 christos }
170