1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A/*
1N/A * hsearch() for systems that have <search.h> but no hsearch()
1N/A * why would such a system provide the interface but not the
1N/A * implementation? that's what happens when one slimes their
1N/A * way through standards compliance
1N/A *
1N/A * NOTE: please excuse the crude feature test
1N/A */
1N/A
1N/A#if !_UWIN
1N/A
1N/Avoid _STUB_hsearch(){}
1N/A
1N/A#else
1N/A
1N/A#if _PACKAGE_ast
1N/A#include <ast.h>
1N/A#endif
1N/A
1N/A#define hcreate ______hcreate
1N/A#define hdestroy ______hdestroy
1N/A#define hsearch ______hsearch
1N/A
1N/A#include <search.h>
1N/A
1N/A#undef hcreate
1N/A#undef hdestroy
1N/A#undef hsearch
1N/A
1N/A#include "dthdr.h"
1N/A
1N/A#if defined(__EXPORT__)
1N/A#define extern __EXPORT__
1N/A#endif
1N/A
1N/A/* POSIX hsearch library based on libdt
1N/A** Written by Kiem-Phong Vo (AT&T Research, 07/19/95)
1N/A*/
1N/A
1N/A/* type of objects in hash table */
1N/Atypedef struct _hash_s
1N/A{ Dtlink_t link;
1N/A ENTRY item;
1N/A} Hash_t;
1N/A
1N/A/* object delete function */
1N/A#if __STD_C
1N/Astatic void hashfree(Dt_t* dt, Void_t* obj, Dtdisc_t* disc)
1N/A#else
1N/Astatic void hashfree(dt, obj, disc)
1N/ADt_t* dt;
1N/AVoid_t* obj;
1N/ADtdisc_t* disc;
1N/A#endif
1N/A{
1N/A free(((Hash_t*)obj)->item.key);
1N/A free(obj);
1N/A}
1N/A
1N/Astatic Dt_t* Hashtab; /* object dictionary */
1N/Astatic Dtdisc_t Hashdisc = /* discipline */
1N/A{ sizeof(Dtlink_t), -1,
1N/A 0,
1N/A NIL(Dtmake_f), hashfree,
1N/A NIL(Dtcompar_f), /* always use strcmp */
1N/A NIL(Dthash_f),
1N/A NIL(Dtmemory_f),
1N/A NIL(Dtevent_f)
1N/A};
1N/A
1N/Aextern
1N/A#if __STD_C
1N/Aint hcreate(size_t nel)
1N/A#else
1N/Aint hcreate(nel)
1N/Asize_t nel;
1N/A#endif
1N/A{
1N/A if(Hashtab) /* already opened */
1N/A return 0;
1N/A
1N/A if(!(Hashtab = dtopen(&Hashdisc,Dtset)) )
1N/A return 0;
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Aextern void hdestroy()
1N/A{ if(Hashtab)
1N/A dtclose(Hashtab);
1N/A Hashtab = NIL(Dt_t*);
1N/A}
1N/A
1N/Aextern
1N/A#if __STD_C
1N/AENTRY* hsearch(ENTRY item, ACTION action)
1N/A#else
1N/AENTRY* hsearch(item, action)
1N/AENTRY item;
1N/AACTION action;
1N/A#endif
1N/A{
1N/A reg Hash_t* o;
1N/A
1N/A if(!Hashtab)
1N/A return NIL(ENTRY*);
1N/A
1N/A if(!(o = (Hash_t*)dtmatch(Hashtab,item.key)) && action == ENTER &&
1N/A (o = (Hash_t*)malloc(sizeof(Hash_t)) ) )
1N/A { o->item = item;
1N/A o = (Hash_t*)dtinsert(Hashtab,o);
1N/A }
1N/A
1N/A return o ? &(o->item) : NIL(ENTRY*);
1N/A}
1N/A
1N/A#endif