xref: /original-bsd/lib/libc/db/hash/ndbm.c (revision d54be081)
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.7 (Berkeley) 06/17/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)strcpy(path, file);
46     (void)strcat(path, DBM_SUFFIX);
47     return( hash_open ( path, flags, mode, &info ) );
48 }
49 
50 extern void
51 dbm_close(db)
52 DBM	*db;
53 {
54     (void)(db->close) (db);
55 }
56 
57 /*
58     Returns 	DATUM on success
59 		NULL on failure
60 */
61 extern datum
62 dbm_fetch( db, key )
63 DBM 	*db;
64 datum	key;
65 {
66     int	status;
67     datum	retval;
68 
69     status = (db->get) ( db, (DBT *)&key, (DBT *)&retval, 0 );
70     if ( status ) {
71 	retval.dptr = NULL;
72 	retval.dsize = 0;
73     }
74     return(retval);
75 }
76 
77 /*
78     Returns 	DATUM on success
79 		NULL on failure
80 */
81 extern datum
82 dbm_firstkey(db)
83 DBM 	*db;
84 {
85     int	status;
86     datum	retkey;
87     datum	retdata;
88 
89     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST );
90     if ( status ) {
91 	retkey.dptr = NULL;
92     }
93     return(retkey);
94 }
95 /*
96     Returns 	DATUM on success
97 		NULL on failure
98 */
99 extern datum
100 dbm_nextkey(db)
101 DBM 	*db;
102 {
103     int	status;
104     datum	retkey;
105     datum	retdata;
106 
107     status = (db->seq) ( db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT );
108     if ( status ) {
109 	retkey.dptr = NULL;
110     }
111     return(retkey);
112 }
113 
114 /*
115     0 on success
116     <0 failure
117 */
118 extern int
119 dbm_delete(db, key)
120 DBM 	*db;
121 datum	key;
122 {
123     int	status;
124 
125     status = (db->del)( db, (DBT *)&key, 0 );
126     if ( status ) {
127 	return(-1);
128     } else {
129 	return(0);
130     }
131 }
132 
133 /*
134     0 on success
135     <0 failure
136     1 if DBM_INSERT and entry exists
137 */
138 extern int
139 dbm_store(db, key, content, flags)
140 DBM 	*db;
141 datum	key;
142 datum	content;
143 int	flags;
144 {
145     return ((db->put)( db, (DBT *)&key, (DBT *)&content,
146 			(flags == DBM_INSERT) ? R_NOOVERWRITE : 0 ));
147 }
148 
149 extern int
150 dbm_error(db)
151 DBM	*db;
152 {
153     HTAB	*hp;
154 
155     hp = (HTAB *)db->internal;
156     return ( hp->errno );
157 }
158 
159 extern int
160 dbm_clearerr(db)
161 DBM	*db;
162 {
163     HTAB	*hp;
164 
165     hp = (HTAB *)db->internal;
166     hp->errno = 0;
167     return ( 0 );
168 }
169