xref: /csrg-svn/lib/libc/db/hash/hsearch.c (revision 46562)
146369Sbostic /*-
246369Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346369Sbostic  * All rights reserved.
446369Sbostic  *
546369Sbostic  * This code is derived from software contributed to Berkeley by
646369Sbostic  * Margo Seltzer.
746369Sbostic  *
846369Sbostic  * %sccs.include.redist.c%
946369Sbostic  */
1046369Sbostic 
1146369Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46562Sbostic static char sccsid[] = "@(#)hsearch.c	5.3 (Berkeley) 02/22/91";
1346369Sbostic #endif /* LIBC_SCCS and not lint */
1446369Sbostic 
1546369Sbostic #include <sys/types.h>
16*46562Sbostic #include <fcntl.h>
17*46562Sbostic #include <string.h>
1846369Sbostic #include <db.h>
1946369Sbostic #include "search.h"
2046369Sbostic 
2146369Sbostic static	DB	*dbp = NULL;
2246369Sbostic static	ENTRY	retval;
2346369Sbostic 
2446369Sbostic extern	int
2546369Sbostic hcreate ( nel )
2646369Sbostic unsigned	nel;
2746369Sbostic {
2846369Sbostic     int	status;
2946369Sbostic     HASHINFO	info;
3046369Sbostic 
3146369Sbostic     info.nelem = nel;
3246369Sbostic     info.bsize = 256;
3346369Sbostic     info.ffactor = 8;
3446369Sbostic     info.ncached = NULL;
3546369Sbostic     info.hash = NULL;
3646369Sbostic     info.lorder = 0;
3746369Sbostic     dbp = hash_open ( NULL, O_CREAT|O_RDWR, 0600, &info );
3846369Sbostic     return ( (int) dbp );
3946369Sbostic }
4046369Sbostic 
4146369Sbostic 
4246369Sbostic extern ENTRY	*
4346369Sbostic hsearch ( item, action )
4446369Sbostic ENTRY	item;
4546369Sbostic ACTION	action;
4646369Sbostic {
4746369Sbostic     int	status;
4846369Sbostic     DBT	key, val;
4946369Sbostic 
5046369Sbostic     if ( !dbp ) {
5146369Sbostic 	return(NULL);
5246369Sbostic     }
5346369Sbostic 
5446369Sbostic     key.data = item.key;
5546369Sbostic     key.size = strlen(item.key) + 1;
5646369Sbostic 
5746369Sbostic     if ( action == ENTER ) {
5846369Sbostic 	val.data = item.data;
5946369Sbostic 	val.size = strlen(item.data) + 1;
6046369Sbostic 	status = (dbp->put) ( dbp, &key, &val, R_NOOVERWRITE );
6146369Sbostic 	if ( status ) {
6246369Sbostic 	    return(NULL);
6346369Sbostic 	}
6446369Sbostic     } else {
6546369Sbostic 	/* FIND */
6646369Sbostic 	status = (dbp->get) ( dbp, &key, &val );
6746369Sbostic 	if ( status ) {
6846369Sbostic 	    return ( NULL );
6946369Sbostic 	} else {
7046369Sbostic 	    item.data = val.data;
7146369Sbostic 	}
7246369Sbostic     }
7346459Sbostic     retval.key = item.key;
7446459Sbostic     retval.data = item.data;
7546459Sbostic     return ( &retval );
7646369Sbostic }
7746369Sbostic 
7846369Sbostic 
7946369Sbostic extern void
8046369Sbostic hdestroy ()
8146369Sbostic {
8246369Sbostic     if (dbp) {
8346369Sbostic 	(void)(dbp->close) (dbp);
8446369Sbostic 	dbp = NULL;
8546369Sbostic     }
8646369Sbostic     return;
8746369Sbostic }
8846369Sbostic 
8946369Sbostic 
90