libc/db: Sync with FreeBSD
[dragonfly.git] / lib / libc / db / hash / ndbm.c
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    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.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#)ndbm.c   8.4 (Berkeley) 7/21/94
33  * $FreeBSD: head/lib/libc/db/hash/ndbm.c 165903 2007-01-09 00:28:16Z imp $
34  */
35
36 /*
37  * This package provides a dbm compatible interface to the new hashing
38  * package described in db(3).
39  */
40
41 #include <sys/param.h>
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <errno.h>
46
47 #include <ndbm.h>
48 #include "hash.h"
49
50 /*
51  * Returns:
52  *      *DBM on success
53  *       NULL on failure
54  */
55 DBM *
56 dbm_open(const char *file, int flags, mode_t mode)
57 {
58         HASHINFO info;
59         char path[MAXPATHLEN];
60
61         info.bsize = 4096;
62         info.ffactor = 40;
63         info.nelem = 1;
64         info.cachesize = 0;
65         info.hash = NULL;
66         info.lorder = 0;
67
68         if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {
69                 errno = ENAMETOOLONG;
70                 return(NULL);
71         }
72         strcpy(path, file);
73         strcat(path, DBM_SUFFIX);
74         return ((DBM *)__hash_open(path, flags, mode, &info, 0));
75 }
76
77 void
78 dbm_close(DBM *db)
79 {
80         (db->close)(db);
81 }
82
83 /*
84  * Returns:
85  *      DATUM on success
86  *      NULL on failure
87  */
88 datum
89 dbm_fetch(DBM *db, datum key)
90 {
91         datum retdata;
92         int status;
93         DBT dbtkey, dbtretdata;
94
95         dbtkey.data = key.dptr;
96         dbtkey.size = key.dsize;
97         status = (db->get)(db, &dbtkey, &dbtretdata, 0);
98         if (status) {
99                 dbtretdata.data = NULL;
100                 dbtretdata.size = 0;
101         }
102         retdata.dptr = dbtretdata.data;
103         retdata.dsize = dbtretdata.size;
104         return (retdata);
105 }
106
107 /*
108  * Returns:
109  *      DATUM on success
110  *      NULL on failure
111  */
112 datum
113 dbm_firstkey(DBM *db)
114 {
115         int status;
116         datum retkey;
117         DBT dbtretkey, dbtretdata;
118
119         status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
120         if (status)
121                 dbtretkey.data = NULL;
122         retkey.dptr = dbtretkey.data;
123         retkey.dsize = dbtretkey.size;
124         return (retkey);
125 }
126
127 /*
128  * Returns:
129  *      DATUM on success
130  *      NULL on failure
131  */
132 datum
133 dbm_nextkey(DBM *db)
134 {
135         int status;
136         datum retkey;
137         DBT dbtretkey, dbtretdata;
138
139         status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
140         if (status)
141                 dbtretkey.data = NULL;
142         retkey.dptr = dbtretkey.data;
143         retkey.dsize = dbtretkey.size;
144         return (retkey);
145 }
146
147 /*
148  * Returns:
149  *       0 on success
150  *      <0 failure
151  */
152 int
153 dbm_delete(DBM *db, datum key)
154 {
155         int status;
156         DBT dbtkey;
157
158         dbtkey.data = key.dptr;
159         dbtkey.size = key.dsize;
160         status = (db->del)(db, &dbtkey, 0);
161         if (status)
162                 return (-1);
163         else
164                 return (0);
165 }
166
167 /*
168  * Returns:
169  *       0 on success
170  *      <0 failure
171  *       1 if DBM_INSERT and entry exists
172  */
173 int
174 dbm_store(DBM *db, datum key, datum data, int flags)
175 {
176         DBT dbtkey, dbtdata;
177
178         dbtkey.data = key.dptr;
179         dbtkey.size = key.dsize;
180         dbtdata.data = data.dptr;
181         dbtdata.size = data.dsize;
182         return ((db->put)(db, &dbtkey, &dbtdata,
183             (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
184 }
185
186 int
187 dbm_error(DBM *db)
188 {
189         HTAB *hp;
190
191         hp = (HTAB *)db->internal;
192         return (hp->error);
193 }
194
195 int
196 dbm_clearerr(DBM *db)
197 {
198         HTAB *hp;
199
200         hp = (HTAB *)db->internal;
201         hp->error = 0;
202         return (0);
203 }
204
205 int
206 dbm_dirfno(DBM *db)
207 {
208         return(((HTAB *)db->internal)->fp);
209 }