dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libast / common / comp / hsearch.c
blobf0037d06840037156faef2849ec471bc619a4358
1 /***********************************************************************
2 * *
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 *
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 ***********************************************************************/
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
31 #if !_UWIN
33 void _STUB_hsearch(){}
35 #else
37 #if _PACKAGE_ast
38 #include <ast.h>
39 #endif
41 #define hcreate ______hcreate
42 #define hdestroy ______hdestroy
43 #define hsearch ______hsearch
45 #include <search.h>
47 #undef hcreate
48 #undef hdestroy
49 #undef hsearch
51 #include "dthdr.h"
53 #if defined(__EXPORT__)
54 #define extern __EXPORT__
55 #endif
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
63 { Dtlink_t link;
64 ENTRY item;
65 } Hash_t;
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
77 free(((Hash_t*)obj)->item.key);
78 free(obj);
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 */
87 NIL(Dthash_f),
88 NIL(Dtmemory_f),
89 NIL(Dtevent_f)
92 extern
93 #if __STD_C
94 int hcreate(size_t nel)
95 #else
96 int hcreate(nel)
97 size_t nel;
98 #endif
100 if(Hashtab) /* already opened */
101 return 0;
103 if(!(Hashtab = dtopen(&Hashdisc,Dtset)) )
104 return 0;
106 return 1;
109 extern void hdestroy()
110 { if(Hashtab)
111 dtclose(Hashtab);
112 Hashtab = NIL(Dt_t*);
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
124 reg Hash_t* o;
126 if(!Hashtab)
127 return NIL(ENTRY*);
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);
135 return o ? &(o->item) : NIL(ENTRY*);
138 #endif