manpages: Uniformly order the prologue macros by Dd/Dt/Os.
[dragonfly.git] / lib / libc / stdlib / hcreate.3
1 .\"-
2 .\" Copyright (c) 1999 The NetBSD Foundation, Inc.
3 .\" All rights reserved.
4 .\"
5 .\" This code is derived from software contributed to The NetBSD Foundation
6 .\" by Klaus Klein.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\"
17 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 .\" POSSIBILITY OF SUCH DAMAGE.
28 .\"
29 .\" $FreeBSD: src/lib/libc/stdlib/hcreate.3,v 1.7 2008/07/06 17:03:37 danger Exp $
30 .\"
31 .Dd July 6, 2008
32 .Dt HCREATE 3
33 .Os
34 .Sh NAME
35 .Nm hcreate ,
36 .Nm hdestroy ,
37 .Nm hsearch
38 .Nd manage hash search table
39 .Sh LIBRARY
40 .Lb libc
41 .Sh SYNOPSIS
42 .In search.h
43 .Ft int
44 .Fn hcreate "size_t nel"
45 .Ft void
46 .Fn hdestroy void
47 .Ft ENTRY *
48 .Fn hsearch "ENTRY item" "ACTION action"
49 .Sh DESCRIPTION
50 The
51 .Fn hcreate ,
52 .Fn hdestroy ,
53 and
54 .Fn hsearch
55 functions manage hash search tables.
56 .Pp
57 The
58 .Fn hcreate
59 function allocates sufficient space for the table, and the application should
60 ensure it is called before
61 .Fn hsearch
62 is used.
63 The
64 .Fa nel
65 argument is an estimate of the maximum
66 number of entries that the table should contain.
67 This number may be adjusted upward by the
68 algorithm in order to obtain certain mathematically favorable circumstances.
69 .Pp
70 The
71 .Fn hdestroy
72 function disposes of the search table, and may be followed by another call to
73 .Fn hcreate .
74 After the call to
75 .Fn hdestroy ,
76 the data can no longer be considered accessible.
77 The
78 .Fn hdestroy
79 function calls
80 .Xr free 3
81 for each comparison key in the search table
82 but not the data item associated with the key.
83 .Pp
84 The
85 .Fn hsearch
86 function is a hash-table search routine.
87 It returns a pointer into a hash table
88 indicating the location at which an entry can be found.
89 The
90 .Fa item
91 argument is a structure of type
92 .Vt ENTRY
93 (defined in the
94 .In search.h
95 header) containing two pointers:
96 .Fa item.key
97 points to the comparison key (a
98 .Vt "char *" ) ,
99 and
100 .Fa item.data
101 (a
102 .Vt "void *" )
103 points to any other data to be associated with
104 that key.
105 The comparison function used by
106 .Fn hsearch
107 is
108 .Xr strcmp 3 .
109 The
110 .Fa action
111 argument is a
112 member of an enumeration type
113 .Vt ACTION
114 indicating the disposition of the entry if it cannot be
115 found in the table.
116 .Dv ENTER
117 indicates that the
118 .Fa item
119 should be inserted in the table at an
120 appropriate point.
121 .Dv FIND
122 indicates that no entry should be made.
123 Unsuccessful resolution is
124 indicated by the return of a
125 .Dv NULL
126 pointer.
127 .Pp
128 The comparison key (passed to
129 .Fn hsearch
130 as
131 .Fa item.key )
132 must be allocated using
133 .Xr malloc 3
134 if
135 .Fa action
136 is
137 .Dv ENTER
138 and
139 .Fn hdestroy
140 is called.
141 .Sh RETURN VALUES
142 The
143 .Fn hcreate
144 function returns 0 if the table creation failed and the global variable
145 .Va errno
146 is set to indicate the error;
147 otherwise, a non-zero value is returned.
148 .Pp
149 The
150 .Fn hdestroy
151 function does not return a value.
152 .Pp
153 The
154 .Fn hsearch
155 function returns a
156 .Dv NULL
157 pointer if either the
158 .Fa action
159 is
160 .Dv FIND
161 and the
162 .Fa item
163 could not be found or the
164 .Fa action
165 is
166 .Dv ENTER
167 and the table is full.
168 .Sh EXAMPLES
169 The following example reads in strings followed by two numbers
170 and stores them in a hash table, discarding duplicates.
171 It then reads in strings and finds the matching entry in the hash
172 table and prints it out.
173 .Bd -literal
174 #include <stdio.h>
175 #include <search.h>
176 #include <string.h>
177 #include <stdlib.h>
178
179 struct info {                   /* This is the info stored in the table */
180         int age, room;          /* other than the key. */
181 };
182
183 #define NUM_EMPL        5000    /* # of elements in search table. */
184
185 int
186 main(void)
187 {
188         char str[BUFSIZ]; /* Space to read string */
189         struct info info_space[NUM_EMPL]; /* Space to store employee info. */
190         struct info *info_ptr = info_space; /* Next space in info_space. */
191         ENTRY item;
192         ENTRY *found_item; /* Name to look for in table. */
193         char name_to_find[30];
194         int i = 0;
195
196         /* Create table; no error checking is performed. */
197         (void) hcreate(NUM_EMPL);
198
199         while (scanf("%s%d%d", str, &info_ptr->age,
200             &info_ptr->room) != EOF && i++ < NUM_EMPL) {
201                 /* Put information in structure, and structure in item. */
202                 item.key = strdup(str);
203                 item.data = info_ptr;
204                 info_ptr++;
205                 /* Put item into table. */
206                 (void) hsearch(item, ENTER);
207         }
208
209         /* Access table. */
210         item.key = name_to_find;
211         while (scanf("%s", item.key) != EOF) {
212                 if ((found_item = hsearch(item, FIND)) != NULL) {
213                         /* If item is in the table. */
214                         (void)printf("found %s, age = %d, room = %d\en",
215                             found_item->key,
216                             ((struct info *)found_item->data)->age,
217                             ((struct info *)found_item->data)->room);
218                 } else
219                         (void)printf("no such employee %s\en", name_to_find);
220         }
221         hdestroy();
222         return 0;
223 }
224 .Ed
225 .Sh ERRORS
226 The
227 .Fn hcreate
228 and
229 .Fn hsearch
230 functions may fail if:
231 .Bl -tag -width Er
232 .It Bq Er ENOMEM
233 Insufficient storage space is available.
234 .It Bq Er EINVAL
235 A table already exists.
236 .El
237 .Sh SEE ALSO
238 .Xr bsearch 3 ,
239 .Xr lsearch 3 ,
240 .Xr malloc 3 ,
241 .Xr strcmp 3 ,
242 .Xr tsearch 3
243 .Sh STANDARDS
244 The
245 .Fn hcreate ,
246 .Fn hdestroy ,
247 and
248 .Fn hsearch
249 functions conform to
250 .St -xpg4.2 .
251 .Sh HISTORY
252 The
253 .Fn hcreate ,
254 .Fn hdestroy ,
255 and
256 .Fn hsearch
257 functions first appeared in
258 .At V .
259 .Sh BUGS
260 The interface permits the use of only one hash table at a time.