xref: /onnv-gate/usr/src/lib/libast/common/string/strsearch.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 #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  */
274887Schin 
284887Schin #include <ast.h>
294887Schin 
304887Schin /*
314887Schin  * return a pointer to the element matching
324887Schin  * name in the (*comparf*)() sorted tab of num elements of
334887Schin  * size siz where the first member of each
344887Schin  * element is a char*
354887Schin  *
364887Schin  * 0 returned if name not found
374887Schin  */
384887Schin 
394887Schin void*
strsearch(const void * tab,size_t num,size_t siz,Strcmp_f comparf,const char * name,void * context)404887Schin strsearch(const void* tab, size_t num, size_t siz, Strcmp_f comparf, const char* name, void* context)
414887Schin {
424887Schin 	register char*		lo = (char*)tab;
434887Schin 	register char*		hi = lo + (num - 1) * siz;
444887Schin 	register char*		mid;
454887Schin 	register int		v;
464887Schin 
474887Schin 	while (lo <= hi)
484887Schin 	{
494887Schin 		mid = lo + (((hi - lo) / siz) / 2) * siz;
504887Schin 		if (!(v = context ? (*(Strcmp_context_f)comparf)(name, *((char**)mid), context) : (*comparf)(name, *((char**)mid))))
514887Schin 			return (void*)mid;
524887Schin 		else if (v > 0)
534887Schin 			lo = mid + siz;
544887Schin 		else hi = mid - siz;
554887Schin 	}
564887Schin 	return 0;
574887Schin }
58