xref: /original-bsd/lib/libc/db/hash/ndbm.c (revision 95ecee29)
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  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)ndbm.c	8.2 (Berkeley) 09/11/93";
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 
22 #include <ndbm.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "hash.h"
27 
28 /*
29  * Returns:
30  * 	*DBM on success
31  *	 NULL on failure
32  */
33 extern DBM *
34 dbm_open(file, flags, mode)
35 	const char *file;
36 	int flags, mode;
37 {
38 	HASHINFO info;
39 	char path[MAXPATHLEN];
40 
41 	info.bsize = 4096;
42 	info.ffactor = 40;
43 	info.nelem = 1;
44 	info.cachesize = NULL;
45 	info.hash = NULL;
46 	info.lorder = 0;
47 	(void)strcpy(path, file);
48 	(void)strcat(path, DBM_SUFFIX);
49 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
50 }
51 
52 extern void
53 dbm_close(db)
54 	DBM *db;
55 {
56 	(void)(db->close)(db);
57 }
58 
59 /*
60  * Returns:
61  *	DATUM on success
62  *	NULL on failure
63  */
64 extern datum
65 dbm_fetch(db, key)
66 	DBM *db;
67 	datum key;
68 {
69 	datum retval;
70 	int status;
71 
72 	status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
73 	if (status) {
74 		retval.dptr = NULL;
75 		retval.dsize = 0;
76 	}
77 	return (retval);
78 }
79 
80 /*
81  * Returns:
82  *	DATUM on success
83  *	NULL on failure
84  */
85 extern datum
86 dbm_firstkey(db)
87 	DBM *db;
88 {
89 	int status;
90 	datum retdata, retkey;
91 
92 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
93 	if (status)
94 		retkey.dptr = NULL;
95 	return (retkey);
96 }
97 
98 /*
99  * Returns:
100  *	DATUM on success
101  *	NULL on failure
102  */
103 extern datum
104 dbm_nextkey(db)
105 	DBM *db;
106 {
107 	int status;
108 	datum retdata, retkey;
109 
110 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
111 	if (status)
112 		retkey.dptr = NULL;
113 	return (retkey);
114 }
115 /*
116  * Returns:
117  *	 0 on success
118  *	<0 failure
119  */
120 extern int
121 dbm_delete(db, key)
122 	DBM *db;
123 	datum key;
124 {
125 	int status;
126 
127 	status = (db->del)(db, (DBT *)&key, 0);
128 	if (status)
129 		return (-1);
130 	else
131 		return (0);
132 }
133 
134 /*
135  * Returns:
136  *	 0 on success
137  *	<0 failure
138  *	 1 if DBM_INSERT and entry exists
139  */
140 extern int
141 dbm_store(db, key, content, flags)
142 	DBM *db;
143 	datum key, content;
144 	int flags;
145 {
146 	return ((db->put)(db, (DBT *)&key, (DBT *)&content,
147 	    (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
148 }
149 
150 extern int
151 dbm_error(db)
152 	DBM *db;
153 {
154 	HTAB *hp;
155 
156 	hp = (HTAB *)db->internal;
157 	return (hp->errno);
158 }
159 
160 extern int
161 dbm_clearerr(db)
162 	DBM *db;
163 {
164 	HTAB *hp;
165 
166 	hp = (HTAB *)db->internal;
167 	hp->errno = 0;
168 	return (0);
169 }
170 
171 extern int
172 dbm_dirfno(db)
173 	DBM *db;
174 {
175 	return(((HTAB *)db->internal)->fp);
176 }
177