xref: /original-bsd/lib/libc/db/hash/ndbm.c (revision 9a765c18)
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.4 (Berkeley) 02/22/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.ncached = 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 );
69     if ( status ) {
70 	retval.dptr = NULL;
71     }
72     return(retval);
73 }
74 
75 /*
76     Returns 	DATUM on success
77 		NULL on failure
78 */
79 extern datum
80 dbm_firstkey(db)
81 DBM 	*db;
82 {
83     int	status;
84     datum	retkey;
85     datum	retdata;
86 
87     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST );
88     if ( status ) {
89 	retkey.dptr = NULL;
90     }
91     return(retkey);
92 }
93 /*
94     Returns 	DATUM on success
95 		NULL on failure
96 */
97 extern datum
98 dbm_nextkey(db)
99 DBM 	*db;
100 {
101     int	status;
102     datum	retkey;
103     datum	retdata;
104 
105     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT );
106     if ( status ) {
107 	retkey.dptr = NULL;
108     }
109     return(retkey);
110 }
111 
112 /*
113     0 on success
114     <0 failure
115 */
116 extern int
117 dbm_delete(db, key)
118 DBM 	*db;
119 datum	key;
120 {
121     int	status;
122 
123     status = (db->del)( db, (DBT *)&key );
124     if ( status ) {
125 	return(-1);
126     } else {
127 	return(0);
128     }
129 }
130 
131 /*
132     0 on success
133     <0 failure
134     1 if DBM_INSERT and entry exists
135 */
136 extern int
137 dbm_store(db, key, content, flags)
138 DBM 	*db;
139 datum	key;
140 datum	content;
141 int	flags;
142 {
143     return ((db->put)( db, (DBT *)&key, (DBT *)&content,
144 			(flags == DBM_INSERT) ? R_NOOVERWRITE : 0 ));
145 }
146 
147 extern int
148 dbm_error(db)
149 DBM	*db;
150 {
151     HTAB	*hp;
152 
153     hp = (HTAB *)db->internal;
154     return ( hp->errno );
155 }
156 
157 extern int
158 dbm_clearerr(db)
159 DBM	*db;
160 {
161     HTAB	*hp;
162 
163     hp = (HTAB *)db->internal;
164     hp->errno = 0;
165     return ( 0 );
166 }
167