ndbmdatum.c revision 1.3 1 1.3 joerg /* $NetBSD: ndbmdatum.c,v 1.3 2008/09/10 17:52:35 joerg 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.3 joerg __RCSID("$NetBSD: ndbmdatum.c,v 1.3 2008/09/10 17:52:35 joerg Exp $");
38 1.1 christos
39 1.1 christos /*
40 1.1 christos * This package provides a dbm compatible interface to the new hashing
41 1.1 christos * package described in db(3).
42 1.1 christos */
43 1.1 christos #include "namespace.h"
44 1.1 christos #include <sys/param.h>
45 1.1 christos
46 1.1 christos #include <fcntl.h>
47 1.1 christos #include <stdio.h>
48 1.1 christos #include <string.h>
49 1.1 christos
50 1.1 christos #include <ndbm.h>
51 1.1 christos #include "hash.h"
52 1.1 christos
53 1.1 christos /*
54 1.1 christos * Returns:
55 1.1 christos * DATUM on success
56 1.1 christos * NULL on failure
57 1.1 christos */
58 1.2 christos datum
59 1.2 christos dbm_fetch(DBM *db, datum key)
60 1.1 christos {
61 1.1 christos datum retdata;
62 1.1 christos int status;
63 1.1 christos DBT dbtkey, dbtretdata;
64 1.1 christos
65 1.1 christos dbtkey.data = key.dptr;
66 1.1 christos dbtkey.size = key.dsize;
67 1.1 christos status = (db->get)(db, &dbtkey, &dbtretdata, 0);
68 1.1 christos if (status) {
69 1.1 christos dbtretdata.data = NULL;
70 1.1 christos dbtretdata.size = 0;
71 1.1 christos }
72 1.1 christos retdata.dptr = dbtretdata.data;
73 1.1 christos retdata.dsize = dbtretdata.size;
74 1.1 christos return (retdata);
75 1.1 christos }
76 1.1 christos
77 1.1 christos /*
78 1.1 christos * Returns:
79 1.1 christos * DATUM on success
80 1.1 christos * NULL on failure
81 1.1 christos */
82 1.2 christos datum
83 1.2 christos dbm_firstkey(DBM *db)
84 1.1 christos {
85 1.1 christos int status;
86 1.1 christos datum retkey;
87 1.1 christos DBT dbtretkey, dbtretdata;
88 1.1 christos
89 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
90 1.1 christos if (status)
91 1.1 christos dbtretkey.data = NULL;
92 1.1 christos retkey.dptr = dbtretkey.data;
93 1.1 christos retkey.dsize = dbtretkey.size;
94 1.1 christos return (retkey);
95 1.1 christos }
96 1.1 christos
97 1.1 christos /*
98 1.1 christos * Returns:
99 1.1 christos * DATUM on success
100 1.1 christos * NULL on failure
101 1.1 christos */
102 1.2 christos datum
103 1.2 christos dbm_nextkey(DBM *db)
104 1.1 christos {
105 1.1 christos int status;
106 1.1 christos datum retkey;
107 1.1 christos DBT dbtretkey, dbtretdata;
108 1.1 christos
109 1.1 christos status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
110 1.1 christos if (status)
111 1.1 christos dbtretkey.data = NULL;
112 1.1 christos retkey.dptr = dbtretkey.data;
113 1.1 christos retkey.dsize = dbtretkey.size;
114 1.1 christos return (retkey);
115 1.1 christos }
116 1.1 christos
117 1.1 christos /*
118 1.1 christos * Returns:
119 1.1 christos * 0 on success
120 1.1 christos * <0 failure
121 1.1 christos */
122 1.2 christos int
123 1.2 christos dbm_delete(DBM *db, datum key)
124 1.1 christos {
125 1.1 christos int status;
126 1.1 christos DBT dbtkey;
127 1.1 christos
128 1.1 christos dbtkey.data = key.dptr;
129 1.1 christos dbtkey.size = key.dsize;
130 1.1 christos status = (db->del)(db, &dbtkey, 0);
131 1.1 christos if (status)
132 1.1 christos return (-1);
133 1.1 christos else
134 1.1 christos return (0);
135 1.1 christos }
136 1.1 christos
137 1.1 christos /*
138 1.1 christos * Returns:
139 1.1 christos * 0 on success
140 1.1 christos * <0 failure
141 1.1 christos * 1 if DBM_INSERT and entry exists
142 1.1 christos */
143 1.2 christos int
144 1.2 christos dbm_store(DBM *db, datum key, datum data, int flags)
145 1.1 christos {
146 1.1 christos DBT dbtkey, dbtdata;
147 1.1 christos
148 1.1 christos dbtkey.data = key.dptr;
149 1.1 christos dbtkey.size = key.dsize;
150 1.1 christos dbtdata.data = data.dptr;
151 1.1 christos dbtdata.size = data.dsize;
152 1.1 christos return ((db->put)(db, &dbtkey, &dbtdata,
153 1.1 christos (u_int)((flags == DBM_INSERT) ? R_NOOVERWRITE : 0)));
154 1.1 christos }
155