Home | History | Annotate | Line # | Download | only in common
ypdb.c revision 1.5
      1 /*	$NetBSD: ypdb.c,v 1.5 1997/11/01 14:24:54 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.5 1997/11/01 14:24:54 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 <rpcsvc/yp.h>
     57 
     58 #include "ypdb.h"
     59 
     60 /*
     61  * Returns:
     62  * 	*DBM on success
     63  *	 NULL on failure
     64  */
     65 
     66 DBM *
     67 ypdb_open(file, flags, mode)
     68 	const char *file;
     69 	int flags, mode;
     70 {
     71 	char path[MAXPATHLEN], *cp;
     72 	DBM *db;
     73 	BTREEINFO info;
     74 
     75 	cp = strrchr(file, '.');
     76 	snprintf(path, sizeof(path), "%s%s", file,
     77 	    (cp != NULL && strcmp(cp, ".db") == 0) ? "" : YPDB_SUFFIX);
     78 
     79 		/* try our btree format first */
     80 	info.flags = 0;
     81 	info.cachesize = 0;
     82 	info.maxkeypage = 0;
     83 	info.minkeypage = 0;
     84 	info.psize = 0;
     85 	info.compare = NULL;
     86 	info.prefix = NULL;
     87 	info.lorder = 0;
     88 	db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info);
     89 	if (db != NULL || errno != EFTYPE)
     90 		return (db);
     91 
     92 		/* fallback to standard hash (for sendmail's aliases.db) */
     93 	db = (DBM *)dbopen(path, flags, mode, DB_HASH, NULL);
     94 	return (db);
     95 }
     96 
     97 void
     98 ypdb_close(db)
     99 	DBM *db;
    100 {
    101 	(void)(db->close)(db);
    102 }
    103 
    104 /*
    105  * Returns:
    106  *	DATUM on success
    107  *	NULL on failure
    108  */
    109 
    110 datum
    111 ypdb_fetch(db, key)
    112 	DBM *db;
    113 	datum key;
    114 {
    115 	datum retval;
    116 	int status;
    117 
    118 	status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
    119 	if (status) {
    120 		retval.dptr = NULL;
    121 		retval.dsize = 0;
    122 	}
    123 	return (retval);
    124 }
    125 
    126 /*
    127  * Returns:
    128  *	DATUM on success
    129  *	NULL on failure
    130  */
    131 
    132 datum
    133 ypdb_firstkey(db)
    134 	DBM *db;
    135 {
    136 	int status;
    137 	datum retdata, retkey;
    138 
    139 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
    140 	if (status)
    141 		retkey.dptr = NULL;
    142 	return (retkey);
    143 }
    144 
    145 /*
    146  * Returns:
    147  *	DATUM on success
    148  *	NULL on failure
    149  */
    150 
    151 datum
    152 ypdb_nextkey(db)
    153 	DBM *db;
    154 {
    155 	int status;
    156 	datum retdata, retkey;
    157 
    158 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
    159 	if (status)
    160 		retkey.dptr = NULL;
    161 	return (retkey);
    162 }
    163 
    164 /*
    165  * Returns:
    166  *	DATUM on success
    167  *	NULL on failure
    168  */
    169 
    170 datum
    171 ypdb_setkey(db, key)
    172 	DBM *db;
    173         datum key;
    174 {
    175 	int status;
    176 	datum retdata;
    177 	status = (db->seq)(db, (DBT *)&key, (DBT *)&retdata, R_CURSOR);
    178 	if (status)
    179 		key.dptr = NULL;
    180 	return (key);
    181 }
    182 
    183 /*
    184  * Returns:
    185  *	 0 on success
    186  *	<0 failure
    187  */
    188 
    189 int
    190 ypdb_delete(db, key)
    191 	DBM *db;
    192 	datum key;
    193 {
    194 	int status;
    195 
    196 	status = (db->del)(db, (DBT *)&key, 0);
    197 	if (status)
    198 		return (-1);
    199 	else
    200 		return (0);
    201 }
    202 
    203 /*
    204  * Returns:
    205  *	 0 on success
    206  *	<0 failure
    207  *	 1 if YPDB_INSERT and entry exists
    208  */
    209 
    210 int
    211 ypdb_store(db, key, content, flags)
    212 	DBM *db;
    213 	datum key, content;
    214 	int flags;
    215 {
    216 	if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD)
    217 		return -1;
    218 	return ((db->put)(db, (DBT *)&key, (DBT *)&content,
    219 	    (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
    220 }
    221