xref: /titanic_44/usr/src/lib/libast/common/comp/hsearch.c (revision bdf0047c9427cca40961a023475891c898579c37)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 /*
23  * hsearch() for systems that have <search.h> but no hsearch()
24  * why would such a system provide the interface but not the
25  * implementation? that's what happens when one slimes their
26  * way through standards compliance
27  *
28  * NOTE: please excuse the crude feature test
29  */
30 
31 #if !_UWIN
32 
33 void _STUB_hsearch(){}
34 
35 #else
36 
37 #if _PACKAGE_ast
38 #include	<ast.h>
39 #endif
40 
41 #define hcreate		______hcreate
42 #define hdestroy	______hdestroy
43 #define hsearch		______hsearch
44 
45 #include	<search.h>
46 
47 #undef	hcreate
48 #undef	hdestroy
49 #undef	hsearch
50 
51 #include	"dthdr.h"
52 
53 #if defined(__EXPORT__)
54 #define extern	__EXPORT__
55 #endif
56 
57 /*	POSIX hsearch library based on libdt
58 **	Written by Kiem-Phong Vo (AT&T Research, 07/19/95)
59 */
60 
61 /* type of objects in hash table */
62 typedef struct _hash_s
63 {	Dtlink_t	link;
64 	ENTRY		item;
65 } Hash_t;
66 
67 /* object delete function */
68 #if __STD_C
69 static void hashfree(Dt_t* dt, Void_t* obj, Dtdisc_t* disc)
70 #else
71 static void hashfree(dt, obj, disc)
72 Dt_t*		dt;
73 Void_t*		obj;
74 Dtdisc_t*	disc;
75 #endif
76 {
77 	free(((Hash_t*)obj)->item.key);
78 	free(obj);
79 }
80 
81 static Dt_t*	Hashtab;	/* object dictionary	*/
82 static Dtdisc_t	Hashdisc =	/* discipline		*/
83 {	sizeof(Dtlink_t), -1,
84 	0,
85 	NIL(Dtmake_f), hashfree,
86 	NIL(Dtcompar_f),	/* always use strcmp	*/
87 	NIL(Dthash_f),
88 	NIL(Dtmemory_f),
89 	NIL(Dtevent_f)
90 };
91 
92 extern
93 #if __STD_C
94 int hcreate(size_t nel)
95 #else
96 int hcreate(nel)
97 size_t	nel;
98 #endif
99 {
100 	if(Hashtab)	/* already opened */
101 		return 0;
102 
103 	if(!(Hashtab = dtopen(&Hashdisc,Dtset)) )
104 		return 0;
105 
106 	return 1;
107 }
108 
109 extern void hdestroy()
110 {	if(Hashtab)
111 		dtclose(Hashtab);
112 	Hashtab = NIL(Dt_t*);
113 }
114 
115 extern
116 #if __STD_C
117 ENTRY* hsearch(ENTRY item, ACTION action)
118 #else
119 ENTRY* hsearch(item, action)
120 ENTRY	item;
121 ACTION	action;
122 #endif
123 {
124 	reg Hash_t*	o;
125 
126 	if(!Hashtab)
127 		return NIL(ENTRY*);
128 
129 	if(!(o = (Hash_t*)dtmatch(Hashtab,item.key)) && action == ENTER &&
130 	   (o = (Hash_t*)malloc(sizeof(Hash_t)) ) )
131 	{	o->item = item;
132 		o = (Hash_t*)dtinsert(Hashtab,o);
133 	}
134 
135 	return o ? &(o->item) : NIL(ENTRY*);
136 }
137 
138 #endif
139