xref: /freebsd/lib/libc/stdlib/lsearch.3 (revision 142de08d3a1eb82b259549c4a84edcb39b85414e)
16c84d0b1SRobert Drehmel.\"
26c84d0b1SRobert Drehmel.\" Initial implementation:
36c84d0b1SRobert Drehmel.\" Copyright (c) 2002 Robert Drehmel
46c84d0b1SRobert Drehmel.\" All rights reserved.
56c84d0b1SRobert Drehmel.\"
66c84d0b1SRobert Drehmel.\" As long as the above copyright statement and this notice remain
76c84d0b1SRobert Drehmel.\" unchanged, you can do what ever you want with this file.
86c84d0b1SRobert Drehmel.\"
96c84d0b1SRobert Drehmel.\" $FreeBSD$
106c84d0b1SRobert Drehmel.\"
116c84d0b1SRobert Drehmel.Dd October 11, 2002
126c84d0b1SRobert Drehmel.Dt LSEARCH 3
136c84d0b1SRobert Drehmel.Os
146c84d0b1SRobert Drehmel.Sh NAME
156c84d0b1SRobert Drehmel.Nm lsearch ,
166c84d0b1SRobert Drehmel.Nm lfind
176c84d0b1SRobert Drehmel.Nd linear search and append
186c84d0b1SRobert Drehmel.Sh LIBRARY
196c84d0b1SRobert Drehmel.Lb libc
206c84d0b1SRobert Drehmel.Sh SYNOPSIS
216c84d0b1SRobert Drehmel.In search.h
22142de08dSRuslan Ermilov.Ft "void *"
23142de08dSRuslan Ermilov.Fo lsearch
24142de08dSRuslan Ermilov.Fa "const void *key" "void *base" "size_t *nelp" "size_t width"
25142de08dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
26142de08dSRuslan Ermilov.Fc
27142de08dSRuslan Ermilov.Ft "void *"
28142de08dSRuslan Ermilov.Fo lfind
29142de08dSRuslan Ermilov.Fa "const void *key" "const void *base" "size_t *nelp" "size_t width"
30142de08dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
31142de08dSRuslan Ermilov.Fc
326c84d0b1SRobert Drehmel.Sh DESCRIPTION
336c84d0b1SRobert DrehmelThe
346c84d0b1SRobert Drehmel.Fn lsearch
356c84d0b1SRobert Drehmeland
366c84d0b1SRobert Drehmel.Fn lfind
376c84d0b1SRobert Drehmelfunctions walk linearly through an array and compare each element with
386c84d0b1SRobert Drehmelthe one to be sought using a supplied comparison function.
396c84d0b1SRobert Drehmel.Pp
406c84d0b1SRobert Drehmel.Fa key
416c84d0b1SRobert Drehmelpoints to an element that matches the one that is searched.
426c84d0b1SRobert DrehmelThe array's address in memory is denoted by the
436c84d0b1SRobert Drehmel.Fa base
446c84d0b1SRobert Drehmelargument.
45142de08dSRuslan ErmilovThe width of one element (i.e., the size as returned by
466c84d0b1SRobert Drehmel.Fn sizeof )
476c84d0b1SRobert Drehmelis passed as the
486c84d0b1SRobert Drehmel.Fa width
496c84d0b1SRobert Drehmelargument.
506c84d0b1SRobert DrehmelThe number of valid elements contained in the array (not the number of
516c84d0b1SRobert Drehmelelements the array has space reserved for) is given in the integer pointed
526c84d0b1SRobert Drehmelto by
536c84d0b1SRobert Drehmel.Fa nelp .
546c84d0b1SRobert DrehmelThe
556c84d0b1SRobert Drehmel.Fa compar
566c84d0b1SRobert Drehmelargument points to a function which compares its two arguments and returns
57142de08dSRuslan Ermilovzero if they are matching, and non-zero otherwise.
586c84d0b1SRobert Drehmel.Pp
596c84d0b1SRobert DrehmelIf no matching element was found in the array,
606c84d0b1SRobert Drehmel.Fn lsearch
616c84d0b1SRobert Drehmelcopies
626c84d0b1SRobert Drehmel.Fa key
636c84d0b1SRobert Drehmelinto the position after the last element and increments the
646c84d0b1SRobert Drehmelinteger pointed to by
656c84d0b1SRobert Drehmel.Fa nelp .
666c84d0b1SRobert Drehmel.Sh RETURN VALUES
676c84d0b1SRobert Drehmel.Fn lsearch
686c84d0b1SRobert Drehmeland
696c84d0b1SRobert Drehmel.Fn lfind
706c84d0b1SRobert Drehmelreturn a pointer to the first element found.
716c84d0b1SRobert DrehmelIf no element was found,
726c84d0b1SRobert Drehmel.Fn lsearch
736c84d0b1SRobert Drehmelreturns a pointer to the newly added element, whereas
746c84d0b1SRobert Drehmel.Fn lfind
756c84d0b1SRobert Drehmelreturns
766c84d0b1SRobert Drehmel.Dv NULL .
776c84d0b1SRobert DrehmelBoth functions return
786c84d0b1SRobert Drehmel.Dv NULL
796c84d0b1SRobert Drehmelif an error occurs.
806c84d0b1SRobert Drehmel.Sh SEE ALSO
816c84d0b1SRobert Drehmel.Xr bsearch 3 ,
826c84d0b1SRobert Drehmel.Xr hsearch 3 ,
836c84d0b1SRobert Drehmel.Xr tsearch 3
846c84d0b1SRobert Drehmel.Sh HISTORY
856c84d0b1SRobert DrehmelThe
866c84d0b1SRobert Drehmel.Fn lsearch
876c84d0b1SRobert Drehmeland
886c84d0b1SRobert Drehmel.Fn lfind
896c84d0b1SRobert Drehmelfunctions appeared in
906c84d0b1SRobert Drehmel.Bx 4.2 .
916c84d0b1SRobert DrehmelIn
926c84d0b1SRobert Drehmel.Fx 5.0 ,
936c84d0b1SRobert Drehmelthey reappeared conforming to
946c84d0b1SRobert Drehmel.St -p1003.1-2001 .
956c84d0b1SRobert Drehmel.Sh STANDARDS
966c84d0b1SRobert DrehmelThe
976c84d0b1SRobert Drehmel.Fn lsearch
986c84d0b1SRobert Drehmeland
996c84d0b1SRobert Drehmel.Fn lfind
1006c84d0b1SRobert Drehmelfunctions conform to
1016c84d0b1SRobert Drehmel.St -p1003.1-2001 .
102