[comp.os.minix] lsearch

tholm@uvicctr.UUCP (Terrence W. Holm) (09/14/88)

EFTH MINIX report #44  - September 1988 -  lsearch(3) & lfind(3)


There follows an implementation of lsearch(3) and lfind(3)
for MINIX. Please consider this public domain software. A
"man" page is included.

----------------------------------------------------------
echo x - lsearch.3
gres '^X' '' > lsearch.3 << '/'
XSUBROUTINES
X    lsearch(3)		- linear search
X
XINVOCATION
X    char *lsearch( key, base, count, width, keycmp )
X      char *key;
X      char *base;
X      unsigned int *count;
X      unsigned int  width;
X      int  (*keycmp)();
X
X    char *lfind( key, base, count, width, keycmp )
X      char *key;
X      char *base;
X      unsigned int *count;
X      unsigned int  width;
X      int  (*keycmp)();
X
XEXPLANATION
X    Lsearch(3) and lfind(3) scan through a list of items until
X    one matches <key>. The list starts at <base> and contains
X    *<count> entries of size <width>. The entries are compared
X    using keycmp(key,entry); this routine should return 0 for
X    matching items, non-zero otherwise.
X
X    If the item is not found then lsearch(3) adds it to the end
X    of the list, and *<count> is updated. Lfind(3) does not append
X    items if they are not found.
X
XRESULTS
X    NULL : Not found, lfind(3) only.
X    o/w  : Pointer to matching entry.
X
XREFERENCES
X    bsearch(3), tsearch(3)
/
echo x - lsearch.c
gres '^X' '' > lsearch.c << '/'
X/*  lsearch(3)  and  lfind(3)
X *
X *  Author: Terrence W. Holm          Sep. 1988
X */
X
X#define  NULL  (char *) 0
X
X
Xchar *lsearch( key, base, count, width, keycmp )
X  char *key;
X  char *base;
X  unsigned *count;
X  unsigned  width;
X  int  (*keycmp)();
X
X  {
X  char *entry;
X  char *last = base + *count * width;
X
X  for ( entry = base;  entry < last;  entry += width )
X    if ( keycmp( key, entry ) == 0 )
X	return( entry );
X
X  bcopy( key, last, width );
X  *count += 1;
X  return( last );
X  }
X
X
X
Xchar *lfind( key, base, count, width, keycmp )
X  char *key;
X  char *base;
X  unsigned *count;
X  unsigned  width;
X  int  (*keycmp)();
X
X  {
X  char *entry;
X  char *last = base + *count * width;
X
X  for ( entry = base;  entry < last;  entry += width )
X    if ( keycmp( key, entry ) == 0 )
X	return( entry );
X
X  return( NULL );
X  }
/
----------------------------------------------------------

		Edwin L. Froese
		  uw-beaver!ubc-cs!mprg!handel!froese

		Terrence W. Holm
		  uw-beaver!uvicctr!tholm