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