1*7c478bd9Sstevel@tonic-gate /*- 2*7c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information. 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998 5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved. 6*7c478bd9Sstevel@tonic-gate */ 7*7c478bd9Sstevel@tonic-gate /* 8*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990, 1993 9*7c478bd9Sstevel@tonic-gate * Margo Seltzer. All rights reserved. 10*7c478bd9Sstevel@tonic-gate */ 11*7c478bd9Sstevel@tonic-gate /* 12*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990, 1993 13*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 14*7c478bd9Sstevel@tonic-gate * 15*7c478bd9Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 16*7c478bd9Sstevel@tonic-gate * Margo Seltzer. 17*7c478bd9Sstevel@tonic-gate * 18*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 19*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 20*7c478bd9Sstevel@tonic-gate * are met: 21*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 22*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 23*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 24*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 25*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 26*7c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 27*7c478bd9Sstevel@tonic-gate * must display the following acknowledgement: 28*7c478bd9Sstevel@tonic-gate * This product includes software developed by the University of 29*7c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors. 30*7c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 31*7c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 32*7c478bd9Sstevel@tonic-gate * without specific prior written permission. 33*7c478bd9Sstevel@tonic-gate * 34*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 35*7c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37*7c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 38*7c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 39*7c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 40*7c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41*7c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 42*7c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 43*7c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44*7c478bd9Sstevel@tonic-gate * SUCH DAMAGE. 45*7c478bd9Sstevel@tonic-gate */ 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate #include "config.h" 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #ifndef lint 50*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)hsearch.c 10.9 (Sleepycat) 4/18/98"; 51*7c478bd9Sstevel@tonic-gate #endif /* not lint */ 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES 54*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate #include <errno.h> 57*7c478bd9Sstevel@tonic-gate #include <string.h> 58*7c478bd9Sstevel@tonic-gate #endif 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate #define DB_DBM_HSEARCH 1 61*7c478bd9Sstevel@tonic-gate #include "db_int.h" 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate static DB *dbp; 64*7c478bd9Sstevel@tonic-gate static ENTRY retval; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate int 67*7c478bd9Sstevel@tonic-gate __db_hcreate(nel) 68*7c478bd9Sstevel@tonic-gate size_t nel; 69*7c478bd9Sstevel@tonic-gate { 70*7c478bd9Sstevel@tonic-gate DB_INFO dbinfo; 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate memset(&dbinfo, 0, sizeof(dbinfo)); 73*7c478bd9Sstevel@tonic-gate dbinfo.db_pagesize = 512; 74*7c478bd9Sstevel@tonic-gate dbinfo.h_ffactor = 16; 75*7c478bd9Sstevel@tonic-gate dbinfo.h_nelem = (u_int32_t)nel; /* XXX: Possible overflow. */ 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate errno = db_open(NULL, 78*7c478bd9Sstevel@tonic-gate DB_HASH, DB_CREATE, __db_omode("rw----"), NULL, &dbinfo, &dbp); 79*7c478bd9Sstevel@tonic-gate return (errno == 0 ? 1 : 0); 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate ENTRY * 83*7c478bd9Sstevel@tonic-gate __db_hsearch(item, action) 84*7c478bd9Sstevel@tonic-gate ENTRY item; 85*7c478bd9Sstevel@tonic-gate ACTION action; 86*7c478bd9Sstevel@tonic-gate { 87*7c478bd9Sstevel@tonic-gate DBT key, val; 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate if (dbp == NULL) { 90*7c478bd9Sstevel@tonic-gate errno = EINVAL; 91*7c478bd9Sstevel@tonic-gate return (NULL); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate memset(&key, 0, sizeof(key)); 94*7c478bd9Sstevel@tonic-gate memset(&val, 0, sizeof(val)); 95*7c478bd9Sstevel@tonic-gate key.data = item.key; 96*7c478bd9Sstevel@tonic-gate key.size = strlen(item.key) + 1; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate switch (action) { 99*7c478bd9Sstevel@tonic-gate case ENTER: 100*7c478bd9Sstevel@tonic-gate val.data = item.data; 101*7c478bd9Sstevel@tonic-gate val.size = strlen(item.data) + 1; 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate /* 104*7c478bd9Sstevel@tonic-gate * Try and add the key to the database. If we fail because 105*7c478bd9Sstevel@tonic-gate * the key already exists, return the existing key. 106*7c478bd9Sstevel@tonic-gate */ 107*7c478bd9Sstevel@tonic-gate if ((errno = 108*7c478bd9Sstevel@tonic-gate dbp->put(dbp, NULL, &key, &val, DB_NOOVERWRITE)) == 0) 109*7c478bd9Sstevel@tonic-gate break; 110*7c478bd9Sstevel@tonic-gate if (errno != DB_KEYEXIST) 111*7c478bd9Sstevel@tonic-gate return (NULL); 112*7c478bd9Sstevel@tonic-gate if ((errno = dbp->get(dbp, NULL, &key, &val, 0)) == 0) 113*7c478bd9Sstevel@tonic-gate break; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate if (errno == DB_NOTFOUND) /* XXX: can't happen. */ 116*7c478bd9Sstevel@tonic-gate errno = EINVAL; 117*7c478bd9Sstevel@tonic-gate break; 118*7c478bd9Sstevel@tonic-gate case FIND: 119*7c478bd9Sstevel@tonic-gate if ((errno = dbp->get(dbp, NULL, &key, &val, 0)) != 0) { 120*7c478bd9Sstevel@tonic-gate if (errno == DB_NOTFOUND) 121*7c478bd9Sstevel@tonic-gate errno = 0; 122*7c478bd9Sstevel@tonic-gate return (NULL); 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate item.data = (char *)val.data; 125*7c478bd9Sstevel@tonic-gate break; 126*7c478bd9Sstevel@tonic-gate default: 127*7c478bd9Sstevel@tonic-gate errno = EINVAL; 128*7c478bd9Sstevel@tonic-gate return (NULL); 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate retval.key = item.key; 131*7c478bd9Sstevel@tonic-gate retval.data = item.data; 132*7c478bd9Sstevel@tonic-gate return (&retval); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate void 136*7c478bd9Sstevel@tonic-gate __db_hdestroy() 137*7c478bd9Sstevel@tonic-gate { 138*7c478bd9Sstevel@tonic-gate if (dbp != NULL) { 139*7c478bd9Sstevel@tonic-gate (void)dbp->close(dbp, 0); 140*7c478bd9Sstevel@tonic-gate dbp = NULL; 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate } 143