ndbmdatum.c revision 1.2 1 1.2 christos /* $NetBSD: ndbmdatum.c,v 1.2 2007/02/03 23:46: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.2 christos __RCSID("$NetBSD: ndbmdatum.c,v 1.2 2007/02/03 23:46: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.2 christos datum
65 1.2 christos dbm_fetch(DBM *db, datum key)
66 1.1 christos {
67 1.1 christos datum retdata;
68 1.1 christos int status;
69 1.1 christos DBT dbtkey, dbtretdata;
70 1.1 christos
71 1.1 christos dbtkey.data = key.dptr;
72 1.1 christos dbtkey.size = key.dsize;
73 1.1 christos status = (db->get)(db, &dbtkey, &dbtretdata, 0);
74 1.1 christos if (status) {
75 1.1 christos dbtretdata.data = NULL;
76 1.1 christos dbtretdata.size = 0;
77 1.1 christos }
78 1.1 christos retdata.dptr = dbtretdata.data;
79 1.1 christos retdata.dsize = dbtretdata.size;
80 1.1 christos return (retdata);
81 1.1 christos }
82 1.1 christos
83 1.1 christos /*
84 1.1 christos * Returns:
85 1.1 christos * DATUM on success
86 1.1 christos * NULL on failure
87 1.1 christos */
88 1.2 christos datum
89 1.2 christos dbm_firstkey(DBM *db)
90 1.1 christos {
91 1.1 christos int status;
92 1.1 christos datum retkey;
93 1.1 christos DBT dbtretkey, dbtretdata;
94 1.1 christos
95 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
96 1.1 christos if (status)
97 1.1 christos dbtretkey.data = NULL;
98 1.1 christos retkey.dptr = dbtretkey.data;
99 1.1 christos retkey.dsize = dbtretkey.size;
100 1.1 christos return (retkey);
101 1.1 christos }
102 1.1 christos
103 1.1 christos /*
104 1.1 christos * Returns:
105 1.1 christos * DATUM on success
106 1.1 christos * NULL on failure
107 1.1 christos */
108 1.2 christos datum
109 1.2 christos dbm_nextkey(DBM *db)
110 1.1 christos {
111 1.1 christos int status;
112 1.1 christos datum retkey;
113 1.1 christos DBT dbtretkey, dbtretdata;
114 1.1 christos
115 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
116 1.1 christos if (status)
117 1.1 christos dbtretkey.data = NULL;
118 1.1 christos retkey.dptr = dbtretkey.data;
119 1.1 christos retkey.dsize = dbtretkey.size;
120 1.1 christos return (retkey);
121 1.1 christos }
122 1.1 christos
123 1.1 christos /*
124 1.1 christos * Returns:
125 1.1 christos * 0 on success
126 1.1 christos * <0 failure
127 1.1 christos */
128 1.2 christos int
129 1.2 christos dbm_delete(DBM *db, datum key)
130 1.1 christos {
131 1.1 christos int status;
132 1.1 christos DBT dbtkey;
133 1.1 christos
134 1.1 christos dbtkey.data = key.dptr;
135 1.1 christos dbtkey.size = key.dsize;
136 1.1 christos status = (db->del)(db, &dbtkey, 0);
137 1.1 christos if (status)
138 1.1 christos return (-1);
139 1.1 christos else
140 1.1 christos return (0);
141 1.1 christos }
142 1.1 christos
143 1.1 christos /*
144 1.1 christos * Returns:
145 1.1 christos * 0 on success
146 1.1 christos * <0 failure
147 1.1 christos * 1 if DBM_INSERT and entry exists
148 1.1 christos */
149 1.2 christos int
150 1.2 christos dbm_store(DBM *db, datum key, datum data, int flags)
151 1.1 christos {
152 1.1 christos DBT dbtkey, dbtdata;
153 1.1 christos
154 1.1 christos dbtkey.data = key.dptr;
155 1.1 christos dbtkey.size = key.dsize;
156 1.1 christos dbtdata.data = data.dptr;
157 1.1 christos dbtdata.size = data.dsize;
158 1.1 christos return ((db->put)(db, &dbtkey, &dbtdata,
159 1.1 christos (u_int)((flags == DBM_INSERT) ? R_NOOVERWRITE : 0)));
160 1.1 christos }
161