1.\" $NetBSD: hcreate.3,v 1.4 2002/02/07 07:00:29 ross Exp $ 2.\" 3.\" Copyright (c) 1999 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Klaus Klein. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. All advertising materials mentioning features or use of this software 18.\" must display the following acknowledgement: 19.\" This product includes software developed by the NetBSD 20.\" Foundation, Inc. and its contributors. 21.\" 4. Neither the name of The NetBSD Foundation nor the names of its 22.\" contributors may be used to endorse or promote products derived 23.\" from this software without specific prior written permission. 24.\" 25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35.\" POSSIBILITY OF SUCH DAMAGE. 36.\" 37.Dd February 13, 2001 38.Dt HCREATE 3 39.Os 40.Sh NAME 41.Nm hcreate , 42.Nm hdestroy , 43.Nm hsearch 44.Nd manage hash search table 45.Sh LIBRARY 46.Lb libc 47.Sh SYNOPSIS 48.Fd #include \*[Lt]search.h\*[Gt] 49.Ft int 50.Fn hcreate "size_t nel" 51.Ft void 52.Fn hdestroy "void" 53.Ft ENTRY * 54.Fn hsearch "ENTRY item" "ACTION action" 55.Sh DESCRIPTION 56The 57.Fn hcreate , 58.Fn hdestroy 59and 60.Fn hsearch 61functions manage hash search tables. 62.Pp 63The 64.Fn hcreate 65function allocates and initializes the table. 66The 67.Fa nel 68argument specifies an estimate of the maximum number of entries to be held 69by the table. Unless further memory allocation fails, supplying an 70insufficient 71.Fa nel 72value will not result in functional harm, although a performance degradation 73may occur. 74Initialization using the 75.Fn hcreate 76function is mandatory prior to any access operations using 77.Fn hsearch . 78.Pp 79The 80.Fn hdestroy 81function destroys a table previously created using 82.Fn hcreate . 83After a call to 84.Fn hdestroy , 85the data can no longer be accessed. 86.Pp 87The 88.Fn hsearch 89function is used to search to the hash table. It returns a pointer into the 90hash table indicating the address of an item. The 91.Fa item 92argument is of type 93.Dv ENTRY , 94a structural type which contains the following members: 95.Bl -tag -compact -offset indent -width voidX*dataXX 96.It Fa char *key 97comparison key. 98.It Fa void *data 99pointer to data associated with 100.Fa key . 101.El 102.Pp 103The key comparison function used by 104.Fn hsearch 105is 106.Xr strcmp 3 . 107.Pp 108The 109.Fa action 110argument is of type 111.Dv ACTION , 112an enumeration type which defines the following values: 113.Bl -tag -compact -offset indent -width ENTERXX 114.It Dv ENTER 115Insert 116.Fa item 117into the hash table. 118If an existing item with the same key is found, it is not replaced. 119Note that the 120.Fa key 121and 122.Fa data 123elements of 124.Fa item 125are used directly by the new table entry. The storage for the 126key must not be modified during the lifetime of the hash table. 127.It Dv FIND 128Search the hash table without inserting 129.Fa item . 130.El 131.Sh RETURN VALUES 132If successful, the 133.Fn hcreate 134function returns a non-zero value. Otherwise, a value of 0 is returned and 135.Va errno 136is set to indicate the error. 137.Pp 138The 139.Fn hdestroy 140functions 141returns no value. 142.Pp 143If successful, the 144.Fn hsearch 145function returns a pointer to hash table entry matching 146the provided key. 147If the action is 148.Dv FIND 149and the item was not found, or if the action is 150.Dv ENTER 151and the insertion failed, 152.Dv NULL 153is returned and 154.Va errno 155is set to indicate the error. 156If the action is 157.Dv ENTER 158and an entry already existed in the table matching the given 159key, the existing entry is returned and is not replaced. 160.Sh ERRORS 161The 162.Fn hcreate 163and 164.Fn hsearch 165functions will fail if: 166.Bl -tag -width Er 167.It Bq Er ENOMEM 168Insufficient memory is available. 169.El 170.Sh SEE ALSO 171.Xr bsearch 3 , 172.Xr lsearch 3 , 173.Xr malloc 3 , 174.Xr strcmp 3 175.Sh STANDARDS 176The 177.Fn hcreate , 178.Fn hdestroy 179and 180.Fn hsearch 181functions conform to 182.St -xpg4.2 . 183.Sh HISTORY 184The 185.Fn hcreate , 186.Fn hdestroy 187and 188.Fn hsearch 189functions first appeared in 190.At V . 191.Sh BUGS 192The interface permits the use of only one hash table at a time. 193