1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
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
28 * NOTE: please excuse the crude feature test
33 void _STUB_hsearch(){}
41 #define hcreate ______hcreate
42 #define hdestroy ______hdestroy
43 #define hsearch ______hsearch
53 #if defined(__EXPORT__)
54 #define extern __EXPORT__
57 /* POSIX hsearch library based on libdt
58 ** Written by Kiem-Phong Vo (AT&T Research, 07/19/95)
61 /* type of objects in hash table */
62 typedef struct _hash_s
67 /* object delete function */
69 static void hashfree(Dt_t
* dt
, Void_t
* obj
, Dtdisc_t
* disc
)
71 static void hashfree(dt
, obj
, disc
)
77 free(((Hash_t
*)obj
)->item
.key
);
81 static Dt_t
* Hashtab
; /* object dictionary */
82 static Dtdisc_t Hashdisc
= /* discipline */
83 { sizeof(Dtlink_t
), -1,
85 NIL(Dtmake_f
), hashfree
,
86 NIL(Dtcompar_f
), /* always use strcmp */
94 int hcreate(size_t nel
)
100 if(Hashtab
) /* already opened */
103 if(!(Hashtab
= dtopen(&Hashdisc
,Dtset
)) )
109 extern void hdestroy()
112 Hashtab
= NIL(Dt_t
*);
117 ENTRY
* hsearch(ENTRY item
, ACTION action
)
119 ENTRY
* hsearch(item
, action
)
129 if(!(o
= (Hash_t
*)dtmatch(Hashtab
,item
.key
)) && action
== ENTER
&&
130 (o
= (Hash_t
*)malloc(sizeof(Hash_t
)) ) )
132 o
= (Hash_t
*)dtinsert(Hashtab
,o
);
135 return o
? &(o
->item
) : NIL(ENTRY
*);