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[] = "@(#)hsearch.c 5.6 (Berkeley) 09/04/91"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/types.h> 16 #include <fcntl.h> 17 #include <string.h> 18 #include <db.h> 19 #include "search.h" 20 21 static DB *dbp = NULL; 22 static ENTRY retval; 23 24 extern int 25 hcreate(nel) 26 u_int nel; 27 { 28 HASHINFO info; 29 30 info.nelem = nel; 31 info.bsize = 256; 32 info.ffactor = 8; 33 info.cachesize = NULL; 34 info.hash = NULL; 35 info.lorder = 0; 36 dbp = (DB *)__hash_open(NULL, O_CREAT | O_RDWR, 0600, &info); 37 return ((int)dbp); 38 } 39 40 extern ENTRY * 41 hsearch(item, action) 42 ENTRY item; 43 ACTION action; 44 { 45 DBT key, val; 46 int status; 47 48 if (!dbp) 49 return (NULL); 50 key.data = (u_char *)item.key; 51 key.size = strlen(item.key) + 1; 52 53 if (action == ENTER) { 54 val.data = (u_char *)item.data; 55 val.size = strlen(item.data) + 1; 56 status = (dbp->put)(dbp, &key, &val, R_NOOVERWRITE); 57 if (status) 58 return (NULL); 59 } else { 60 /* FIND */ 61 status = (dbp->get)(dbp, &key, &val, 0); 62 if (status) 63 return (NULL); 64 else 65 item.data = (char *)val.data; 66 } 67 retval.key = item.key; 68 retval.data = item.data; 69 return (&retval); 70 } 71 72 extern void 73 hdestroy() 74 { 75 if (dbp) { 76 (void)(dbp->close)(dbp); 77 dbp = NULL; 78 } 79 } 80