xref: /original-bsd/lib/libc/db/hash/hsearch.c (revision 95a66346)
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.5 (Berkeley) 03/12/91";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/types.h>
16 #include <fcntl.h>
17 #include <string.h>
18 #include <db.h>
19 #include "search.h"
20 
21 static	DB	*dbp = NULL;
22 static	ENTRY	retval;
23 
24 extern	int
25 hcreate ( nel )
26 unsigned	nel;
27 {
28     int	status;
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 = hash_open ( NULL, O_CREAT|O_RDWR, 0600, &info );
38     return ( (int) dbp );
39 }
40 
41 
42 extern ENTRY	*
43 hsearch ( item, action )
44 ENTRY	item;
45 ACTION	action;
46 {
47     int	status;
48     DBT	key, val;
49 
50     if ( !dbp ) {
51 	return(NULL);
52     }
53 
54     key.data = (u_char *)item.key;
55     key.size = strlen(item.key) + 1;
56 
57     if ( action == ENTER ) {
58 	val.data = (u_char *)item.data;
59 	val.size = strlen(item.data) + 1;
60 	status = (dbp->put) ( dbp, &key, &val, R_NOOVERWRITE );
61 	if ( status ) {
62 	    return(NULL);
63 	}
64     } else {
65 	/* FIND */
66 	status = (dbp->get) ( dbp, &key, &val, 0 );
67 	if ( status ) {
68 	    return ( NULL );
69 	} else {
70 	    item.data = (char *)val.data;
71 	}
72     }
73     retval.key = item.key;
74     retval.data = item.data;
75     return ( &retval );
76 }
77 
78 
79 extern void
80 hdestroy ()
81 {
82     if (dbp) {
83 	(void)(dbp->close) (dbp);
84 	dbp = NULL;
85     }
86     return;
87 }
88 
89 
90