LSEARCH(3P)
LSEARCH(3P) π linear search but make it iconic
okay bestie, welcome to the POSIX Programmer's Manual β¨ this one's a little letter called LSEARCH(3P) and it's giving "I just want to find my thing in a table and maybe add it if it's not there" energy. let's get into it π«Ά
πΈ PROLOG πΈ
so like, real talk first: this page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β like, consult the corresponding Linux manual page for the deets on Linux behavior, bestie β or honestly the interface might not even be implemented on Linux at all. π no cap.
β¨ NAME β¨
lsearch, lfind β linear search and update π
π SYNOPSIS π
#include <search.h>
void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
int (*compar)(const void *, const void *));
void *lfind(const void *key, const void *base, size_t *nelp,
size_t width, int (*compar)(const void *, const void *));
π DESCRIPTION π
okay so lsearch() shall linearly search the table and return a pointer into the table for the matching entry π and if that entry does not occur, it shall be added at the end of the table. periodt.
let's break down the cast of characters, because there's a lot of them:
keyβ points to the entry you're looking for in the table πbaseβ points to the first element in the tablewidthβ the size of one element, in bytes πnelpβ points to an integer holding the current number of elements in the table. and listen: that integer to whichnelppoints shall be incremented if the entry gets added to the table πcomparβ points to a comparison function that the application shall supply (for example,strcmp()). it gets called with two arguments that point to the elements being compared. and the application shall ensure that this function returns0if the elements are equal, and non-zero otherwise. no pressure π«Ά
now lfind()? she's the chill twin. lfind() shall be equivalent to lsearch(), EXCEPT β and this is the whole plot twist β if the entry is not found, it is not added to the table. instead, a null pointer is returned. she's not here to expand your table, she's just here to look. π
π₯ RETURN VALUE π₯
if the searched-for entry is found, both lsearch() and lfind() shall return a pointer to it π
otherwise:
lfind()shall return a null pointerlsearch()shall return a pointer to the newly added element
and both functions shall return a null pointer in case of error. π
π ERRORS π
No errors are defined.
and the following sections are informative (iykyk, they're the bonus content, not the rules) β
π¦ EXAMPLES π¦
πΈ Storing Strings in a Table πΈ
this fragment reads in less than or equal to TABSIZE strings of length less than or equal to ELSIZE and stores them in a table, eliminating duplicates. she's efficient, she's clean, she slays β¨
#include <stdio.h>
#include <string.h>
#include <search.h>
#define TABSIZE 50
#define ELSIZE 120
...
char line[ELSIZE], tab[TABSIZE][ELSIZE];
size_t nel = 0;
...
while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE)
(void) lsearch(line, tab, &nel,
ELSIZE, (int (*)(const void *, const void *)) strcmp);
...
π Finding a Matching Entry π
this next one finds any line that reads "Thisisatest.". ok the prose says that, the code says "This is a test.\n" β we're keeping both exactly as written, that's the vibe, don't @ me π«Ά
#include <search.h>
#include <string.h>
...
char line[ELSIZE], tab[TABSIZE][ELSIZE];
size_t nel = 0;
char *findline;
void *entry;
findline = "This is a test.\n";
entry = lfind(findline, tab, &nel, ELSIZE, (
int (*)(const void *, const void *)) strcmp);
π APPLICATION USAGE π
the comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. like, she's flexible, she's not judging your struct π
β οΈ Undefined results can occur if there is not enough room in the table to add a new item. β οΈ fr fr, bestie, make sure your table has room. this is the "check your bag before you leave the house" of this function. π
π§ RATIONALE π§
None.
she simply had nothing to add. silence. iconic. π
β FUTURE DIRECTIONS β
None.
the future is bright and also unchanged β¨
π SEE ALSO π
hcreate()tdelete()- The Base Definitions volume of POSIX.1β2017,
<search.h>
π COPYRIGHT π
okay legal bestie moment, we gotta be respectful π«Ά
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! π brought to you by IEEE/The Open Group, 2017 β yes that's the source and the date, and yes they ate. go find your elements, add them if they're missing, and remember: lsearch adds, lfind just looks. periodt. π
β¨π¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms