Asterisk - The Open Source Telephony Project  18.5.0
ndbm.c
Go to the documentation of this file.
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. All advertising materials mentioning features or use of this software
17  * must display the following acknowledgement:
18  * This product includes software developed by the University of
19  * California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  * may be used to endorse or promote products derived from this software
22  * without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";
39 #endif /* LIBC_SCCS and not lint */
40 
41 /*
42  * This package provides a dbm compatible interface to the new hashing
43  * package described in db(3).
44  */
45 
46 #include <sys/param.h>
47 
48 #include <stdio.h>
49 #include <string.h>
50 #include <stdlib.h>
51 
52 #include <ndbm.h>
53 #include "hash.h"
54 
55 /*
56  * Returns:
57  * *DBM on success
58  * NULL on failure
59  */
60 extern DBM *
61 dbm_open(file, flags, mode)
62  const char *file;
63  int flags, mode;
64 {
65  DBM *db;
66  HASHINFO info;
67  const size_t len = strlen(file) + sizeof (DBM_SUFFIX);
68 #ifdef __GNUC__
69  char path[len];
70 #else
71  char *path = malloc(len);
72  if (path == NULL)
73  return NULL;
74 #endif
75 
76  info.bsize = 4096;
77  info.ffactor = 40;
78  info.nelem = 1;
79  info.cachesize = 0;
80  info.hash = NULL;
81  info.lorder = 0;
82  snprintf(path, len, "%s%s", file, DBM_SUFFIX);
83  db = (DBM *)__hash_open(path, flags, mode, &info, 0);
84 #ifndef __GNUC__
85  free(path);
86 #endif
87  return db;
88 }
89 
90 extern void
92  DBM *db;
93 {
94  (void)(db->close)(db);
95 }
96 
97 /*
98  * Returns:
99  * DATUM on success
100  * NULL on failure
101  */
102 extern datum
103 dbm_fetch(db, key)
104  DBM *db;
105  datum key;
106 {
107  datum retdata;
108  int status;
109  DBT dbtkey, dbtretdata;
110 
111  dbtkey.data = key.dptr;
112  dbtkey.size = key.dsize;
113  status = (db->get)(db, &dbtkey, &dbtretdata, 0);
114  if (status) {
115  dbtretdata.data = NULL;
116  dbtretdata.size = 0;
117  }
118  retdata.dptr = dbtretdata.data;
119  retdata.dsize = dbtretdata.size;
120  return (retdata);
121 }
122 
123 /*
124  * Returns:
125  * DATUM on success
126  * NULL on failure
127  */
128 extern datum
130  DBM *db;
131 {
132  int status;
133  datum retkey;
134  DBT dbtretkey, dbtretdata;
135 
136  status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);
137  if (status)
138  dbtretkey.data = NULL;
139  retkey.dptr = dbtretkey.data;
140  retkey.dsize = dbtretkey.size;
141  return (retkey);
142 }
143 
144 /*
145  * Returns:
146  * DATUM on success
147  * NULL on failure
148  */
149 extern datum
151  DBM *db;
152 {
153  int status;
154  datum retkey;
155  DBT dbtretkey, dbtretdata;
156 
157  status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);
158  if (status)
159  dbtretkey.data = NULL;
160  retkey.dptr = dbtretkey.data;
161  retkey.dsize = dbtretkey.size;
162  return (retkey);
163 }
164 /*
165  * Returns:
166  * 0 on success
167  * <0 failure
168  */
169 extern int
170 dbm_delete(db, key)
171  DBM *db;
172  datum key;
173 {
174  int status;
175  DBT dbtkey;
176 
177  dbtkey.data = key.dptr;
178  dbtkey.size = key.dsize;
179  status = (db->del)(db, &dbtkey, 0);
180  if (status)
181  return (-1);
182  else
183  return (0);
184 }
185 
186 /*
187  * Returns:
188  * 0 on success
189  * <0 failure
190  * 1 if DBM_INSERT and entry exists
191  */
192 extern int
193 dbm_store(db, key, data, flags)
194  DBM *db;
195  datum key, data;
196  int flags;
197 {
198  DBT dbtkey, dbtdata;
199 
200  dbtkey.data = key.dptr;
201  dbtkey.size = key.dsize;
202  dbtdata.data = data.dptr;
203  dbtdata.size = data.dsize;
204  return ((db->put)(db, &dbtkey, &dbtdata,
205  (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
206 }
207 
208 extern int
210  DBM *db;
211 {
212  HTAB *hp;
213 
214  hp = (HTAB *)db->internal;
215  return (hp->errnum);
216 }
217 
218 extern int
220  DBM *db;
221 {
222  HTAB *hp;
223 
224  hp = (HTAB *)db->internal;
225  hp->errnum = 0;
226  return (0);
227 }
228 
229 extern int
231  DBM *db;
232 {
233  return(((HTAB *)db->internal)->fp);
234 }
void dbm_close(DBM *db)
Definition: ndbm.c:91
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
int lorder
Definition: db.h:170
u_int cachesize
Definition: db.h:167
int errnum
Definition: hash.h:106
DB * __hash_open(char *file, int flags, int mode, const HASHINFO *info, int dflags) const
Definition: hash.c:96
static sqlite3 * db
#define DBM_INSERT
Definition: ndbm.h:48
void * internal
Definition: db.h:137
Definition: db.h:85
char * dptr
Definition: ndbm.h:58
#define NULL
Definition: resample.c:96
Definition: hash.h:92
Definition: db.h:163
Definition: ndbm.h:57
char * malloc()
void free()
datum dbm_firstkey(DBM *db)
Definition: ndbm.c:129
int dsize
Definition: ndbm.h:59
#define DBM_SUFFIX
Definition: ndbm.h:55
int dbm_store(DBM *db, datum key, datum data, int flags)
Definition: ndbm.c:193
DBM * dbm_open(char *file, int flags, int mode) const
Definition: ndbm.c:61
def info(msg)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define R_NEXT
Definition: db.h:97
u_int nelem
Definition: db.h:166
int dbm_error(DBM *db)
Definition: ndbm.c:209
int dbm_dirfno(DBM *db)
Definition: ndbm.c:230
#define R_FIRST
Definition: db.h:93
int dbm_clearerr(DBM *db)
Definition: ndbm.c:219
#define R_NOOVERWRITE
Definition: db.h:98
datum dbm_fetch(DBM *db, datum key)
Definition: ndbm.c:103
Definition: db.h:129
datum dbm_nextkey(DBM *db)
Definition: ndbm.c:150
u_int ffactor
Definition: db.h:165
static struct hostent * hp
Definition: chan_skinny.c:1236
int dbm_delete(DBM *db, datum key)
Definition: ndbm.c:170
jack_status_t status
Definition: app_jack.c:146
u_int bsize
Definition: db.h:164