ypdb.c revision 1.4
1/*	$NetBSD: ypdb.c,v 1.4 1997/10/13 03:42:30 lukem Exp $	*/
2
3/*
4 * Copyright (c) 1990, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Margo Seltzer.
10 *
11 * This code is derived from ndbm module of BSD4.4 db (hash) by
12 * Mats O Jansson
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 *    must display the following acknowledgement:
24 *	This product includes software developed by the University of
25 *	California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 *    may be used to endorse or promote products derived from this software
28 *    without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
31 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
34 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43#include <sys/cdefs.h>
44#ifndef lint
45__RCSID("$NetBSD: ypdb.c,v 1.4 1997/10/13 03:42:30 lukem Exp $");
46#endif
47
48#include <sys/param.h>
49#include <sys/types.h>
50
51#include <db.h>
52#include <errno.h>
53#include <stdio.h>
54#include <string.h>
55
56#include "ypdb.h"
57
58/*
59 * Returns:
60 * 	*DBM on success
61 *	 NULL on failure
62 */
63
64DBM *
65ypdb_open(file, flags, mode)
66	const char *file;
67	int flags, mode;
68{
69	char path[MAXPATHLEN], *cp;
70	DBM *db;
71	BTREEINFO info;
72
73	cp = strrchr(file, '.');
74	snprintf(path, sizeof(path), "%s%s", file,
75	    (cp != NULL && strcmp(cp, ".db") == 0) ? "" : YPDB_SUFFIX);
76
77		/* try our btree format first */
78	info.flags = 0;
79	info.cachesize = 0;
80	info.maxkeypage = 0;
81	info.minkeypage = 0;
82	info.psize = 0;
83	info.compare = NULL;
84	info.prefix = NULL;
85	info.lorder = 0;
86	db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info);
87	if (db != NULL || errno != EFTYPE)
88		return (db);
89
90		/* fallback to standard hash (for sendmail's aliases.db) */
91	db = (DBM *)dbopen(path, flags, mode, DB_HASH, NULL);
92	return (db);
93}
94
95void
96ypdb_close(db)
97	DBM *db;
98{
99	(void)(db->close)(db);
100}
101
102/*
103 * Returns:
104 *	DATUM on success
105 *	NULL on failure
106 */
107
108datum
109ypdb_fetch(db, key)
110	DBM *db;
111	datum key;
112{
113	datum retval;
114	int status;
115
116	status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
117	if (status) {
118		retval.dptr = NULL;
119		retval.dsize = 0;
120	}
121	return (retval);
122}
123
124/*
125 * Returns:
126 *	DATUM on success
127 *	NULL on failure
128 */
129
130datum
131ypdb_firstkey(db)
132	DBM *db;
133{
134	int status;
135	datum retdata, retkey;
136
137	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
138	if (status)
139		retkey.dptr = NULL;
140	return (retkey);
141}
142
143/*
144 * Returns:
145 *	DATUM on success
146 *	NULL on failure
147 */
148
149datum
150ypdb_nextkey(db)
151	DBM *db;
152{
153	int status;
154	datum retdata, retkey;
155
156	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
157	if (status)
158		retkey.dptr = NULL;
159	return (retkey);
160}
161
162/*
163 * Returns:
164 *	DATUM on success
165 *	NULL on failure
166 */
167
168datum
169ypdb_setkey(db, key)
170	DBM *db;
171        datum key;
172{
173	int status;
174	datum retdata;
175	status = (db->seq)(db, (DBT *)&key, (DBT *)&retdata, R_CURSOR);
176	if (status)
177		key.dptr = NULL;
178	return (key);
179}
180
181/*
182 * Returns:
183 *	 0 on success
184 *	<0 failure
185 */
186
187int
188ypdb_delete(db, key)
189	DBM *db;
190	datum key;
191{
192	int status;
193
194	status = (db->del)(db, (DBT *)&key, 0);
195	if (status)
196		return (-1);
197	else
198		return (0);
199}
200
201/*
202 * Returns:
203 *	 0 on success
204 *	<0 failure
205 *	 1 if YPDB_INSERT and entry exists
206 */
207
208int
209ypdb_store(db, key, content, flags)
210	DBM *db;
211	datum key, content;
212	int flags;
213{
214	return ((db->put)(db, (DBT *)&key, (DBT *)&content,
215	    (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
216}
217