146175Sbostic /*-
2*61217Sbostic  * Copyright (c) 1991, 1993
3*61217Sbostic  *	The Regents of the University of California.  All rights reserved.
446175Sbostic  *
546175Sbostic  * This code is derived from software contributed to Berkeley by
646175Sbostic  * Margo Seltzer.
746175Sbostic  *
850986Sbostic  * %sccs.include.redist.c%
946175Sbostic  */
1046175Sbostic 
1146175Sbostic #ifndef lint
12*61217Sbostic static char copyright[] =
13*61217Sbostic "@(#) Copyright (c) 1991, 1993\n\
14*61217Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1546175Sbostic #endif /* not lint */
1646175Sbostic 
1746175Sbostic #ifndef lint
18*61217Sbostic static char sccsid[] = "@(#)thash4.c	8.1 (Berkeley) 06/04/93";
1946175Sbostic #endif /* not lint */
2046175Sbostic 
2150986Sbostic #include <sys/types.h>
2246175Sbostic #include <sys/file.h>
2346175Sbostic #include <sys/timeb.h>
2450986Sbostic #include <stdio.h>
2546175Sbostic #include <errno.h>
2646175Sbostic #include <db.h>
2746175Sbostic 
2846175Sbostic #define INITIAL	25000
2946175Sbostic #define MAXWORDS    25000	       /* # of elements in search table */
3046175Sbostic 
3146175Sbostic /* Usage: thash pagesize fillfactor file */
3250985Sbostic char	wp1[8192];
3346175Sbostic char	wp2[8192];
main(argc,argv)3446175Sbostic main(argc, argv)
3546175Sbostic char **argv;
3646175Sbostic {
3746175Sbostic 	DBT item, key, res;
3846175Sbostic 	DB *dbp;
3946175Sbostic 	HASHINFO ctl;
4046175Sbostic 	FILE *fp;
4146175Sbostic 	int	stat;
4246175Sbostic 	time_t	t;
4346175Sbostic 
4446175Sbostic 	int i = 0;
4546175Sbostic 
4646175Sbostic 	argv++;
4746175Sbostic 	ctl.hash = NULL;
4846175Sbostic 	ctl.bsize = atoi(*argv++);
4946175Sbostic 	ctl.ffactor = atoi(*argv++);
5046175Sbostic 	ctl.nelem = atoi(*argv++);
5147253Sbostic 	ctl.cachesize = atoi(*argv++);
5246175Sbostic 	ctl.lorder = 0;
5351078Sbostic 	if (!(dbp = dbopen( NULL, O_CREAT|O_RDWR, 0400, DB_HASH, &ctl))) {
5446175Sbostic 		/* create table */
5546175Sbostic 		fprintf(stderr, "cannot create: hash table size %d)\n",
5646175Sbostic 			INITIAL);
5746175Sbostic 		fprintf(stderr, "\terrno: %d\n", errno);
5846175Sbostic 		exit(1);
5946175Sbostic 	}
6046175Sbostic 
6146175Sbostic 	key.data = wp1;
6246175Sbostic 	item.data = wp2;
6350985Sbostic 	while ( fgets(wp1, 8192, stdin) &&
6446175Sbostic 		fgets(wp2, 8192, stdin) &&
6546175Sbostic 		i++ < MAXWORDS) {
6646175Sbostic /*
6746175Sbostic * put info in structure, and structure in the item
6846175Sbostic */
6946175Sbostic 		key.size = strlen(wp1);
7046175Sbostic 		item.size = strlen(wp2);
7146175Sbostic 
7246175Sbostic /*
7346175Sbostic  * enter key/data pair into the table
7446175Sbostic  */
7546175Sbostic 		if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
7646175Sbostic 			fprintf(stderr, "cannot enter: key %s\n",
7746175Sbostic 				item.data);
7846175Sbostic 			fprintf(stderr, "\terrno: %d\n", errno);
7946175Sbostic 			exit(1);
8046175Sbostic 		}
8146175Sbostic 	}
8246175Sbostic 
8346175Sbostic 	if ( --argc ) {
8446175Sbostic 		fp = fopen ( argv[0], "r");
8546175Sbostic 		i = 0;
8646175Sbostic 		while ( fgets(wp1, 256, fp) &&
8746175Sbostic 			fgets(wp2, 8192, fp) &&
8846175Sbostic 			i++ < MAXWORDS) {
8946175Sbostic 
9046175Sbostic 		    key.size = strlen(wp1);
9150985Sbostic 		    stat = (dbp->get)(dbp, &key, &res, 0);
9246175Sbostic 		    if (stat < 0 ) {
9346175Sbostic 			fprintf ( stderr, "Error retrieving %s\n", key.data );
9446175Sbostic 			fprintf(stderr, "\terrno: %d\n", errno);
9546175Sbostic 			exit(1);
9646175Sbostic 		    } else if ( stat > 0 ) {
9746175Sbostic 			fprintf ( stderr, "%s not found\n", key.data );
9846175Sbostic 			fprintf(stderr, "\terrno: %d\n", errno);
9946175Sbostic 			exit(1);
10046175Sbostic 		    }
10146175Sbostic 		}
10246175Sbostic 		fclose(fp);
10346175Sbostic 	}
10446175Sbostic 	dbp->close(dbp);
10546175Sbostic 	exit(0);
10646175Sbostic }
107