xref: /csrg-svn/lib/libc/db/hash/hsearch.c (revision 46369)
1*46369Sbostic /*-
2*46369Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46369Sbostic  * All rights reserved.
4*46369Sbostic  *
5*46369Sbostic  * This code is derived from software contributed to Berkeley by
6*46369Sbostic  * Margo Seltzer.
7*46369Sbostic  *
8*46369Sbostic  * %sccs.include.redist.c%
9*46369Sbostic  */
10*46369Sbostic 
11*46369Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46369Sbostic static char sccsid[] = "@(#)hsearch.c	5.1 (Berkeley) 02/12/91";
13*46369Sbostic #endif /* LIBC_SCCS and not lint */
14*46369Sbostic 
15*46369Sbostic #include <sys/file.h>
16*46369Sbostic #include <sys/types.h>
17*46369Sbostic #include <stdio.h>
18*46369Sbostic #include <db.h>
19*46369Sbostic #include "search.h"
20*46369Sbostic 
21*46369Sbostic static	DB	*dbp = NULL;
22*46369Sbostic static	ENTRY	retval;
23*46369Sbostic 
24*46369Sbostic extern	int
25*46369Sbostic hcreate ( nel )
26*46369Sbostic unsigned	nel;
27*46369Sbostic {
28*46369Sbostic     int	status;
29*46369Sbostic     HASHINFO	info;
30*46369Sbostic 
31*46369Sbostic     info.nelem = nel;
32*46369Sbostic     info.bsize = 256;
33*46369Sbostic     info.ffactor = 8;
34*46369Sbostic     info.ncached = NULL;
35*46369Sbostic     info.hash = NULL;
36*46369Sbostic     info.lorder = 0;
37*46369Sbostic     dbp = hash_open ( NULL, O_CREAT|O_RDWR, 0600, &info );
38*46369Sbostic     return ( (int) dbp );
39*46369Sbostic }
40*46369Sbostic 
41*46369Sbostic 
42*46369Sbostic extern ENTRY	*
43*46369Sbostic hsearch ( item, action )
44*46369Sbostic ENTRY	item;
45*46369Sbostic ACTION	action;
46*46369Sbostic {
47*46369Sbostic     int	status;
48*46369Sbostic     DBT	key, val;
49*46369Sbostic 
50*46369Sbostic     if ( !dbp ) {
51*46369Sbostic 	return(NULL);
52*46369Sbostic     }
53*46369Sbostic 
54*46369Sbostic     key.data = item.key;
55*46369Sbostic     key.size = strlen(item.key) + 1;
56*46369Sbostic 
57*46369Sbostic     if ( action == ENTER ) {
58*46369Sbostic 	val.data = item.data;
59*46369Sbostic 	val.size = strlen(item.data) + 1;
60*46369Sbostic 	status = (dbp->put) ( dbp, &key, &val, R_NOOVERWRITE );
61*46369Sbostic 	if ( status ) {
62*46369Sbostic 	    return(NULL);
63*46369Sbostic 	}
64*46369Sbostic     } else {
65*46369Sbostic 	/* FIND */
66*46369Sbostic 	status = (dbp->get) ( dbp, &key, &val );
67*46369Sbostic 	if ( status ) {
68*46369Sbostic 	    return ( NULL );
69*46369Sbostic 	} else {
70*46369Sbostic 	    item.data = val.data;
71*46369Sbostic 	}
72*46369Sbostic     }
73*46369Sbostic     return ( &item );
74*46369Sbostic }
75*46369Sbostic 
76*46369Sbostic 
77*46369Sbostic extern void
78*46369Sbostic hdestroy ()
79*46369Sbostic {
80*46369Sbostic     if (dbp) {
81*46369Sbostic 	(void)(dbp->close) (dbp);
82*46369Sbostic 	dbp = NULL;
83*46369Sbostic     }
84*46369Sbostic     return;
85*46369Sbostic }
86*46369Sbostic 
87*46369Sbostic 
88