146175Sbostic /*-
246175Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346175Sbostic  * All rights reserved.
446175Sbostic  *
546175Sbostic  * This code is derived from software contributed to Berkeley by
646175Sbostic  * Margo Seltzer.
746175Sbostic  *
8*50985Sbostic  * Redistribution and use in source and binary forms, with or without
9*50985Sbostic  * modification, are permitted provided that the following conditions
10*50985Sbostic  * are met:
11*50985Sbostic  * 1. Redistributions of source code must retain the above copyright
12*50985Sbostic  *    notice, this list of conditions and the following disclaimer.
13*50985Sbostic  * 2. Redistributions in binary form must reproduce the above copyright
14*50985Sbostic  *    notice, this list of conditions and the following disclaimer in the
15*50985Sbostic  *    documentation and/or other materials provided with the distribution.
16*50985Sbostic  * 3. All advertising materials mentioning features or use of this software
17*50985Sbostic  *    must display the following acknowledgement:
18*50985Sbostic  *	This product includes software developed by the University of
19*50985Sbostic  *	California, Berkeley and its contributors.
20*50985Sbostic  * 4. Neither the name of the University nor the names of its contributors
21*50985Sbostic  *    may be used to endorse or promote products derived from this software
22*50985Sbostic  *    without specific prior written permission.
23*50985Sbostic  *
24*50985Sbostic  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*50985Sbostic  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*50985Sbostic  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*50985Sbostic  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*50985Sbostic  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*50985Sbostic  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*50985Sbostic  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*50985Sbostic  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*50985Sbostic  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*50985Sbostic  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*50985Sbostic  * SUCH DAMAGE.
3546175Sbostic  */
3646175Sbostic 
3746175Sbostic #ifndef lint
3846175Sbostic char copyright[] =
3946175Sbostic "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
4046175Sbostic  All rights reserved.\n";
4146175Sbostic #endif /* not lint */
4246175Sbostic 
4346175Sbostic #ifndef lint
44*50985Sbostic static char sccsid[] = "@(#)thash4.c	5.2 (Berkeley) 3/12/91";
4546175Sbostic #endif /* not lint */
4646175Sbostic 
4746175Sbostic #include <stdio.h>
4846175Sbostic #include <sys/file.h>
4946175Sbostic #include <sys/types.h>
5046175Sbostic #include <sys/timeb.h>
5146175Sbostic #include <errno.h>
5246175Sbostic #include <db.h>
5346175Sbostic 
5446175Sbostic #define INITIAL	25000
5546175Sbostic #define MAXWORDS    25000	       /* # of elements in search table */
5646175Sbostic 
5746175Sbostic /* Usage: thash pagesize fillfactor file */
58*50985Sbostic char	wp1[8192];
5946175Sbostic char	wp2[8192];
6046175Sbostic main(argc, argv)
6146175Sbostic char **argv;
6246175Sbostic {
6346175Sbostic 	DBT item, key, res;
6446175Sbostic 	DB *dbp;
6546175Sbostic 	HASHINFO ctl;
6646175Sbostic 	FILE *fp;
6746175Sbostic 	int	stat;
6846175Sbostic 	time_t	t;
6946175Sbostic 
7046175Sbostic 	int i = 0;
7146175Sbostic 
7246175Sbostic 	argv++;
7346175Sbostic 	ctl.hash = NULL;
7446175Sbostic 	ctl.bsize = atoi(*argv++);
7546175Sbostic 	ctl.ffactor = atoi(*argv++);
7646175Sbostic 	ctl.nelem = atoi(*argv++);
7747253Sbostic 	ctl.cachesize = atoi(*argv++);
7846175Sbostic 	ctl.lorder = 0;
7946175Sbostic 	if (!(dbp = hash_open( NULL, O_CREAT|O_RDWR, 0400, &ctl))) {
8046175Sbostic 		/* create table */
8146175Sbostic 		fprintf(stderr, "cannot create: hash table size %d)\n",
8246175Sbostic 			INITIAL);
8346175Sbostic 		fprintf(stderr, "\terrno: %d\n", errno);
8446175Sbostic 		exit(1);
8546175Sbostic 	}
8646175Sbostic 
8746175Sbostic 	key.data = wp1;
8846175Sbostic 	item.data = wp2;
89*50985Sbostic 	while ( fgets(wp1, 8192, stdin) &&
9046175Sbostic 		fgets(wp2, 8192, stdin) &&
9146175Sbostic 		i++ < MAXWORDS) {
9246175Sbostic /*
9346175Sbostic * put info in structure, and structure in the item
9446175Sbostic */
9546175Sbostic 		key.size = strlen(wp1);
9646175Sbostic 		item.size = strlen(wp2);
9746175Sbostic 
9846175Sbostic /*
9946175Sbostic  * enter key/data pair into the table
10046175Sbostic  */
10146175Sbostic 		if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
10246175Sbostic 			fprintf(stderr, "cannot enter: key %s\n",
10346175Sbostic 				item.data);
10446175Sbostic 			fprintf(stderr, "\terrno: %d\n", errno);
10546175Sbostic 			exit(1);
10646175Sbostic 		}
10746175Sbostic 	}
10846175Sbostic 
10946175Sbostic 	if ( --argc ) {
11046175Sbostic 		fp = fopen ( argv[0], "r");
11146175Sbostic 		i = 0;
11246175Sbostic 		while ( fgets(wp1, 256, fp) &&
11346175Sbostic 			fgets(wp2, 8192, fp) &&
11446175Sbostic 			i++ < MAXWORDS) {
11546175Sbostic 
11646175Sbostic 		    key.size = strlen(wp1);
117*50985Sbostic 		    stat = (dbp->get)(dbp, &key, &res, 0);
11846175Sbostic 		    if (stat < 0 ) {
11946175Sbostic 			fprintf ( stderr, "Error retrieving %s\n", key.data );
12046175Sbostic 			fprintf(stderr, "\terrno: %d\n", errno);
12146175Sbostic 			exit(1);
12246175Sbostic 		    } else if ( stat > 0 ) {
12346175Sbostic 			fprintf ( stderr, "%s not found\n", key.data );
12446175Sbostic 			fprintf(stderr, "\terrno: %d\n", errno);
12546175Sbostic 			exit(1);
12646175Sbostic 		    }
12746175Sbostic 		}
12846175Sbostic 		fclose(fp);
12946175Sbostic 	}
13046175Sbostic 	dbp->close(dbp);
13146175Sbostic 	exit(0);
13246175Sbostic }
133