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