Make roken build on windows
[heimdal.git] / lib / roken / ndbm_wrap.c
blob4fbdb4da0f7b57c3ad4e29872259f94f68562c50
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_DB4_DB_H)
38 #include <db4/db.h>
39 #elif defined(HAVE_DB3_DB_H)
40 #include <db3/db.h>
41 #else
42 #include <db.h>
43 #endif
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <fcntl.h>
50 /* XXX undefine open so this works on Solaris with large file support */
51 #undef open
53 #define DBT2DATUM(DBT, DATUM) do { (DATUM)->dptr = (DBT)->data; (DATUM)->dsize = (DBT)->size; } while(0)
54 #define DATUM2DBT(DATUM, DBT) do { (DBT)->data = (DATUM)->dptr; (DBT)->size = (DATUM)->dsize; } while(0)
55 #define RETURN(X) return ((X) == 0) ? 0 : -1
57 #ifdef HAVE_DB3
58 static DBC *cursor;
59 #endif
61 #define D(X) ((DB*)(X))
63 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
64 dbm_close (DBM *db)
66 #ifdef HAVE_DB3
67 D(db)->close(D(db), 0);
68 cursor = NULL;
69 #else
70 D(db)->close(D(db));
71 #endif
74 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
75 dbm_delete (DBM *db, datum dkey)
77 DBT key;
78 DATUM2DBT(&dkey, &key);
79 #ifdef HAVE_DB3
80 RETURN(D(db)->del(D(db), NULL, &key, 0));
81 #else
82 RETURN(D(db)->del(D(db), &key, 0));
83 #endif
86 datum
87 dbm_fetch (DBM *db, datum dkey)
89 datum dvalue;
90 DBT key, value;
91 DATUM2DBT(&dkey, &key);
92 if(D(db)->get(D(db),
93 #ifdef HAVE_DB3
94 NULL,
95 #endif
96 &key, &value, 0) != 0) {
97 dvalue.dptr = NULL;
98 dvalue.dsize = 0;
100 else
101 DBT2DATUM(&value, &dvalue);
103 return dvalue;
106 static datum
107 dbm_get (DB *db, int flags)
109 DBT key, value;
110 datum datum;
111 #ifdef HAVE_DB3
112 if(cursor == NULL)
113 db->cursor(db, NULL, &cursor, 0);
114 if(cursor->c_get(cursor, &key, &value, flags) != 0) {
115 datum.dptr = NULL;
116 datum.dsize = 0;
117 } else
118 DBT2DATUM(&value, &datum);
119 #else
120 db->seq(db, &key, &value, flags);
121 #endif
122 return datum;
125 #ifndef DB_FIRST
126 #define DB_FIRST R_FIRST
127 #define DB_NEXT R_NEXT
128 #define DB_NOOVERWRITE R_NOOVERWRITE
129 #define DB_KEYEXIST 1
130 #endif
132 ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
133 dbm_firstkey (DBM *db)
135 return dbm_get(D(db), DB_FIRST);
138 ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
139 dbm_nextkey (DBM *db)
141 return dbm_get(D(db), DB_NEXT);
144 ROKEN_LIB_FUNCTION DBM* ROKEN_LIB_CALL
145 dbm_open (const char *file, int flags, mode_t mode)
147 DB *db;
148 int myflags = 0;
149 char *fn = malloc(strlen(file) + 4);
150 if(fn == NULL)
151 return NULL;
152 strcpy(fn, file);
153 strcat(fn, ".db");
154 #ifdef HAVE_DB3
155 if (flags & O_CREAT)
156 myflags |= DB_CREATE;
158 if (flags & O_EXCL)
159 myflags |= DB_EXCL;
161 if (flags & O_RDONLY)
162 myflags |= DB_RDONLY;
164 if (flags & O_TRUNC)
165 myflags |= DB_TRUNCATE;
166 if(db_create(&db, NULL, 0) != 0) {
167 free(fn);
168 return NULL;
171 #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
172 if(db->open(db, NULL, fn, NULL, DB_BTREE, myflags, mode) != 0) {
173 #else
174 if(db->open(db, fn, NULL, DB_BTREE, myflags, mode) != 0) {
175 #endif
176 free(fn);
177 db->close(db, 0);
178 return NULL;
180 #else
181 db = dbopen(fn, flags, mode, DB_BTREE, NULL);
182 #endif
183 free(fn);
184 return (DBM*)db;
187 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
188 dbm_store (DBM *db, datum dkey, datum dvalue, int flags)
190 int ret;
191 DBT key, value;
192 int myflags = 0;
193 if((flags & DBM_REPLACE) == 0)
194 myflags |= DB_NOOVERWRITE;
195 DATUM2DBT(&dkey, &key);
196 DATUM2DBT(&dvalue, &value);
197 ret = D(db)->put(D(db),
198 #ifdef HAVE_DB3
199 NULL,
200 #endif
201 &key, &value, myflags);
202 if(ret == DB_KEYEXIST)
203 return 1;
204 RETURN(ret);
207 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
208 dbm_error (DBM *db)
210 return 0;
213 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
214 dbm_clearerr (DBM *db)
216 return 0;