xref: /onnv-gate/usr/src/lib/libast/common/comp/hsearch.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin /*
234887Schin  * hsearch() for systems that have <search.h> but no hsearch()
244887Schin  * why would such a system provide the interface but not the
254887Schin  * implementation? that's what happens when one slimes their
264887Schin  * way through standards compliance
274887Schin  *
284887Schin  * NOTE: please excuse the crude feature test
294887Schin  */
304887Schin 
314887Schin #if !_UWIN
324887Schin 
_STUB_hsearch()334887Schin void _STUB_hsearch(){}
344887Schin 
354887Schin #else
364887Schin 
374887Schin #if _PACKAGE_ast
384887Schin #include	<ast.h>
394887Schin #endif
404887Schin 
414887Schin #define hcreate		______hcreate
424887Schin #define hdestroy	______hdestroy
434887Schin #define hsearch		______hsearch
444887Schin 
454887Schin #include	<search.h>
464887Schin 
474887Schin #undef	hcreate
484887Schin #undef	hdestroy
494887Schin #undef	hsearch
504887Schin 
514887Schin #include	"dthdr.h"
524887Schin 
534887Schin #if defined(__EXPORT__)
544887Schin #define extern	__EXPORT__
554887Schin #endif
564887Schin 
574887Schin /*	POSIX hsearch library based on libdt
584887Schin **	Written by Kiem-Phong Vo (AT&T Research, 07/19/95)
594887Schin */
604887Schin 
614887Schin /* type of objects in hash table */
624887Schin typedef struct _hash_s
634887Schin {	Dtlink_t	link;
644887Schin 	ENTRY		item;
654887Schin } Hash_t;
664887Schin 
674887Schin /* object delete function */
684887Schin #if __STD_C
hashfree(Dt_t * dt,Void_t * obj,Dtdisc_t * disc)694887Schin static void hashfree(Dt_t* dt, Void_t* obj, Dtdisc_t* disc)
704887Schin #else
714887Schin static void hashfree(dt, obj, disc)
724887Schin Dt_t*		dt;
734887Schin Void_t*		obj;
744887Schin Dtdisc_t*	disc;
754887Schin #endif
764887Schin {
774887Schin 	free(((Hash_t*)obj)->item.key);
784887Schin 	free(obj);
794887Schin }
804887Schin 
814887Schin static Dt_t*	Hashtab;	/* object dictionary	*/
824887Schin static Dtdisc_t	Hashdisc =	/* discipline		*/
834887Schin {	sizeof(Dtlink_t), -1,
844887Schin 	0,
854887Schin 	NIL(Dtmake_f), hashfree,
864887Schin 	NIL(Dtcompar_f),	/* always use strcmp	*/
874887Schin 	NIL(Dthash_f),
884887Schin 	NIL(Dtmemory_f),
894887Schin 	NIL(Dtevent_f)
904887Schin };
914887Schin 
924887Schin extern
934887Schin #if __STD_C
hcreate(size_t nel)944887Schin int hcreate(size_t nel)
954887Schin #else
964887Schin int hcreate(nel)
974887Schin size_t	nel;
984887Schin #endif
994887Schin {
1004887Schin 	if(Hashtab)	/* already opened */
1014887Schin 		return 0;
1024887Schin 
1034887Schin 	if(!(Hashtab = dtopen(&Hashdisc,Dtset)) )
1044887Schin 		return 0;
1054887Schin 
1064887Schin 	return 1;
1074887Schin }
1084887Schin 
hdestroy()1094887Schin extern void hdestroy()
1104887Schin {	if(Hashtab)
1114887Schin 		dtclose(Hashtab);
1124887Schin 	Hashtab = NIL(Dt_t*);
1134887Schin }
1144887Schin 
1154887Schin extern
1164887Schin #if __STD_C
hsearch(ENTRY item,ACTION action)1174887Schin ENTRY* hsearch(ENTRY item, ACTION action)
1184887Schin #else
1194887Schin ENTRY* hsearch(item, action)
1204887Schin ENTRY	item;
1214887Schin ACTION	action;
1224887Schin #endif
1234887Schin {
1244887Schin 	reg Hash_t*	o;
1254887Schin 
1264887Schin 	if(!Hashtab)
1274887Schin 		return NIL(ENTRY*);
1284887Schin 
1294887Schin 	if(!(o = (Hash_t*)dtmatch(Hashtab,item.key)) && action == ENTER &&
1304887Schin 	   (o = (Hash_t*)malloc(sizeof(Hash_t)) ) )
1314887Schin 	{	o->item = item;
1324887Schin 		o = (Hash_t*)dtinsert(Hashtab,o);
1334887Schin 	}
1344887Schin 
1354887Schin 	return o ? &(o->item) : NIL(ENTRY*);
1364887Schin }
1374887Schin 
1384887Schin #endif
139