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

hcreate.3p (5922B)


  1. '\" et
  2. .TH HCREATE "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. hcreate,
  12. hdestroy,
  13. hsearch
  14. \(em manage hash search table
  15. .SH SYNOPSIS
  16. .LP
  17. .nf
  18. #include <search.h>
  19. .P
  20. int hcreate(size_t \fInel\fP);
  21. void hdestroy(void);
  22. ENTRY *hsearch(ENTRY \fIitem\fP, ACTION \fIaction\fP);
  23. .fi
  24. .SH DESCRIPTION
  25. The
  26. \fIhcreate\fR(),
  27. \fIhdestroy\fR(),
  28. and
  29. \fIhsearch\fR()
  30. functions shall manage hash search tables.
  31. .P
  32. The
  33. \fIhcreate\fR()
  34. function shall allocate sufficient space for the table, and the
  35. application shall ensure it is called before
  36. \fIhsearch\fR()
  37. is used. The
  38. .IR nel
  39. argument is an estimate of the maximum number of entries that the table
  40. shall contain. This number may be adjusted upward by the algorithm in
  41. order to obtain certain mathematically favorable circumstances.
  42. .P
  43. The
  44. \fIhdestroy\fR()
  45. function shall dispose of the search table, and may be followed by
  46. another call to
  47. \fIhcreate\fR().
  48. After the call to
  49. \fIhdestroy\fR(),
  50. the data can no longer be considered accessible.
  51. .P
  52. The
  53. \fIhsearch\fR()
  54. function is a hash-table search routine. It shall return a pointer into a
  55. hash table indicating the location at which an entry can be found. The
  56. .IR item
  57. argument is a structure of type
  58. .BR ENTRY
  59. (defined in the
  60. .IR <search.h>
  61. header) containing two pointers:
  62. .IR item.key
  63. points to the comparison key (a
  64. .BR "char *" ),
  65. and
  66. .IR item.data
  67. (a
  68. .BR "void *" )
  69. points to any other data to be associated with that key. The
  70. comparison function used by
  71. \fIhsearch\fR()
  72. is
  73. \fIstrcmp\fR().
  74. The
  75. .IR action
  76. argument is a member of an enumeration type
  77. .BR ACTION
  78. indicating the disposition of the entry if it cannot be found in the
  79. table. ENTER indicates that the item should be inserted in the table
  80. at an appropriate point. FIND indicates that no entry should be made.
  81. Unsuccessful resolution is indicated by the return of a null pointer.
  82. .P
  83. These functions need not be thread-safe.
  84. .SH "RETURN VALUE"
  85. The
  86. \fIhcreate\fR()
  87. function shall return 0 if it cannot allocate sufficient space for the
  88. table; otherwise, it shall return non-zero.
  89. .P
  90. The
  91. \fIhdestroy\fR()
  92. function shall not return a value.
  93. .P
  94. The
  95. \fIhsearch\fR()
  96. function shall return a null pointer if either the action is FIND and
  97. the item could not be found or the action is ENTER and the table is
  98. full.
  99. .SH ERRORS
  100. The
  101. \fIhcreate\fR()
  102. and
  103. \fIhsearch\fR()
  104. functions may fail if:
  105. .TP
  106. .BR ENOMEM
  107. Insufficient storage space is available.
  108. .LP
  109. .IR "The following sections are informative."
  110. .SH "EXAMPLES"
  111. The following example reads in strings followed by two numbers and
  112. stores them in a hash table, discarding duplicates. It then reads in
  113. strings and finds the matching entry in the hash table and prints it
  114. out.
  115. .sp
  116. .RS 4
  117. .nf
  118. #include <stdio.h>
  119. #include <search.h>
  120. #include <string.h>
  121. .P
  122. struct info { /* This is the info stored in the table */
  123. int age, room; /* other than the key. */
  124. };
  125. .P
  126. #define NUM_EMPL 5000 /* # of elements in search table. */
  127. .P
  128. int main(void)
  129. {
  130. char string_space[NUM_EMPL*20]; /* Space to store strings. */
  131. struct info info_space[NUM_EMPL]; /* Space to store employee info. */
  132. char *str_ptr = string_space; /* Next space in string_space. */
  133. struct info *info_ptr = info_space;
  134. /* Next space in info_space. */
  135. ENTRY item;
  136. ENTRY *found_item; /* Name to look for in table. */
  137. char name_to_find[30];
  138. .P
  139. int i = 0;
  140. .P
  141. /* Create table; no error checking is performed. */
  142. (void) hcreate(NUM_EMPL);
  143. while (scanf("%s%d%d", str_ptr, &info_ptr->age,
  144. &info_ptr->room) != EOF && i++ < NUM_EMPL) {
  145. .P
  146. /* Put information in structure, and structure in item. */
  147. item.key = str_ptr;
  148. item.data = info_ptr;
  149. str_ptr += strlen(str_ptr) + 1;
  150. info_ptr++;
  151. .P
  152. /* Put item into table. */
  153. (void) hsearch(item, ENTER);
  154. }
  155. .P
  156. /* Access table. */
  157. item.key = name_to_find;
  158. while (scanf("%s", item.key) != EOF) {
  159. if ((found_item = hsearch(item, FIND)) != NULL) {
  160. .P
  161. /* If item is in the table. */
  162. (void)printf("found %s, age = %d, room = %d\en",
  163. found_item->key,
  164. ((struct info *)found_item->data)->age,
  165. ((struct info *)found_item->data)->room);
  166. } else
  167. (void)printf("no such employee %s\en", name_to_find);
  168. }
  169. return 0;
  170. }
  171. .fi
  172. .P
  173. .RE
  174. .SH "APPLICATION USAGE"
  175. The
  176. \fIhcreate\fR()
  177. and
  178. \fIhsearch\fR()
  179. functions may use
  180. \fImalloc\fR()
  181. to allocate space.
  182. .SH RATIONALE
  183. None.
  184. .SH "FUTURE DIRECTIONS"
  185. None.
  186. .SH "SEE ALSO"
  187. .IR "\fIbsearch\fR\^(\|)",
  188. .IR "\fIlsearch\fR\^(\|)",
  189. .IR "\fImalloc\fR\^(\|)",
  190. .IR "\fIstrcmp\fR\^(\|)",
  191. .IR "\fItdelete\fR\^(\|)"
  192. .P
  193. The Base Definitions volume of POSIX.1\(hy2017,
  194. .IR "\fB<search.h>\fP"
  195. .\"
  196. .SH COPYRIGHT
  197. Portions of this text are reprinted and reproduced in electronic form
  198. from IEEE Std 1003.1-2017, Standard for Information Technology
  199. -- Portable Operating System Interface (POSIX), The Open Group Base
  200. Specifications Issue 7, 2018 Edition,
  201. Copyright (C) 2018 by the Institute of
  202. Electrical and Electronics Engineers, Inc and The Open Group.
  203. In the event of any discrepancy between this version and the original IEEE and
  204. The Open Group Standard, the original IEEE and The Open Group Standard
  205. is the referee document. The original Standard can be obtained online at
  206. http://www.opengroup.org/unix/online.html .
  207. .PP
  208. Any typographical or formatting errors that appear
  209. in this page are most likely
  210. to have been introduced during the conversion of the source files to
  211. man page format. To report such errors, see
  212. https://www.kernel.org/doc/man-pages/reporting_bugs.html .