Files
oldlinux-files/study/Ref-docs/POSIX/susv3/functions/lsearch.html
2024-02-19 00:25:23 -05:00

180 lines
6.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>lsearch</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="lsearch"></a> <a name="tag_03_353"></a><!-- lsearch -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_353_01"></a>NAME</h4>
<blockquote>lsearch, lfind - linear search and update</blockquote>
<h4><a name="tag_03_353_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> #include &lt;<a href="../basedefs/search.h.html">search.h</a>&gt;<br>
<br>
void *lsearch(const void *</tt><i>key</i><tt>, void *</tt><i>base</i><tt>, size_t *</tt><i>nelp</i><tt>, size_t</tt>
<i>width</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int (*</tt><i>compar</i><tt>)(const void *, const void *));<br>
void *lfind(const void *</tt><i>key</i><tt>, const void *</tt><i>base</i><tt>, size_t *</tt><i>nelp</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t width, int (*</tt><i>compar</i><tt>)(const void *, const void *)); <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div>
<tt><br>
</tt></blockquote>
<h4><a name="tag_03_353_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>lsearch</i>() function shall linearly search the table and return a pointer into the table for the matching entry. If the
entry does not occur, it shall be added at the end of the table. The <i>key</i> argument points to the entry to be sought in the
table. The <i>base</i> argument points to the first element in the table. The <i>width</i> argument is the size of an element in
bytes. The <i>nelp</i> argument points to an integer containing the current number of elements in the table. The integer to which
<i>nelp</i> points shall be incremented if the entry is added to the table. The <i>compar</i> argument points to a comparison
function which the application shall supply (for example, <a href="../functions/strcmp.html"><i>strcmp</i>()</a>). It is called
with two arguments that point to the elements being compared. The application shall ensure that the function returns 0 if the
elements are equal, and non-zero otherwise.</p>
<p>The <i>lfind</i>() function shall be equivalent to <i>lsearch</i>(), except that if the entry is not found, it is not added to
the table. Instead, a null pointer is returned.</p>
</blockquote>
<h4><a name="tag_03_353_04"></a>RETURN VALUE</h4>
<blockquote>
<p>If the searched for entry is found, both <i>lsearch</i>() and <i>lfind</i>() shall return a pointer to it. Otherwise,
<i>lfind</i>() shall return a null pointer and <i>lsearch</i>() shall return a pointer to the newly added element.</p>
<p>Both functions shall return a null pointer in case of error.</p>
</blockquote>
<h4><a name="tag_03_353_05"></a>ERRORS</h4>
<blockquote>
<p>No errors are defined.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_353_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_353_06_01"></a>Storing Strings in a Table</h5>
<p>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.</p>
<pre>
<tt>#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;search.h&gt;
<br>
#define TABSIZE 50
#define ELSIZE 120
<br>
...
char line[ELSIZE], tab[TABSIZE][ELSIZE];
size_t nel = 0;
...
while (fgets(line, ELSIZE, stdin) != NULL &amp;&amp; nel &lt; TABSIZE)
(void) lsearch(line, tab, &amp;nel,
ELSIZE, (int (*)(const void *, const void *)) strcmp);
...
</tt>
</pre>
<h5><a name="tag_03_353_06_02"></a>Finding a Matching Entry</h5>
<p>The following example finds any line that reads <tt>"This is a test."</tt> .</p>
<pre>
<tt>#include &lt;search.h&gt;
#include &lt;string.h&gt;
...
char line[ELSIZE], tab[TABSIZE][ELSIZE];
size_t nel = 0;
char *findline;
void *entry;
<br>
findline = "This is a test.\n";
<br>
entry = lfind(findline, tab, &amp;nel, ELSIZE, (
int (*)(const void *, const void *)) strcmp);
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_353_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the
values being compared.</p>
<p>Undefined results can occur if there is not enough room in the table to add a new item.</p>
</blockquote>
<h4><a name="tag_03_353_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_353_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_353_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="hcreate.html"><i>hcreate</i>()</a> , <a href="tsearch.html"><i>tsearch</i>()</a> , the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/search.h.html"><i>&lt;search.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_353_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<h4><a name="tag_03_353_12"></a>Issue 6</h4>
<blockquote>
<p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>