tdel.c revision 1573
129415Sjmg/*-
229415Sjmg * Copyright (c) 1991, 1993
329415Sjmg *	The Regents of the University of California.  All rights reserved.
429415Sjmg *
529415Sjmg * This code is derived from software contributed to Berkeley by
629415Sjmg * Margo Seltzer.
729415Sjmg *
829415Sjmg * Redistribution and use in source and binary forms, with or without
929415Sjmg * modification, are permitted provided that the following conditions
1029415Sjmg * are met:
1129415Sjmg * 1. Redistributions of source code must retain the above copyright
1229415Sjmg *    notice, this list of conditions and the following disclaimer.
1329415Sjmg * 2. Redistributions in binary form must reproduce the above copyright
1430869Sjmg *    notice, this list of conditions and the following disclaimer in the
1530869Sjmg *    documentation and/or other materials provided with the distribution.
1630869Sjmg * 3. All advertising materials mentioning features or use of this software
1730869Sjmg *    must display the following acknowledgement:
1830869Sjmg *	This product includes software developed by the University of
1930869Sjmg *	California, Berkeley and its contributors.
2030869Sjmg * 4. Neither the name of the University nor the names of its contributors
2130869Sjmg *    may be used to endorse or promote products derived from this software
2229415Sjmg *    without specific prior written permission.
2329415Sjmg *
2429415Sjmg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2529415Sjmg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2629415Sjmg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2729415Sjmg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2829415Sjmg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2929415Sjmg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3029415Sjmg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3129415Sjmg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3229415Sjmg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3329415Sjmg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3429415Sjmg * SUCH DAMAGE.
3529415Sjmg */
3629415Sjmg
3729415Sjmg#ifndef lint
3829415Sjmgstatic char copyright[] =
3929415Sjmg"@(#) Copyright (c) 1991, 1993\n\
4029415Sjmg	The Regents of the University of California.  All rights reserved.\n";
4129415Sjmg#endif /* not lint */
4229415Sjmg
4329415Sjmg#ifndef lint
4429415Sjmgstatic char sccsid[] = "@(#)tdel.c	8.1 (Berkeley) 6/4/93";
4529415Sjmg#endif /* not lint */
4629415Sjmg
4729415Sjmg#include <sys/types.h>
4829415Sjmg#include <sys/file.h>
4929415Sjmg#include <db.h>
5029415Sjmg#include <stdio.h>
5129415Sjmg
5229415Sjmg#define INITIAL	25000
5329415Sjmg#define MAXWORDS    25000	       /* # of elements in search table */
5429415Sjmg
5529415Sjmg/* Usage: thash pagesize fillfactor file */
5629415Sjmgchar	wp1[8192];
5729415Sjmgchar	wp2[8192];
5829415Sjmgmain(argc, argv)
5929415Sjmgchar **argv;
6029415Sjmg{
6129415Sjmg	DBT item, key;
6229415Sjmg	DB	*dbp;
6329415Sjmg	HASHINFO ctl;
6429415Sjmg	FILE *fp;
6529415Sjmg	int	stat;
6629415Sjmg
6729415Sjmg	int i = 0;
6829415Sjmg
6929415Sjmg	argv++;
7029415Sjmg	ctl.nelem = INITIAL;
7129415Sjmg	ctl.hash = NULL;
7229415Sjmg	ctl.bsize = atoi(*argv++);
7329415Sjmg	ctl.ffactor = atoi(*argv++);
7429415Sjmg	ctl.cachesize = 1024 * 1024;	/* 1 MEG */
7529415Sjmg	ctl.lorder = 0;
7629415Sjmg	argc -= 2;
7729415Sjmg	if (!(dbp = dbopen( NULL, O_CREAT|O_RDWR, 0400, DB_HASH, &ctl))) {
7829415Sjmg		/* create table */
7929415Sjmg		fprintf(stderr, "cannot create: hash table size %d)\n",
8029415Sjmg			INITIAL);
8129415Sjmg		exit(1);
8229415Sjmg	}
8329415Sjmg
8429415Sjmg	key.data = wp1;
8529415Sjmg	item.data = wp2;
8629415Sjmg	while ( fgets(wp1, 8192, stdin) &&
8729415Sjmg		fgets(wp2, 8192, stdin) &&
8829415Sjmg		i++ < MAXWORDS) {
8929415Sjmg/*
9029415Sjmg* put info in structure, and structure in the item
9129415Sjmg*/
9229415Sjmg		key.size = strlen(wp1);
9329415Sjmg		item.size = strlen(wp2);
9429415Sjmg
9529565Sjmg/*
9629415Sjmg * enter key/data pair into the table
9729415Sjmg */
9829415Sjmg		if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
9929415Sjmg			fprintf(stderr, "cannot enter: key %s\n",
10029415Sjmg				item.data);
10129415Sjmg			exit(1);
10229415Sjmg		}
10329415Sjmg	}
10429415Sjmg
10529415Sjmg	if ( --argc ) {
10629415Sjmg		fp = fopen ( argv[0], "r");
10729415Sjmg		i = 0;
10829415Sjmg		while ( fgets(wp1, 8192, fp) &&
10929415Sjmg			fgets(wp2, 8192, fp) &&
11029415Sjmg			i++ < MAXWORDS) {
11129415Sjmg		    key.size = strlen(wp1);
11229415Sjmg		    stat = (dbp->del)(dbp, &key, 0);
11329415Sjmg		    if (stat) {
11429415Sjmg			fprintf ( stderr, "Error retrieving %s\n", key.data );
11529415Sjmg			exit(1);
11630869Sjmg		    }
11729415Sjmg		}
11829415Sjmg		fclose(fp);
11929415Sjmg	}
12029415Sjmg	(dbp->close)(dbp);
12129415Sjmg	exit(0);
12229415Sjmg}
12329565Sjmg