146369Sbostic /*-
261202Sbostic * Copyright (c) 1990, 1993
361202Sbostic * The Regents of the University of California. 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*66193Sbostic static char sccsid[] = "@(#)hsearch.c 8.3 (Berkeley) 02/21/94";
1346369Sbostic #endif /* LIBC_SCCS and not lint */
1446369Sbostic
1546369Sbostic #include <sys/types.h>
1657932Sbostic
1746562Sbostic #include <fcntl.h>
1846562Sbostic #include <string.h>
1957932Sbostic
2046369Sbostic #include <db.h>
2146369Sbostic #include "search.h"
2246369Sbostic
2350997Sbostic static DB *dbp = NULL;
2450997Sbostic static ENTRY retval;
2546369Sbostic
2650997Sbostic extern int
2750997Sbostic hcreate(nel)
2850997Sbostic u_int nel;
2946369Sbostic {
3050997Sbostic HASHINFO info;
3146369Sbostic
3250997Sbostic info.nelem = nel;
3350997Sbostic info.bsize = 256;
3450997Sbostic info.ffactor = 8;
3550997Sbostic info.cachesize = NULL;
3650997Sbostic info.hash = NULL;
3750997Sbostic info.lorder = 0;
3864465Sbostic dbp = (DB *)__hash_open(NULL, O_CREAT | O_RDWR, 0600, &info, 0);
3950997Sbostic return ((int)dbp);
4046369Sbostic }
4146369Sbostic
4250997Sbostic extern ENTRY *
4350997Sbostic hsearch(item, action)
4450997Sbostic ENTRY item;
4550997Sbostic ACTION action;
4646369Sbostic {
4750997Sbostic DBT key, val;
4850997Sbostic int status;
4946369Sbostic
5050997Sbostic if (!dbp)
5150997Sbostic return (NULL);
5250997Sbostic key.data = (u_char *)item.key;
5350997Sbostic key.size = strlen(item.key) + 1;
5446369Sbostic
5550997Sbostic if (action == ENTER) {
5650997Sbostic val.data = (u_char *)item.data;
5750997Sbostic val.size = strlen(item.data) + 1;
5850997Sbostic status = (dbp->put)(dbp, &key, &val, R_NOOVERWRITE);
5950997Sbostic if (status)
6050997Sbostic return (NULL);
6146369Sbostic } else {
6250997Sbostic /* FIND */
6350997Sbostic status = (dbp->get)(dbp, &key, &val, 0);
6450997Sbostic if (status)
6550997Sbostic return (NULL);
6650997Sbostic else
6750997Sbostic item.data = (char *)val.data;
6846369Sbostic }
6950997Sbostic retval.key = item.key;
7050997Sbostic retval.data = item.data;
7150997Sbostic return (&retval);
7246369Sbostic }
7346369Sbostic
7446369Sbostic extern void
hdestroy()7550997Sbostic hdestroy()
7646369Sbostic {
7750997Sbostic if (dbp) {
7850997Sbostic (void)(dbp->close)(dbp);
7950997Sbostic dbp = NULL;
8050997Sbostic }
8146369Sbostic }
82