Path: utzoo!utgpu!water!watmath!clyde!att!rutgers!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm From: tholm@uvicctr.UUCP (Terrence W. Holm) Newsgroups: comp.os.minix Subject: lsearch(3) & lfind(3) Message-ID: <500@uvicctr.UUCP> Date: 14 Sep 88 01:21:52 GMT Reply-To: tholm@uvicctr.UUCP (Terrence W. Holm) Organization: University of Victoria, Victoria B.C. Canada Lines: 103 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 . The list starts at and contains X * entries of size . 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 * 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