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