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