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