logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

lsearch.3p (4534B)


  1. '\" et
  2. .TH LSEARCH "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. lsearch,
  12. lfind
  13. \(em linear search and update
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <search.h>
  18. .P
  19. void *lsearch(const void *\fIkey\fP, void *\fIbase\fP, size_t *\fInelp\fP, size_t \fIwidth\fP,
  20. int (*\fIcompar\fP)(const void *, const void *));
  21. void *lfind(const void *\fIkey\fP, const void *\fIbase\fP, size_t *\fInelp\fP,
  22. size_t width, int (*\fIcompar\fP)(const void *, const void *));
  23. .fi
  24. .SH DESCRIPTION
  25. The
  26. \fIlsearch\fR()
  27. function shall linearly search the table and return a pointer into the
  28. table for the matching entry. If the entry does not occur, it shall be
  29. added at the end of the table. The
  30. .IR key
  31. argument points to the entry to be sought in the table. The
  32. .IR base
  33. argument points to the first element in the table. The
  34. .IR width
  35. argument is the size of an element in bytes. The
  36. .IR nelp
  37. argument points to an integer containing the current number of elements
  38. in the table. The integer to which
  39. .IR nelp
  40. points shall be incremented if the entry is added to the table. The
  41. .IR compar
  42. argument points to a comparison function which the application shall
  43. supply (for example,
  44. \fIstrcmp\fR()).
  45. It is called with two arguments that point to the elements being
  46. compared. The application shall ensure that the function returns 0
  47. if the elements are equal, and non-zero otherwise.
  48. .P
  49. The
  50. \fIlfind\fR()
  51. function shall be equivalent to
  52. \fIlsearch\fR(),
  53. except that if the entry is not found, it is not added to the table.
  54. Instead, a null pointer is returned.
  55. .SH "RETURN VALUE"
  56. If the searched for entry is found, both
  57. \fIlsearch\fR()
  58. and
  59. \fIlfind\fR()
  60. shall return a pointer to it. Otherwise,
  61. \fIlfind\fR()
  62. shall return a null pointer and
  63. \fIlsearch\fR()
  64. shall return a pointer to the newly added element.
  65. .P
  66. Both functions shall return a null pointer in case of error.
  67. .SH ERRORS
  68. No errors are defined.
  69. .LP
  70. .IR "The following sections are informative."
  71. .SH "EXAMPLES"
  72. .SS "Storing Strings in a Table"
  73. .P
  74. This fragment reads in less than or equal to TABSIZE
  75. strings of length less than or equal to ELSIZE and stores them in a
  76. table, eliminating duplicates.
  77. .sp
  78. .RS 4
  79. .nf
  80. #include <stdio.h>
  81. #include <string.h>
  82. #include <search.h>
  83. .P
  84. #define TABSIZE 50
  85. #define ELSIZE 120
  86. .P
  87. \&...
  88. char line[ELSIZE], tab[TABSIZE][ELSIZE];
  89. size_t nel = 0;
  90. ...
  91. while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE)
  92. (void) lsearch(line, tab, &nel,
  93. ELSIZE, (int (*)(const void *, const void *)) strcmp);
  94. ...
  95. .fi
  96. .P
  97. .RE
  98. .SS "Finding a Matching Entry"
  99. .P
  100. The following example finds any line that reads
  101. .BR \(dqThis is a test.\(dq .
  102. .sp
  103. .RS 4
  104. .nf
  105. #include <search.h>
  106. #include <string.h>
  107. \&...
  108. char line[ELSIZE], tab[TABSIZE][ELSIZE];
  109. size_t nel = 0;
  110. char *findline;
  111. void *entry;
  112. .P
  113. findline = "This is a test.\en";
  114. .P
  115. entry = lfind(findline, tab, &nel, ELSIZE, (
  116. int (*)(const void *, const void *)) strcmp);
  117. .fi
  118. .P
  119. .RE
  120. .SH "APPLICATION USAGE"
  121. The comparison function need not compare every byte, so arbitrary
  122. data may be contained in the elements in addition to the values
  123. being compared.
  124. .P
  125. Undefined results can occur if there is not enough room in the table to
  126. add a new item.
  127. .SH RATIONALE
  128. None.
  129. .SH "FUTURE DIRECTIONS"
  130. None.
  131. .SH "SEE ALSO"
  132. .IR "\fIhcreate\fR\^(\|)",
  133. .IR "\fItdelete\fR\^(\|)"
  134. .P
  135. The Base Definitions volume of POSIX.1\(hy2017,
  136. .IR "\fB<search.h>\fP"
  137. .\"
  138. .SH COPYRIGHT
  139. Portions of this text are reprinted and reproduced in electronic form
  140. from IEEE Std 1003.1-2017, Standard for Information Technology
  141. -- Portable Operating System Interface (POSIX), The Open Group Base
  142. Specifications Issue 7, 2018 Edition,
  143. Copyright (C) 2018 by the Institute of
  144. Electrical and Electronics Engineers, Inc and The Open Group.
  145. In the event of any discrepancy between this version and the original IEEE and
  146. The Open Group Standard, the original IEEE and The Open Group Standard
  147. is the referee document. The original Standard can be obtained online at
  148. http://www.opengroup.org/unix/online.html .
  149. .PP
  150. Any typographical or formatting errors that appear
  151. in this page are most likely
  152. to have been introduced during the conversion of the source files to
  153. man page format. To report such errors, see
  154. https://www.kernel.org/doc/man-pages/reporting_bugs.html .