xref: /csrg-svn/lib/libc/db/hash/hsearch.c (revision 57932)
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*57932Sbostic static char sccsid[] = "@(#)hsearch.c	5.8 (Berkeley) 02/11/93";
1346369Sbostic #endif /* LIBC_SCCS and not lint */
1446369Sbostic 
1546369Sbostic #include <sys/types.h>
16*57932Sbostic 
1746562Sbostic #include <fcntl.h>
1846562Sbostic #include <string.h>
19*57932Sbostic 
2051047Sbostic #define	__DBINTERFACE_PRIVATE
2146369Sbostic #include <db.h>
2246369Sbostic #include "search.h"
2346369Sbostic 
2450997Sbostic static DB *dbp = NULL;
2550997Sbostic static ENTRY retval;
2646369Sbostic 
2750997Sbostic extern int
2850997Sbostic hcreate(nel)
2950997Sbostic 	u_int nel;
3046369Sbostic {
3150997Sbostic 	HASHINFO info;
3246369Sbostic 
3350997Sbostic 	info.nelem = nel;
3450997Sbostic 	info.bsize = 256;
3550997Sbostic 	info.ffactor = 8;
3650997Sbostic 	info.cachesize = NULL;
3750997Sbostic 	info.hash = NULL;
3850997Sbostic 	info.lorder = 0;
3950997Sbostic 	dbp = (DB *)__hash_open(NULL, O_CREAT | O_RDWR, 0600, &info);
4050997Sbostic 	return ((int)dbp);
4146369Sbostic }
4246369Sbostic 
4350997Sbostic extern ENTRY *
4450997Sbostic hsearch(item, action)
4550997Sbostic 	ENTRY item;
4650997Sbostic 	ACTION action;
4746369Sbostic {
4850997Sbostic 	DBT key, val;
4950997Sbostic 	int status;
5046369Sbostic 
5150997Sbostic 	if (!dbp)
5250997Sbostic 		return (NULL);
5350997Sbostic 	key.data = (u_char *)item.key;
5450997Sbostic 	key.size = strlen(item.key) + 1;
5546369Sbostic 
5650997Sbostic 	if (action == ENTER) {
5750997Sbostic 		val.data = (u_char *)item.data;
5850997Sbostic 		val.size = strlen(item.data) + 1;
5950997Sbostic 		status = (dbp->put)(dbp, &key, &val, R_NOOVERWRITE);
6050997Sbostic 		if (status)
6150997Sbostic 			return (NULL);
6246369Sbostic 	} else {
6350997Sbostic 		/* FIND */
6450997Sbostic 		status = (dbp->get)(dbp, &key, &val, 0);
6550997Sbostic 		if (status)
6650997Sbostic 			return (NULL);
6750997Sbostic 		else
6850997Sbostic 			item.data = (char *)val.data;
6946369Sbostic 	}
7050997Sbostic 	retval.key = item.key;
7150997Sbostic 	retval.data = item.data;
7250997Sbostic 	return (&retval);
7346369Sbostic }
7446369Sbostic 
7546369Sbostic extern void
7650997Sbostic hdestroy()
7746369Sbostic {
7850997Sbostic 	if (dbp) {
7950997Sbostic 		(void)(dbp->close)(dbp);
8050997Sbostic 		dbp = NULL;
8150997Sbostic 	}
8246369Sbostic }
83