xref: /freebsd/lib/libc/stdlib/lsearch.3 (revision 5a39901bc479651f37cebb788cc73f00582d13ec)
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.\"
11*5a39901bSWarren Block.Dd April 17, 2016
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
402efeeba5SRuslan ErmilovThe
416c84d0b1SRobert Drehmel.Fa key
422efeeba5SRuslan Ermilovargument
436c84d0b1SRobert Drehmelpoints to an element that matches the one that is searched.
446c84d0b1SRobert DrehmelThe array's address in memory is denoted by the
456c84d0b1SRobert Drehmel.Fa base
466c84d0b1SRobert Drehmelargument.
47142de08dSRuslan ErmilovThe width of one element (i.e., the size as returned by
486c84d0b1SRobert Drehmel.Fn sizeof )
496c84d0b1SRobert Drehmelis passed as the
506c84d0b1SRobert Drehmel.Fa width
516c84d0b1SRobert Drehmelargument.
526c84d0b1SRobert DrehmelThe number of valid elements contained in the array (not the number of
536c84d0b1SRobert Drehmelelements the array has space reserved for) is given in the integer pointed
546c84d0b1SRobert Drehmelto by
556c84d0b1SRobert Drehmel.Fa nelp .
566c84d0b1SRobert DrehmelThe
576c84d0b1SRobert Drehmel.Fa compar
586c84d0b1SRobert Drehmelargument points to a function which compares its two arguments and returns
59142de08dSRuslan Ermilovzero if they are matching, and non-zero otherwise.
606c84d0b1SRobert Drehmel.Pp
616c84d0b1SRobert DrehmelIf no matching element was found in the array,
626c84d0b1SRobert Drehmel.Fn lsearch
636c84d0b1SRobert Drehmelcopies
646c84d0b1SRobert Drehmel.Fa key
656c84d0b1SRobert Drehmelinto the position after the last element and increments the
666c84d0b1SRobert Drehmelinteger pointed to by
676c84d0b1SRobert Drehmel.Fa nelp .
686c84d0b1SRobert Drehmel.Sh RETURN VALUES
691fae73b1SRuslan ErmilovThe
706c84d0b1SRobert Drehmel.Fn lsearch
716c84d0b1SRobert Drehmeland
726c84d0b1SRobert Drehmel.Fn lfind
731fae73b1SRuslan Ermilovfunctions
746c84d0b1SRobert Drehmelreturn a pointer to the first element found.
756c84d0b1SRobert DrehmelIf no element was found,
766c84d0b1SRobert Drehmel.Fn lsearch
776c84d0b1SRobert Drehmelreturns a pointer to the newly added element, whereas
786c84d0b1SRobert Drehmel.Fn lfind
796c84d0b1SRobert Drehmelreturns
806c84d0b1SRobert Drehmel.Dv NULL .
816c84d0b1SRobert DrehmelBoth functions return
826c84d0b1SRobert Drehmel.Dv NULL
836c84d0b1SRobert Drehmelif an error occurs.
8415f764ddSJoel Dahl.Sh EXAMPLES
8515f764ddSJoel Dahl.Bd -literal
8615f764ddSJoel Dahl#include <search.h>
8715f764ddSJoel Dahl#include <stdio.h>
8815f764ddSJoel Dahl#include <stdlib.h>
8915f764ddSJoel Dahl
9015f764ddSJoel Dahlstatic int
9115f764ddSJoel Dahlelement_compare(const void *p1, const void *p2)
9215f764ddSJoel Dahl{
9315f764ddSJoel Dahl	int left = *(const int *)p1;
9415f764ddSJoel Dahl	int right = *(const int *)p2;
9515f764ddSJoel Dahl
9615f764ddSJoel Dahl	return (left - right);
9715f764ddSJoel Dahl}
9815f764ddSJoel Dahl
9915f764ddSJoel Dahlint
10015f764ddSJoel Dahlmain(int argc, char **argv)
10115f764ddSJoel Dahl{
10215f764ddSJoel Dahl	const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
10315f764ddSJoel Dahl	size_t element_size = sizeof(array[0]);
10415f764ddSJoel Dahl	size_t array_size = sizeof(array) / element_size;
10515f764ddSJoel Dahl	int key;
10615f764ddSJoel Dahl	void *element;
10715f764ddSJoel Dahl
10815f764ddSJoel Dahl	printf("Enter a number: ");
10915f764ddSJoel Dahl	if (scanf("%d", &key) != 1) {
110*5a39901bSWarren Block		printf("Bad input\en");
11115f764ddSJoel Dahl		return (EXIT_FAILURE);
11215f764ddSJoel Dahl	}
11315f764ddSJoel Dahl
11415f764ddSJoel Dahl	element = lfind(&key, array, &array_size, element_size,
11515f764ddSJoel Dahl	    element_compare);
11615f764ddSJoel Dahl
11715f764ddSJoel Dahl	if (element != NULL)
118*5a39901bSWarren Block		printf("Element found: %d\en", *(int *)element);
11915f764ddSJoel Dahl	else
120*5a39901bSWarren Block		printf("Element not found\en");
12115f764ddSJoel Dahl
12215f764ddSJoel Dahl	return (EXIT_SUCCESS);
12315f764ddSJoel Dahl}
12415f764ddSJoel Dahl.Ed
1256c84d0b1SRobert Drehmel.Sh SEE ALSO
1266c84d0b1SRobert Drehmel.Xr bsearch 3 ,
1276c84d0b1SRobert Drehmel.Xr hsearch 3 ,
1286c84d0b1SRobert Drehmel.Xr tsearch 3
12924a0682cSRuslan Ermilov.Sh STANDARDS
13024a0682cSRuslan ErmilovThe
13124a0682cSRuslan Ermilov.Fn lsearch
13224a0682cSRuslan Ermilovand
13324a0682cSRuslan Ermilov.Fn lfind
13424a0682cSRuslan Ermilovfunctions conform to
13524a0682cSRuslan Ermilov.St -p1003.1-2001 .
1366c84d0b1SRobert Drehmel.Sh HISTORY
1376c84d0b1SRobert DrehmelThe
1386c84d0b1SRobert Drehmel.Fn lsearch
1396c84d0b1SRobert Drehmeland
1406c84d0b1SRobert Drehmel.Fn lfind
1416c84d0b1SRobert Drehmelfunctions appeared in
1426c84d0b1SRobert Drehmel.Bx 4.2 .
1436c84d0b1SRobert DrehmelIn
1446c84d0b1SRobert Drehmel.Fx 5.0 ,
1456c84d0b1SRobert Drehmelthey reappeared conforming to
1466c84d0b1SRobert Drehmel.St -p1003.1-2001 .
147