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.\" 9*5a39901bSWarren Block.Dd April 17, 2016 106c84d0b1SRobert Drehmel.Dt LSEARCH 3 116c84d0b1SRobert Drehmel.Os 126c84d0b1SRobert Drehmel.Sh NAME 136c84d0b1SRobert Drehmel.Nm lsearch , 146c84d0b1SRobert Drehmel.Nm lfind 156c84d0b1SRobert Drehmel.Nd linear search and append 166c84d0b1SRobert Drehmel.Sh LIBRARY 176c84d0b1SRobert Drehmel.Lb libc 186c84d0b1SRobert Drehmel.Sh SYNOPSIS 196c84d0b1SRobert Drehmel.In search.h 20142de08dSRuslan Ermilov.Ft "void *" 21142de08dSRuslan Ermilov.Fo lsearch 22142de08dSRuslan Ermilov.Fa "const void *key" "void *base" "size_t *nelp" "size_t width" 23142de08dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 24142de08dSRuslan Ermilov.Fc 25142de08dSRuslan Ermilov.Ft "void *" 26142de08dSRuslan Ermilov.Fo lfind 27142de08dSRuslan Ermilov.Fa "const void *key" "const void *base" "size_t *nelp" "size_t width" 28142de08dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 29142de08dSRuslan Ermilov.Fc 306c84d0b1SRobert Drehmel.Sh DESCRIPTION 316c84d0b1SRobert DrehmelThe 326c84d0b1SRobert Drehmel.Fn lsearch 336c84d0b1SRobert Drehmeland 346c84d0b1SRobert Drehmel.Fn lfind 356c84d0b1SRobert Drehmelfunctions walk linearly through an array and compare each element with 366c84d0b1SRobert Drehmelthe one to be sought using a supplied comparison function. 376c84d0b1SRobert Drehmel.Pp 382efeeba5SRuslan ErmilovThe 396c84d0b1SRobert Drehmel.Fa key 402efeeba5SRuslan Ermilovargument 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 671fae73b1SRuslan ErmilovThe 686c84d0b1SRobert Drehmel.Fn lsearch 696c84d0b1SRobert Drehmeland 706c84d0b1SRobert Drehmel.Fn lfind 711fae73b1SRuslan Ermilovfunctions 726c84d0b1SRobert Drehmelreturn a pointer to the first element found. 736c84d0b1SRobert DrehmelIf no element was found, 746c84d0b1SRobert Drehmel.Fn lsearch 756c84d0b1SRobert Drehmelreturns a pointer to the newly added element, whereas 766c84d0b1SRobert Drehmel.Fn lfind 776c84d0b1SRobert Drehmelreturns 786c84d0b1SRobert Drehmel.Dv NULL . 796c84d0b1SRobert DrehmelBoth functions return 806c84d0b1SRobert Drehmel.Dv NULL 816c84d0b1SRobert Drehmelif an error occurs. 8215f764ddSJoel Dahl.Sh EXAMPLES 8315f764ddSJoel Dahl.Bd -literal 8415f764ddSJoel Dahl#include <search.h> 8515f764ddSJoel Dahl#include <stdio.h> 8615f764ddSJoel Dahl#include <stdlib.h> 8715f764ddSJoel Dahl 8815f764ddSJoel Dahlstatic int 8915f764ddSJoel Dahlelement_compare(const void *p1, const void *p2) 9015f764ddSJoel Dahl{ 9115f764ddSJoel Dahl int left = *(const int *)p1; 9215f764ddSJoel Dahl int right = *(const int *)p2; 9315f764ddSJoel Dahl 9415f764ddSJoel Dahl return (left - right); 9515f764ddSJoel Dahl} 9615f764ddSJoel Dahl 9715f764ddSJoel Dahlint 9815f764ddSJoel Dahlmain(int argc, char **argv) 9915f764ddSJoel Dahl{ 10015f764ddSJoel Dahl const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 10115f764ddSJoel Dahl size_t element_size = sizeof(array[0]); 10215f764ddSJoel Dahl size_t array_size = sizeof(array) / element_size; 10315f764ddSJoel Dahl int key; 10415f764ddSJoel Dahl void *element; 10515f764ddSJoel Dahl 10615f764ddSJoel Dahl printf("Enter a number: "); 10715f764ddSJoel Dahl if (scanf("%d", &key) != 1) { 108*5a39901bSWarren Block printf("Bad input\en"); 10915f764ddSJoel Dahl return (EXIT_FAILURE); 11015f764ddSJoel Dahl } 11115f764ddSJoel Dahl 11215f764ddSJoel Dahl element = lfind(&key, array, &array_size, element_size, 11315f764ddSJoel Dahl element_compare); 11415f764ddSJoel Dahl 11515f764ddSJoel Dahl if (element != NULL) 116*5a39901bSWarren Block printf("Element found: %d\en", *(int *)element); 11715f764ddSJoel Dahl else 118*5a39901bSWarren Block printf("Element not found\en"); 11915f764ddSJoel Dahl 12015f764ddSJoel Dahl return (EXIT_SUCCESS); 12115f764ddSJoel Dahl} 12215f764ddSJoel Dahl.Ed 1236c84d0b1SRobert Drehmel.Sh SEE ALSO 1246c84d0b1SRobert Drehmel.Xr bsearch 3 , 1256c84d0b1SRobert Drehmel.Xr hsearch 3 , 1266c84d0b1SRobert Drehmel.Xr tsearch 3 12724a0682cSRuslan Ermilov.Sh STANDARDS 12824a0682cSRuslan ErmilovThe 12924a0682cSRuslan Ermilov.Fn lsearch 13024a0682cSRuslan Ermilovand 13124a0682cSRuslan Ermilov.Fn lfind 13224a0682cSRuslan Ermilovfunctions conform to 13324a0682cSRuslan Ermilov.St -p1003.1-2001 . 1346c84d0b1SRobert Drehmel.Sh HISTORY 1356c84d0b1SRobert DrehmelThe 1366c84d0b1SRobert Drehmel.Fn lsearch 1376c84d0b1SRobert Drehmeland 1386c84d0b1SRobert Drehmel.Fn lfind 1396c84d0b1SRobert Drehmelfunctions appeared in 1406c84d0b1SRobert Drehmel.Bx 4.2 . 1416c84d0b1SRobert DrehmelIn 1426c84d0b1SRobert Drehmel.Fx 5.0 , 1436c84d0b1SRobert Drehmelthey reappeared conforming to 1446c84d0b1SRobert Drehmel.St -p1003.1-2001 . 145