xref: /csrg-svn/lib/libc/db/hash/hsearch.c (revision 51047)
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*51047Sbostic static char sccsid[] = "@(#)hsearch.c	5.7 (Berkeley) 09/08/91";
1346369Sbostic #endif /* LIBC_SCCS and not lint */
1446369Sbostic 
1546369Sbostic #include <sys/types.h>
1646562Sbostic #include <fcntl.h>
1746562Sbostic #include <string.h>
18*51047Sbostic #define	__DBINTERFACE_PRIVATE
1946369Sbostic #include <db.h>
2046369Sbostic #include "search.h"
2146369Sbostic 
2250997Sbostic static DB *dbp = NULL;
2350997Sbostic static ENTRY retval;
2446369Sbostic 
2550997Sbostic extern int
2650997Sbostic hcreate(nel)
2750997Sbostic 	u_int nel;
2846369Sbostic {
2950997Sbostic 	HASHINFO info;
3046369Sbostic 
3150997Sbostic 	info.nelem = nel;
3250997Sbostic 	info.bsize = 256;
3350997Sbostic 	info.ffactor = 8;
3450997Sbostic 	info.cachesize = NULL;
3550997Sbostic 	info.hash = NULL;
3650997Sbostic 	info.lorder = 0;
3750997Sbostic 	dbp = (DB *)__hash_open(NULL, O_CREAT | O_RDWR, 0600, &info);
3850997Sbostic 	return ((int)dbp);
3946369Sbostic }
4046369Sbostic 
4150997Sbostic extern ENTRY *
4250997Sbostic hsearch(item, action)
4350997Sbostic 	ENTRY item;
4450997Sbostic 	ACTION action;
4546369Sbostic {
4650997Sbostic 	DBT key, val;
4750997Sbostic 	int status;
4846369Sbostic 
4950997Sbostic 	if (!dbp)
5050997Sbostic 		return (NULL);
5150997Sbostic 	key.data = (u_char *)item.key;
5250997Sbostic 	key.size = strlen(item.key) + 1;
5346369Sbostic 
5450997Sbostic 	if (action == ENTER) {
5550997Sbostic 		val.data = (u_char *)item.data;
5650997Sbostic 		val.size = strlen(item.data) + 1;
5750997Sbostic 		status = (dbp->put)(dbp, &key, &val, R_NOOVERWRITE);
5850997Sbostic 		if (status)
5950997Sbostic 			return (NULL);
6046369Sbostic 	} else {
6150997Sbostic 		/* FIND */
6250997Sbostic 		status = (dbp->get)(dbp, &key, &val, 0);
6350997Sbostic 		if (status)
6450997Sbostic 			return (NULL);
6550997Sbostic 		else
6650997Sbostic 			item.data = (char *)val.data;
6746369Sbostic 	}
6850997Sbostic 	retval.key = item.key;
6950997Sbostic 	retval.data = item.data;
7050997Sbostic 	return (&retval);
7146369Sbostic }
7246369Sbostic 
7346369Sbostic extern void
7450997Sbostic hdestroy()
7546369Sbostic {
7650997Sbostic 	if (dbp) {
7750997Sbostic 		(void)(dbp->close)(dbp);
7850997Sbostic 		dbp = NULL;
7950997Sbostic 	}
8046369Sbostic }
81