Windows 10 SDK build fixes
[heimdal.git] / lib / roken / ndbm_wrap.c
blobf8403da44ab2639a5fcdb15b6696050bb376020c
1 /*
2 * Copyright (c) 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <config.h>
36 #include "ndbm_wrap.h"
37 #if defined(HAVE_DBHEADER)
38 #include <db.h>
39 #elif defined(HAVE_DB6_DB_H)
40 #include <db6/db.h>
41 #elif defined(HAVE_DB5_DB_H)
42 #include <db5/db.h>
43 #elif defined(HAVE_DB4_DB_H)
44 #include <db4/db.h>
45 #elif defined(HAVE_DB3_DB_H)
46 #include <db3/db.h>
47 #else
48 #include <db.h>
49 #endif
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <fcntl.h>
56 /* XXX undefine open so this works on Solaris with large file support */
57 #undef open
59 #define DBT2DATUM(DBT, DATUM) do { (DATUM)->dptr = (DBT)->data; (DATUM)->dsize = (DBT)->size; } while(0)
60 #define DATUM2DBT(DATUM, DBT) do { (DBT)->data = (DATUM)->dptr; (DBT)->size = (DATUM)->dsize; } while(0)
61 #define RETURN(X) return ((X) == 0) ? 0 : -1
63 #ifdef HAVE_DB3
64 static DBC *cursor;
65 #endif
67 #define D(X) ((DB*)(X))
69 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
70 dbm_close (DBM *db)
72 #ifdef HAVE_DB3
73 D(db)->close(D(db), 0);
74 cursor = NULL;
75 #else
76 D(db)->close(D(db));
77 #endif
80 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
81 dbm_delete (DBM *db, datum dkey)
83 DBT key;
84 DATUM2DBT(&dkey, &key);
85 #ifdef HAVE_DB3
86 RETURN(D(db)->del(D(db), NULL, &key, 0));
87 #else
88 RETURN(D(db)->del(D(db), &key, 0));
89 #endif
92 datum
93 dbm_fetch (DBM *db, datum dkey)
95 datum dvalue;
96 DBT key, value;
97 DATUM2DBT(&dkey, &key);
98 if(D(db)->get(D(db),
99 #ifdef HAVE_DB3
100 NULL,
101 #endif
102 &key, &value, 0) != 0) {
103 dvalue.dptr = NULL;
104 dvalue.dsize = 0;
106 else
107 DBT2DATUM(&value, &dvalue);
109 return dvalue;
112 static datum
113 dbm_get (DB *db, int flags)
115 DBT key, value;
116 datum d;
117 #ifdef HAVE_DB3
118 if(cursor == NULL)
119 db->cursor(db, NULL, &cursor, 0);
120 if(cursor->c_get(cursor, &key, &value, flags) != 0) {
121 d.dptr = NULL;
122 d.dsize = 0;
123 } else
124 DBT2DATUM(&value, &d);
125 #else
126 db->seq(db, &key, &value, flags);
127 DBT2DATUM(&value, &d);
128 #endif
129 return d;
132 #ifndef DB_FIRST
133 #define DB_FIRST R_FIRST
134 #define DB_NEXT R_NEXT
135 #define DB_NOOVERWRITE R_NOOVERWRITE
136 #define DB_KEYEXIST 1
137 #endif
139 ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
140 dbm_firstkey (DBM *db)
142 return dbm_get(D(db), DB_FIRST);
145 ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
146 dbm_nextkey (DBM *db)
148 return dbm_get(D(db), DB_NEXT);
151 ROKEN_LIB_FUNCTION DBM* ROKEN_LIB_CALL
152 dbm_open (const char *file, int flags, mode_t mode)
154 #ifdef HAVE_DB3
155 int myflags = 0;
156 #endif
157 DB *db;
158 char *fn = malloc(strlen(file) + 4);
159 if(fn == NULL)
160 return NULL;
161 strcpy(fn, file);
162 strcat(fn, ".db");
163 #ifdef HAVE_DB3
164 if (flags & O_CREAT)
165 myflags |= DB_CREATE;
167 if (flags & O_EXCL)
168 myflags |= DB_EXCL;
170 if (flags & O_RDONLY)
171 myflags |= DB_RDONLY;
173 if (flags & O_TRUNC)
174 myflags |= DB_TRUNCATE;
175 if(db_create(&db, NULL, 0) != 0) {
176 free(fn);
177 return NULL;
180 #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
181 if(db->open(db, NULL, fn, NULL, DB_BTREE, myflags, mode) != 0) {
182 #else
183 if(db->open(db, fn, NULL, DB_BTREE, myflags, mode) != 0) {
184 #endif
185 free(fn);
186 db->close(db, 0);
187 return NULL;
189 #else
190 db = dbopen(fn, flags, mode, DB_BTREE, NULL);
191 #endif
192 free(fn);
193 return (DBM*)db;
196 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
197 dbm_store (DBM *db, datum dkey, datum dvalue, int flags)
199 int ret;
200 DBT key, value;
201 int myflags = 0;
202 if((flags & DBM_REPLACE) == 0)
203 myflags |= DB_NOOVERWRITE;
204 DATUM2DBT(&dkey, &key);
205 DATUM2DBT(&dvalue, &value);
206 ret = D(db)->put(D(db),
207 #ifdef HAVE_DB3
208 NULL,
209 #endif
210 &key, &value, myflags);
211 if(ret == DB_KEYEXIST)
212 return 1;
213 RETURN(ret);
216 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
217 dbm_error (DBM *db)
219 return 0;
222 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
223 dbm_clearerr (DBM *db)
225 return 0;