xref: /original-bsd/lib/libc/db/hash/hsearch.c (revision d1f811c7)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.7 (Berkeley) 09/08/91";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/types.h>
16 #include <fcntl.h>
17 #include <string.h>
18 #define	__DBINTERFACE_PRIVATE
19 #include <db.h>
20 #include "search.h"
21 
22 static DB *dbp = NULL;
23 static ENTRY retval;
24 
25 extern int
26 hcreate(nel)
27 	u_int nel;
28 {
29 	HASHINFO info;
30 
31 	info.nelem = nel;
32 	info.bsize = 256;
33 	info.ffactor = 8;
34 	info.cachesize = NULL;
35 	info.hash = NULL;
36 	info.lorder = 0;
37 	dbp = (DB *)__hash_open(NULL, O_CREAT | O_RDWR, 0600, &info);
38 	return ((int)dbp);
39 }
40 
41 extern ENTRY *
42 hsearch(item, action)
43 	ENTRY item;
44 	ACTION action;
45 {
46 	DBT key, val;
47 	int status;
48 
49 	if (!dbp)
50 		return (NULL);
51 	key.data = (u_char *)item.key;
52 	key.size = strlen(item.key) + 1;
53 
54 	if (action == ENTER) {
55 		val.data = (u_char *)item.data;
56 		val.size = strlen(item.data) + 1;
57 		status = (dbp->put)(dbp, &key, &val, R_NOOVERWRITE);
58 		if (status)
59 			return (NULL);
60 	} else {
61 		/* FIND */
62 		status = (dbp->get)(dbp, &key, &val, 0);
63 		if (status)
64 			return (NULL);
65 		else
66 			item.data = (char *)val.data;
67 	}
68 	retval.key = item.key;
69 	retval.data = item.data;
70 	return (&retval);
71 }
72 
73 extern void
74 hdestroy()
75 {
76 	if (dbp) {
77 		(void)(dbp->close)(dbp);
78 		dbp = NULL;
79 	}
80 }
81