xref: /original-bsd/lib/libc/db/hash/ndbm.c (revision 95a66346)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)ndbm.c	5.6 (Berkeley) 03/12/91";
13 #endif /* LIBC_SCCS and not lint */
14 
15 /*
16     This package provides a dbm compatible interface to the new hashing
17     package described in db(3)
18 */
19 
20 #include <sys/param.h>
21 #include <ndbm.h>
22 #include <db.h>
23 #include <stdio.h>
24 #include "hash.h"
25 
26 /*
27     return 	*DBM on success
28 		NULL on failure
29 */
30 extern DBM *
31 dbm_open( file, flags, mode )
32 const char 	*file;
33 int	flags;
34 int	mode;
35 {
36     HASHINFO	info;
37     char path[MAXPATHLEN];
38 
39     info.bsize = 1024;
40     info.ffactor = 5;
41     info.nelem = 1;
42     info.cachesize = NULL;
43     info.hash = NULL;
44     info.lorder = 0;
45     (void)sprintf(path, "%s%s", file, DBM_SUFFIX);
46     return( hash_open ( path, flags, mode, &info ) );
47 }
48 
49 extern void
50 dbm_close(db)
51 DBM	*db;
52 {
53     (void)(db->close) (db);
54 }
55 
56 /*
57     Returns 	DATUM on success
58 		NULL on failure
59 */
60 extern datum
61 dbm_fetch( db, key )
62 DBM 	*db;
63 datum	key;
64 {
65     int	status;
66     datum	retval;
67 
68     status = (db->get) ( db, (DBT *)&key, (DBT *)&retval, 0 );
69     if ( status ) {
70 	retval.dptr = NULL;
71 	retval.dsize = 0;
72     }
73     return(retval);
74 }
75 
76 /*
77     Returns 	DATUM on success
78 		NULL on failure
79 */
80 extern datum
81 dbm_firstkey(db)
82 DBM 	*db;
83 {
84     int	status;
85     datum	retkey;
86     datum	retdata;
87 
88     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST );
89     if ( status ) {
90 	retkey.dptr = NULL;
91     }
92     return(retkey);
93 }
94 /*
95     Returns 	DATUM on success
96 		NULL on failure
97 */
98 extern datum
99 dbm_nextkey(db)
100 DBM 	*db;
101 {
102     int	status;
103     datum	retkey;
104     datum	retdata;
105 
106     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT );
107     if ( status ) {
108 	retkey.dptr = NULL;
109     }
110     return(retkey);
111 }
112 
113 /*
114     0 on success
115     <0 failure
116 */
117 extern int
118 dbm_delete(db, key)
119 DBM 	*db;
120 datum	key;
121 {
122     int	status;
123 
124     status = (db->del)( db, (DBT *)&key, 0 );
125     if ( status ) {
126 	return(-1);
127     } else {
128 	return(0);
129     }
130 }
131 
132 /*
133     0 on success
134     <0 failure
135     1 if DBM_INSERT and entry exists
136 */
137 extern int
138 dbm_store(db, key, content, flags)
139 DBM 	*db;
140 datum	key;
141 datum	content;
142 int	flags;
143 {
144     return ((db->put)( db, (DBT *)&key, (DBT *)&content,
145 			(flags == DBM_INSERT) ? R_NOOVERWRITE : 0 ));
146 }
147 
148 extern int
149 dbm_error(db)
150 DBM	*db;
151 {
152     HTAB	*hp;
153 
154     hp = (HTAB *)db->internal;
155     return ( hp->errno );
156 }
157 
158 extern int
159 dbm_clearerr(db)
160 DBM	*db;
161 {
162     HTAB	*hp;
163 
164     hp = (HTAB *)db->internal;
165     hp->errno = 0;
166     return ( 0 );
167 }
168