xref: /freebsd/lib/libc/stdlib/hcreate.3 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
15fd5badfSDaniel Gerzo.\"-
25fd5badfSDaniel Gerzo.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
35fd5badfSDaniel Gerzo.\" All rights reserved.
45fd5badfSDaniel Gerzo.\"
55fd5badfSDaniel Gerzo.\" This code is derived from software contributed to The NetBSD Foundation
65fd5badfSDaniel Gerzo.\" by Klaus Klein.
75fd5badfSDaniel Gerzo.\"
85fd5badfSDaniel Gerzo.\" Redistribution and use in source and binary forms, with or without
95fd5badfSDaniel Gerzo.\" modification, are permitted provided that the following conditions
105fd5badfSDaniel Gerzo.\" are met:
115fd5badfSDaniel Gerzo.\" 1. Redistributions of source code must retain the above copyright
125fd5badfSDaniel Gerzo.\"    notice, this list of conditions and the following disclaimer.
135fd5badfSDaniel Gerzo.\" 2. Redistributions in binary form must reproduce the above copyright
145fd5badfSDaniel Gerzo.\"    notice, this list of conditions and the following disclaimer in the
155fd5badfSDaniel Gerzo.\"    documentation and/or other materials provided with the distribution.
165fd5badfSDaniel Gerzo.\"
175fd5badfSDaniel Gerzo.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
185fd5badfSDaniel Gerzo.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
195fd5badfSDaniel Gerzo.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
205fd5badfSDaniel Gerzo.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
215fd5badfSDaniel Gerzo.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
225fd5badfSDaniel Gerzo.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
235fd5badfSDaniel Gerzo.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245fd5badfSDaniel Gerzo.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
255fd5badfSDaniel Gerzo.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265fd5badfSDaniel Gerzo.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
275fd5badfSDaniel Gerzo.\" POSSIBILITY OF SUCH DAMAGE.
285fd5badfSDaniel Gerzo.\"
29*d5f154d3SEnji Cooper.Dd February 6, 2017
30ec7d1254SRuslan Ermilov.Dt HCREATE 3
31aa12cea2SUlrich Spörlein.Os
32ec7d1254SRuslan Ermilov.Sh NAME
339823a90cSPedro F. Giffuni.Nm hcreate ,
349823a90cSPedro F. Giffuni.Nm hcreate_r ,
359823a90cSPedro F. Giffuni.Nm hdestroy ,
369823a90cSPedro F. Giffuni.Nm hdestroy_r ,
379823a90cSPedro F. Giffuni.Nm hsearch ,
389823a90cSPedro F. Giffuni.Nm hsearch_r
39ec7d1254SRuslan Ermilov.Nd manage hash search table
40ec7d1254SRuslan Ermilov.Sh LIBRARY
41ec7d1254SRuslan Ermilov.Lb libc
42ec7d1254SRuslan Ermilov.Sh SYNOPSIS
43ec7d1254SRuslan Ermilov.In search.h
44ec7d1254SRuslan Ermilov.Ft int
45ec7d1254SRuslan Ermilov.Fn hcreate "size_t nel"
469823a90cSPedro F. Giffuni.Ft int
479823a90cSPedro F. Giffuni.Fn hcreate_r "size_t nel" "struct hsearch_data *table"
48ec7d1254SRuslan Ermilov.Ft void
499823a90cSPedro F. Giffuni.Fn hdestroy "void"
509823a90cSPedro F. Giffuni.Ft void
519823a90cSPedro F. Giffuni.Fn hdestroy_r "struct hsearch_data *table"
52ec7d1254SRuslan Ermilov.Ft ENTRY *
53ec7d1254SRuslan Ermilov.Fn hsearch "ENTRY item" "ACTION action"
549823a90cSPedro F. Giffuni.Ft int
559823a90cSPedro F. Giffuni.Fn hsearch_r "ENTRY item" "ACTION action" "ENTRY ** itemp" "struct hsearch_data *table"
56ec7d1254SRuslan Ermilov.Sh DESCRIPTION
57ec7d1254SRuslan ErmilovThe
58ec7d1254SRuslan Ermilov.Fn hcreate ,
599823a90cSPedro F. Giffuni.Fn hcreate_r ,
60ec7d1254SRuslan Ermilov.Fn hdestroy ,
619823a90cSPedro F. Giffuni.Fn hdestroy_r
629823a90cSPedro F. Giffuni.Fn hsearch ,
63ec7d1254SRuslan Ermilovand
649823a90cSPedro F. Giffuni.Fn hsearch_r
65ec7d1254SRuslan Ermilovfunctions manage hash search tables.
66ec7d1254SRuslan Ermilov.Pp
67ec7d1254SRuslan ErmilovThe
68ec7d1254SRuslan Ermilov.Fn hcreate
69ec7d1254SRuslan Ermilovfunction allocates sufficient space for the table, and the application should
70ec7d1254SRuslan Ermilovensure it is called before
71ec7d1254SRuslan Ermilov.Fn hsearch
72ec7d1254SRuslan Ermilovis used.
73ec7d1254SRuslan ErmilovThe
74ec7d1254SRuslan Ermilov.Fa nel
75ec7d1254SRuslan Ermilovargument is an estimate of the maximum
76ec7d1254SRuslan Ermilovnumber of entries that the table should contain.
772747eff1SEd SchoutenAs this implementation resizes the hash table dynamically,
782747eff1SEd Schoutenthis argument is ignored.
79ec7d1254SRuslan Ermilov.Pp
80ec7d1254SRuslan ErmilovThe
81ec7d1254SRuslan Ermilov.Fn hdestroy
82ec7d1254SRuslan Ermilovfunction disposes of the search table, and may be followed by another call to
83ec7d1254SRuslan Ermilov.Fn hcreate .
84ec7d1254SRuslan ErmilovAfter the call to
85ec7d1254SRuslan Ermilov.Fn hdestroy ,
86ec7d1254SRuslan Ermilovthe data can no longer be considered accessible.
875560a5abSDavid MaloneThe
885560a5abSDavid Malone.Fn hdestroy
895560a5abSDavid Malonefunction calls
905560a5abSDavid Malone.Xr free 3
915560a5abSDavid Malonefor each comparison key in the search table
925560a5abSDavid Malonebut not the data item associated with the key.
93ec7d1254SRuslan Ermilov.Pp
94ec7d1254SRuslan ErmilovThe
95ec7d1254SRuslan Ermilov.Fn hsearch
96ec7d1254SRuslan Ermilovfunction is a hash-table search routine.
97ec7d1254SRuslan ErmilovIt returns a pointer into a hash table
98ec7d1254SRuslan Ermilovindicating the location at which an entry can be found.
99ec7d1254SRuslan ErmilovThe
100ec7d1254SRuslan Ermilov.Fa item
101ec7d1254SRuslan Ermilovargument is a structure of type
102ec7d1254SRuslan Ermilov.Vt ENTRY
103ec7d1254SRuslan Ermilov(defined in the
104fe08efe6SRuslan Ermilov.In search.h
1059823a90cSPedro F. Giffuniheader) that contains two pointers:
106ec7d1254SRuslan Ermilov.Fa item.key
107ec7d1254SRuslan Ermilovpoints to the comparison key (a
108ec7d1254SRuslan Ermilov.Vt "char *" ) ,
109ec7d1254SRuslan Ermilovand
110ec7d1254SRuslan Ermilov.Fa item.data
111ec7d1254SRuslan Ermilov(a
112ec7d1254SRuslan Ermilov.Vt "void *" )
113ec7d1254SRuslan Ermilovpoints to any other data to be associated with
114ec7d1254SRuslan Ermilovthat key.
115ec7d1254SRuslan ErmilovThe comparison function used by
116ec7d1254SRuslan Ermilov.Fn hsearch
117ec7d1254SRuslan Ermilovis
118ec7d1254SRuslan Ermilov.Xr strcmp 3 .
119ec7d1254SRuslan ErmilovThe
120ec7d1254SRuslan Ermilov.Fa action
121ec7d1254SRuslan Ermilovargument is a
122ec7d1254SRuslan Ermilovmember of an enumeration type
123ec7d1254SRuslan Ermilov.Vt ACTION
124ec7d1254SRuslan Ermilovindicating the disposition of the entry if it cannot be
125ec7d1254SRuslan Ermilovfound in the table.
126ec7d1254SRuslan Ermilov.Dv ENTER
127ec7d1254SRuslan Ermilovindicates that the
128ec7d1254SRuslan Ermilov.Fa item
129ec7d1254SRuslan Ermilovshould be inserted in the table at an
130ec7d1254SRuslan Ermilovappropriate point.
131ec7d1254SRuslan Ermilov.Dv FIND
132ec7d1254SRuslan Ermilovindicates that no entry should be made.
133ec7d1254SRuslan ErmilovUnsuccessful resolution is
134ec7d1254SRuslan Ermilovindicated by the return of a
135ec7d1254SRuslan Ermilov.Dv NULL
136ec7d1254SRuslan Ermilovpointer.
1375560a5abSDavid Malone.Pp
1385560a5abSDavid MaloneThe comparison key (passed to
1395560a5abSDavid Malone.Fn hsearch
1405560a5abSDavid Maloneas
1415560a5abSDavid Malone.Fa item.key )
1425560a5abSDavid Malonemust be allocated using
1435560a5abSDavid Malone.Xr malloc 3
1445560a5abSDavid Maloneif
1455560a5abSDavid Malone.Fa action
1465560a5abSDavid Maloneis
1475560a5abSDavid Malone.Dv ENTER
1485560a5abSDavid Maloneand
1495560a5abSDavid Malone.Fn hdestroy
1505560a5abSDavid Maloneis called.
1519823a90cSPedro F. Giffuni.Pp
1529823a90cSPedro F. GiffuniThe
1539823a90cSPedro F. Giffuni.Fn hcreate_r ,
1549823a90cSPedro F. Giffuni.Fn hdestroy_r ,
1559823a90cSPedro F. Giffuniand
1569823a90cSPedro F. Giffuni.Fn hsearch_r
1579823a90cSPedro F. Giffunifunctions are re-entrant versions of the above functions that can
1589823a90cSPedro F. Giffunioperate on a table supplied by the user.
1599823a90cSPedro F. GiffuniThe
1609823a90cSPedro F. Giffuni.Fn hsearch_r
1619823a90cSPedro F. Giffunifunction returns
1629823a90cSPedro F. Giffuni.Dv 0
1639823a90cSPedro F. Giffuniif the action is
1649823a90cSPedro F. Giffuni.Dv ENTER
1659823a90cSPedro F. Giffuniand the element cannot be created,
1669823a90cSPedro F. Giffuni.Dv 1
1679823a90cSPedro F. Giffuniotherwise.
1689823a90cSPedro F. GiffuniIf the element exists or can be created, it will be placed in
1699823a90cSPedro F. Giffuni.Fa itemp ,
1709823a90cSPedro F. Giffuniotherwise
1719823a90cSPedro F. Giffuni.Fa itemp
1729823a90cSPedro F. Giffuniwill be set to
1739823a90cSPedro F. Giffuni.Dv NULL .
174ec7d1254SRuslan Ermilov.Sh RETURN VALUES
175ec7d1254SRuslan ErmilovThe
176ec7d1254SRuslan Ermilov.Fn hcreate
1779823a90cSPedro F. Giffuniand
1789823a90cSPedro F. Giffuni.Fn hcreate_r
1799823a90cSPedro F. Giffunifunctions return 0 if the table creation failed and the global variable
1806d05da1dSDaniel Gerzo.Va errno
1816d05da1dSDaniel Gerzois set to indicate the error;
1826d05da1dSDaniel Gerzootherwise, a non-zero value is returned.
183ec7d1254SRuslan Ermilov.Pp
184ec7d1254SRuslan ErmilovThe
185ec7d1254SRuslan Ermilov.Fn hdestroy
1869823a90cSPedro F. Giffuniand
1879823a90cSPedro F. Giffuni.Fn hdestroy_r
1889823a90cSPedro F. Giffunifunctions return no value.
189ec7d1254SRuslan Ermilov.Pp
190ec7d1254SRuslan ErmilovThe
191ec7d1254SRuslan Ermilov.Fn hsearch
1929823a90cSPedro F. Giffuniand
1939823a90cSPedro F. Giffuni.Fn hsearch_r
1949823a90cSPedro F. Giffunifunctions return a
195ec7d1254SRuslan Ermilov.Dv NULL
196ec7d1254SRuslan Ermilovpointer if either the
197ec7d1254SRuslan Ermilov.Fa action
198ec7d1254SRuslan Ermilovis
199ec7d1254SRuslan Ermilov.Dv FIND
200ec7d1254SRuslan Ermilovand the
201ec7d1254SRuslan Ermilov.Fa item
202ec7d1254SRuslan Ermilovcould not be found or the
203ec7d1254SRuslan Ermilov.Fa action
204ec7d1254SRuslan Ermilovis
205ec7d1254SRuslan Ermilov.Dv ENTER
206ec7d1254SRuslan Ermilovand the table is full.
207ec7d1254SRuslan Ermilov.Sh EXAMPLES
208ec7d1254SRuslan ErmilovThe following example reads in strings followed by two numbers
209ec7d1254SRuslan Ermilovand stores them in a hash table, discarding duplicates.
210ec7d1254SRuslan ErmilovIt then reads in strings and finds the matching entry in the hash
211ec7d1254SRuslan Ermilovtable and prints it out.
212ec7d1254SRuslan Ermilov.Bd -literal
213ec7d1254SRuslan Ermilov#include <stdio.h>
214ec7d1254SRuslan Ermilov#include <search.h>
215ec7d1254SRuslan Ermilov#include <string.h>
2165560a5abSDavid Malone#include <stdlib.h>
217ec7d1254SRuslan Ermilov
218ec7d1254SRuslan Ermilovstruct info {			/* This is the info stored in the table */
219ec7d1254SRuslan Ermilov	int age, room;		/* other than the key. */
220ec7d1254SRuslan Ermilov};
221ec7d1254SRuslan Ermilov
222ec7d1254SRuslan Ermilov#define NUM_EMPL	5000	/* # of elements in search table. */
223ec7d1254SRuslan Ermilov
224ec7d1254SRuslan Ermilovint
225ec7d1254SRuslan Ermilovmain(void)
226ec7d1254SRuslan Ermilov{
2275560a5abSDavid Malone	char str[BUFSIZ]; /* Space to read string */
228ec7d1254SRuslan Ermilov	struct info info_space[NUM_EMPL]; /* Space to store employee info. */
229ec7d1254SRuslan Ermilov	struct info *info_ptr = info_space; /* Next space in info_space. */
230ec7d1254SRuslan Ermilov	ENTRY item;
231ec7d1254SRuslan Ermilov	ENTRY *found_item; /* Name to look for in table. */
232ec7d1254SRuslan Ermilov	char name_to_find[30];
233ec7d1254SRuslan Ermilov	int i = 0;
234ec7d1254SRuslan Ermilov
235ec7d1254SRuslan Ermilov	/* Create table; no error checking is performed. */
236ec7d1254SRuslan Ermilov	(void) hcreate(NUM_EMPL);
237ec7d1254SRuslan Ermilov
2385560a5abSDavid Malone	while (scanf("%s%d%d", str, &info_ptr->age,
239ec7d1254SRuslan Ermilov	    &info_ptr->room) != EOF && i++ < NUM_EMPL) {
240ec7d1254SRuslan Ermilov		/* Put information in structure, and structure in item. */
2415560a5abSDavid Malone		item.key = strdup(str);
242ec7d1254SRuslan Ermilov		item.data = info_ptr;
243ec7d1254SRuslan Ermilov		info_ptr++;
244ec7d1254SRuslan Ermilov		/* Put item into table. */
245ec7d1254SRuslan Ermilov		(void) hsearch(item, ENTER);
246ec7d1254SRuslan Ermilov	}
247ec7d1254SRuslan Ermilov
248ec7d1254SRuslan Ermilov	/* Access table. */
249ec7d1254SRuslan Ermilov	item.key = name_to_find;
250ec7d1254SRuslan Ermilov	while (scanf("%s", item.key) != EOF) {
251ec7d1254SRuslan Ermilov		if ((found_item = hsearch(item, FIND)) != NULL) {
252ec7d1254SRuslan Ermilov			/* If item is in the table. */
253e25e8ab4SRuslan Ermilov			(void)printf("found %s, age = %d, room = %d\en",
254ec7d1254SRuslan Ermilov			    found_item->key,
255ec7d1254SRuslan Ermilov			    ((struct info *)found_item->data)->age,
256ec7d1254SRuslan Ermilov			    ((struct info *)found_item->data)->room);
257ec7d1254SRuslan Ermilov		} else
258e25e8ab4SRuslan Ermilov			(void)printf("no such employee %s\en", name_to_find);
259ec7d1254SRuslan Ermilov	}
2605560a5abSDavid Malone	hdestroy();
261ec7d1254SRuslan Ermilov	return 0;
262ec7d1254SRuslan Ermilov}
263ec7d1254SRuslan Ermilov.Ed
26424a0682cSRuslan Ermilov.Sh ERRORS
26524a0682cSRuslan ErmilovThe
266*d5f154d3SEnji Cooper.Fn hcreate ,
2679823a90cSPedro F. Giffuni.Fn hcreate_r ,
268*d5f154d3SEnji Cooper.Fn hsearch ,
2699823a90cSPedro F. Giffuniand
2709823a90cSPedro F. Giffuni.Fn hsearch_r
2719823a90cSPedro F. Giffunifunctions will fail if:
27224a0682cSRuslan Ermilov.Bl -tag -width Er
27324a0682cSRuslan Ermilov.It Bq Er ENOMEM
2749823a90cSPedro F. GiffuniInsufficient memory is available.
27524a0682cSRuslan Ermilov.El
2769823a90cSPedro F. Giffuni.Pp
2779823a90cSPedro F. GiffuniThe
2789823a90cSPedro F. Giffuni.Fn hsearch
2799823a90cSPedro F. Giffuniand
2809823a90cSPedro F. Giffuni.Fn hsearch_r
2819823a90cSPedro F. Giffunifunctions will also fail if the action is
282*d5f154d3SEnji Cooper.Dv FIND
2839823a90cSPedro F. Giffuniand the element is not found:
2849823a90cSPedro F. Giffuni.Bl -tag -width Er
2859823a90cSPedro F. Giffuni.It Bq Er ESRCH
2869823a90cSPedro F. GiffuniThe
2879823a90cSPedro F. Giffuni.Fa item
2889823a90cSPedro F. Giffunigiven is not found.
2899823a90cSPedro F. Giffuni.El
290ec7d1254SRuslan Ermilov.Sh SEE ALSO
291ec7d1254SRuslan Ermilov.Xr bsearch 3 ,
292ec7d1254SRuslan Ermilov.Xr lsearch 3 ,
293ec7d1254SRuslan Ermilov.Xr malloc 3 ,
294ec7d1254SRuslan Ermilov.Xr strcmp 3 ,
295ec7d1254SRuslan Ermilov.Xr tsearch 3
296ec7d1254SRuslan Ermilov.Sh STANDARDS
297ec7d1254SRuslan ErmilovThe
298ec7d1254SRuslan Ermilov.Fn hcreate ,
299ec7d1254SRuslan Ermilov.Fn hdestroy ,
300ec7d1254SRuslan Ermilovand
301ec7d1254SRuslan Ermilov.Fn hsearch
302ec7d1254SRuslan Ermilovfunctions conform to
303ec7d1254SRuslan Ermilov.St -xpg4.2 .
304ec7d1254SRuslan Ermilov.Sh HISTORY
305ec7d1254SRuslan ErmilovThe
306ec7d1254SRuslan Ermilov.Fn hcreate ,
307ec7d1254SRuslan Ermilov.Fn hdestroy ,
308ec7d1254SRuslan Ermilovand
309ec7d1254SRuslan Ermilov.Fn hsearch
310ec7d1254SRuslan Ermilovfunctions first appeared in
311ec7d1254SRuslan Ermilov.At V .
3129823a90cSPedro F. GiffuniThe
3139823a90cSPedro F. Giffuni.Fn hcreate_r ,
3149823a90cSPedro F. Giffuni.Fn hdestroy_r
3159823a90cSPedro F. Giffuniand
3169823a90cSPedro F. Giffuni.Fn hsearch_r
3179823a90cSPedro F. Giffunifunctions are
3189823a90cSPedro F. Giffuni.Tn GNU
3199823a90cSPedro F. Giffuniextensions.
320ec7d1254SRuslan Ermilov.Sh BUGS
3219823a90cSPedro F. GiffuniThe original,
3229823a90cSPedro F. Giffuni.Pf non- Tn GNU
3239823a90cSPedro F. Giffuniinterface permits the use of only one hash table at a time.
324