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