xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 66211)
156039Sbostic /*-
261220Sbostic  * Copyright (c) 1992, 1993
361220Sbostic  *	The Regents of the University of California.  All rights reserved.
456039Sbostic  *
556039Sbostic  * %sccs.include.redist.c%
656039Sbostic  */
756039Sbostic 
856039Sbostic #ifndef lint
961220Sbostic static char copyright[] =
1061220Sbostic "@(#) Copyright (c) 1992, 1993\n\
1161220Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1256039Sbostic #endif /* not lint */
1356039Sbostic 
1456039Sbostic #ifndef lint
15*66211Sbostic static char sccsid[] = "@(#)dbtest.c	8.8 (Berkeley) 02/21/94";
1656039Sbostic #endif /* not lint */
1756039Sbostic 
1856039Sbostic #include <sys/param.h>
1956039Sbostic #include <sys/stat.h>
2056039Sbostic 
2156039Sbostic #include <ctype.h>
2256039Sbostic #include <errno.h>
2356039Sbostic #include <fcntl.h>
2456039Sbostic #include <limits.h>
2556039Sbostic #include <stdio.h>
2656039Sbostic #include <stdlib.h>
2756039Sbostic #include <string.h>
2856039Sbostic #include <unistd.h>
2956039Sbostic 
3059492Sbostic #include <db.h>
3159492Sbostic 
3256557Sbostic enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
3356039Sbostic 
3456557Sbostic void	 compare __P((DBT *, DBT *));
3556039Sbostic DBTYPE	 dbtype __P((char *));
3656992Sbostic void	 dump __P((DB *, int));
3756039Sbostic void	 err __P((const char *, ...));
3856039Sbostic void	 get __P((DB *, DBT *));
3956557Sbostic void	 getdata __P((DB *, DBT *, DBT *));
4056039Sbostic void	 put __P((DB *, DBT *, DBT *));
4156039Sbostic void	 rem __P((DB *, DBT *));
4256039Sbostic void	*rfile __P((char *, size_t *));
4356059Sbostic void	 seq __P((DB *, DBT *));
4456039Sbostic u_int	 setflags __P((char *));
4556039Sbostic void	*setinfo __P((DBTYPE, char *));
4656039Sbostic void	 usage __P((void));
4756039Sbostic void	*xmalloc __P((char *, size_t));
4856039Sbostic 
4956039Sbostic DBTYPE type;
5056039Sbostic void *infop;
5156039Sbostic u_long lineno;
5256039Sbostic u_int flags;
5356039Sbostic int ofd = STDOUT_FILENO;
5456039Sbostic 
5557456Sbostic DB *XXdbp;				/* Global for gdb. */
5657456Sbostic 
5756039Sbostic int
main(argc,argv)5856039Sbostic main(argc, argv)
5956039Sbostic 	int argc;
6056039Sbostic 	char *argv[];
6156039Sbostic {
6257461Sbostic 	extern int optind;
6357461Sbostic 	extern char *optarg;
6456039Sbostic 	enum S command, state;
6556039Sbostic 	DB *dbp;
6656557Sbostic 	DBT data, key, keydata;
6756039Sbostic 	size_t len;
6864453Sbostic 	int ch, oflags;
6959626Sbostic 	char *fname, *infoarg, *p, buf[8 * 1024];
7056039Sbostic 
7156039Sbostic 	infoarg = NULL;
7259626Sbostic 	fname = NULL;
7364453Sbostic 	oflags = O_CREAT | O_RDWR;
7464453Sbostic 	while ((ch = getopt(argc, argv, "f:i:lo:")) != EOF)
7556039Sbostic 		switch(ch) {
7659626Sbostic 		case 'f':
7759626Sbostic 			fname = optarg;
7859626Sbostic 			break;
7956039Sbostic 		case 'i':
8056039Sbostic 			infoarg = optarg;
8156039Sbostic 			break;
8264453Sbostic 		case 'l':
8364453Sbostic 			oflags |= DB_LOCK;
8464453Sbostic 			break;
8556039Sbostic 		case 'o':
8656039Sbostic 			if ((ofd = open(optarg,
8756039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
8856039Sbostic 				err("%s: %s", optarg, strerror(errno));
8956039Sbostic 			break;
9056039Sbostic 		case '?':
9156039Sbostic 		default:
9256039Sbostic 			usage();
9356039Sbostic 		}
9456039Sbostic 	argc -= optind;
9556039Sbostic 	argv += optind;
9656039Sbostic 
9756039Sbostic 	if (argc != 2)
9856039Sbostic 		usage();
9956039Sbostic 
10056039Sbostic 	/* Set the type. */
10156039Sbostic 	type = dbtype(*argv++);
10256039Sbostic 
10356039Sbostic 	/* Open the descriptor file. */
10456039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
10556039Sbostic 		err("%s: %s", *argv, strerror(errno));
10656039Sbostic 
10756039Sbostic 	/* Set up the db structure as necessary. */
10856039Sbostic 	if (infoarg == NULL)
10956039Sbostic 		infop = NULL;
11056039Sbostic 	else
11157463Sbostic 		for (p = strtok(infoarg, ",\t "); p != NULL;
11257463Sbostic 		    p = strtok(0, ",\t "))
11356039Sbostic 			if (*p != '\0')
11456039Sbostic 				infop = setinfo(type, p);
11556039Sbostic 
11659626Sbostic 	/* Open the DB. */
11759626Sbostic 	if (fname == NULL) {
11860129Smargo 		p = getenv("TMPDIR");
11960129Smargo 		if (p == NULL)
12060129Smargo 			p = "/var/tmp";
12160129Smargo 		(void)sprintf(buf, "%s/__dbtest", p);
12260129Smargo 		fname = buf;
12360129Smargo 		(void)unlink(buf);
12459626Sbostic 	}
12559626Sbostic 	if ((dbp = dbopen(fname,
12664453Sbostic 	    oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
12756039Sbostic 		err("dbopen: %s", strerror(errno));
12857456Sbostic 	XXdbp = dbp;
12956039Sbostic 
13056039Sbostic 	state = COMMAND;
13156039Sbostic 	for (lineno = 1;
13256039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
13356039Sbostic 		len = strlen(buf);
13456039Sbostic 		switch(*p) {
13556557Sbostic 		case 'c':			/* compare */
13656557Sbostic 			if (state != COMMAND)
13756557Sbostic 				err("line %lu: not expecting command", lineno);
13856557Sbostic 			state = KEY;
13956557Sbostic 			command = COMPARE;
14056557Sbostic 			break;
14156059Sbostic 		case 'e':			/* echo */
14256059Sbostic 			if (state != COMMAND)
14356059Sbostic 				err("line %lu: not expecting command", lineno);
14456557Sbostic 			/* Don't display the newline, if CR at EOL. */
14556557Sbostic 			if (p[len - 2] == '\r')
14656557Sbostic 				--len;
14756557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
14856557Sbostic 				err("write: %s", strerror(errno));
14956059Sbostic 			break;
15056039Sbostic 		case 'g':			/* get */
15156039Sbostic 			if (state != COMMAND)
15256039Sbostic 				err("line %lu: not expecting command", lineno);
15356039Sbostic 			state = KEY;
15456039Sbostic 			command = GET;
15556039Sbostic 			break;
15656039Sbostic 		case 'p':			/* put */
15756039Sbostic 			if (state != COMMAND)
15856039Sbostic 				err("line %lu: not expecting command", lineno);
15956039Sbostic 			state = KEY;
16056039Sbostic 			command = PUT;
16156039Sbostic 			break;
16256039Sbostic 		case 'r':			/* remove */
16356039Sbostic 			if (state != COMMAND)
16456039Sbostic 				err("line %lu: not expecting command", lineno);
16556039Sbostic 			state = KEY;
16656039Sbostic 			command = REMOVE;
16756039Sbostic 			break;
16856039Sbostic 		case 's':			/* seq */
16956039Sbostic 			if (state != COMMAND)
17056039Sbostic 				err("line %lu: not expecting command", lineno);
17156059Sbostic 			if (flags == R_CURSOR) {
17256059Sbostic 				state = KEY;
17356059Sbostic 				command = SEQ;
17456059Sbostic 			} else
17556059Sbostic 				seq(dbp, &key);
17656039Sbostic 			break;
17756039Sbostic 		case 'f':
17856059Sbostic 			flags = setflags(p + 1);
17956039Sbostic 			break;
18056039Sbostic 		case 'D':			/* data file */
18156039Sbostic 			if (state != DATA)
18256039Sbostic 				err("line %lu: not expecting data", lineno);
18356039Sbostic 			data.data = rfile(p + 1, &data.size);
18456992Sbostic 			goto ldata;
18556039Sbostic 		case 'd':			/* data */
18656039Sbostic 			if (state != DATA)
18756039Sbostic 				err("line %lu: not expecting data", lineno);
18856557Sbostic 			data.data = xmalloc(p + 1, len - 1);
18956557Sbostic 			data.size = len - 1;
19056992Sbostic ldata:			switch(command) {
19156557Sbostic 			case COMPARE:
19256557Sbostic 				compare(&keydata, &data);
19356557Sbostic 				break;
19456557Sbostic 			case PUT:
19556557Sbostic 				put(dbp, &key, &data);
19656557Sbostic 				break;
19756557Sbostic 			default:
19856039Sbostic 				err("line %lu: command doesn't take data",
19956039Sbostic 				    lineno);
20056557Sbostic 			}
20159493Sbostic 			if (type != DB_RECNO)
20259493Sbostic 				free(key.data);
20356039Sbostic 			free(data.data);
20456039Sbostic 			state = COMMAND;
20556039Sbostic 			break;
20656039Sbostic 		case 'K':			/* key file */
20756039Sbostic 			if (state != KEY)
20856039Sbostic 				err("line %lu: not expecting a key", lineno);
20956039Sbostic 			if (type == DB_RECNO)
21056039Sbostic 				err("line %lu: 'K' not available for recno",
21156039Sbostic 				    lineno);
21256039Sbostic 			key.data = rfile(p + 1, &key.size);
21356992Sbostic 			goto lkey;
21456039Sbostic 		case 'k':			/* key */
21556039Sbostic 			if (state != KEY)
21656039Sbostic 				err("line %lu: not expecting a key", lineno);
21756039Sbostic 			if (type == DB_RECNO) {
21856039Sbostic 				static recno_t recno;
21964443Sbostic 				recno = atoi(p + 1);
22056039Sbostic 				key.data = &recno;
22156039Sbostic 				key.size = sizeof(recno);
22256039Sbostic 			} else {
22356039Sbostic 				key.data = xmalloc(p + 1, len - 1);
22456039Sbostic 				key.size = len - 1;
22556039Sbostic 			}
22656992Sbostic lkey:			switch(command) {
22756557Sbostic 			case COMPARE:
22856557Sbostic 				getdata(dbp, &key, &keydata);
22956557Sbostic 				state = DATA;
23056557Sbostic 				break;
23156039Sbostic 			case GET:
23256039Sbostic 				get(dbp, &key);
23356039Sbostic 				if (type != DB_RECNO)
23456039Sbostic 					free(key.data);
23556039Sbostic 				state = COMMAND;
23656039Sbostic 				break;
23756039Sbostic 			case PUT:
23856039Sbostic 				state = DATA;
23956039Sbostic 				break;
24056039Sbostic 			case REMOVE:
24156039Sbostic 				rem(dbp, &key);
24256039Sbostic 				if (type != DB_RECNO)
24356039Sbostic 					free(key.data);
24456039Sbostic 				state = COMMAND;
24556039Sbostic 				break;
24656059Sbostic 			case SEQ:
24756059Sbostic 				seq(dbp, &key);
24856059Sbostic 				if (type != DB_RECNO)
24956059Sbostic 					free(key.data);
25056059Sbostic 				state = COMMAND;
25156059Sbostic 				break;
25256039Sbostic 			default:
25356039Sbostic 				err("line %lu: command doesn't take a key",
25456039Sbostic 				    lineno);
25556039Sbostic 			}
25656039Sbostic 			break;
25756992Sbostic 		case 'o':
25856992Sbostic 			dump(dbp, p[1] == 'r');
25956992Sbostic 			break;
26056039Sbostic 		default:
26156039Sbostic 			err("line %lu: %s: unknown command character",
26256059Sbostic 			    p, lineno);
26356039Sbostic 		}
26456039Sbostic 	}
26564486Sbostic #ifdef STATISTICS
26664486Sbostic 	if (type == DB_BTREE)
26764486Sbostic 		__bt_stat(dbp);
26864486Sbostic #endif
26959626Sbostic 	if (dbp->close(dbp))
27059626Sbostic 		err("db->close: %s", strerror(errno));
27156039Sbostic 	(void)close(ofd);
27256039Sbostic 	exit(0);
27356039Sbostic }
27456039Sbostic 
27556059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
27656059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
27756059Sbostic 
27856039Sbostic void
compare(db1,db2)27956557Sbostic compare(db1, db2)
28056557Sbostic 	DBT *db1, *db2;
28156557Sbostic {
28256557Sbostic 	register size_t len;
28356557Sbostic 	register u_char *p1, *p2;
28456557Sbostic 
28556557Sbostic 	if (db1->size != db2->size)
28656557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
28756557Sbostic 		    db1->size, db2->size);
28856557Sbostic 
28956557Sbostic 	len = MIN(db1->size, db2->size);
29056557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
29156557Sbostic 		if (*p1++ != *p2++) {
29256557Sbostic 			printf("compare failed at offset %d\n",
29356557Sbostic 			    p1 - (u_char *)db1->data);
29456557Sbostic 			break;
29556557Sbostic 		}
29656557Sbostic }
29756557Sbostic 
29856557Sbostic void
get(dbp,kp)29956039Sbostic get(dbp, kp)
30056039Sbostic 	DB *dbp;
30156039Sbostic 	DBT *kp;
30256039Sbostic {
30356039Sbostic 	DBT data;
30456039Sbostic 
30556059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
30656059Sbostic 	case 0:
30756059Sbostic 		(void)write(ofd, data.data, data.size);
30856059Sbostic 		break;
30956059Sbostic 	case -1:
31056039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
31156059Sbostic 		/* NOTREACHED */
31256059Sbostic 	case 1:
31356059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
31457456Sbostic 		(void)fprintf(stderr, "%d: %.*s: %s\n",
31557456Sbostic 		    lineno, kp->size, kp->data, NOSUCHKEY);
31656059Sbostic 		break;
31756059Sbostic 	}
31856039Sbostic }
31956039Sbostic 
32056039Sbostic void
getdata(dbp,kp,dp)32156557Sbostic getdata(dbp, kp, dp)
32256557Sbostic 	DB *dbp;
32356557Sbostic 	DBT *kp, *dp;
32456557Sbostic {
32556557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
32656557Sbostic 	case 0:
32756557Sbostic 		return;
32856557Sbostic 	case -1:
32956557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
33056557Sbostic 		/* NOTREACHED */
33156557Sbostic 	case 1:
33256557Sbostic 		err("line %lu: get failed, no such key", lineno);
33356557Sbostic 		/* NOTREACHED */
33456557Sbostic 	}
33556557Sbostic }
33656557Sbostic 
33756557Sbostic void
put(dbp,kp,dp)33856039Sbostic put(dbp, kp, dp)
33956039Sbostic 	DB *dbp;
34056039Sbostic 	DBT *kp, *dp;
34156039Sbostic {
34256059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
34356059Sbostic 	case 0:
34456059Sbostic 		break;
34556059Sbostic 	case -1:
34656039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
34756059Sbostic 		/* NOTREACHED */
34856059Sbostic 	case 1:
34956059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
35056059Sbostic 		break;
35156059Sbostic 	}
35256039Sbostic }
35356039Sbostic 
35456039Sbostic void
rem(dbp,kp)35556039Sbostic rem(dbp, kp)
35656039Sbostic 	DB *dbp;
35756039Sbostic 	DBT *kp;
35856039Sbostic {
35956059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
36056059Sbostic 	case 0:
36156059Sbostic 		break;
36256059Sbostic 	case -1:
36356039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
36456059Sbostic 		/* NOTREACHED */
36556059Sbostic 	case 1:
36656059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
36756059Sbostic 		break;
36856059Sbostic 	}
36956039Sbostic }
37056039Sbostic 
37156039Sbostic void
seq(dbp,kp)37256059Sbostic seq(dbp, kp)
37356039Sbostic 	DB *dbp;
37456059Sbostic 	DBT *kp;
37556039Sbostic {
37656059Sbostic 	DBT data;
37756039Sbostic 
37856059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
37956059Sbostic 	case 0:
38056059Sbostic 		(void)write(ofd, data.data, data.size);
38156059Sbostic 		break;
38256059Sbostic 	case -1:
38356039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
38456059Sbostic 		/* NOTREACHED */
38556059Sbostic 	case 1:
38656059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
38756059Sbostic 		break;
38856059Sbostic 	}
38956039Sbostic }
39056992Sbostic 
39156992Sbostic void
dump(dbp,rev)39256992Sbostic dump(dbp, rev)
39356992Sbostic 	DB *dbp;
39456992Sbostic 	int rev;
39556992Sbostic {
39656992Sbostic 	DBT key, data;
39756992Sbostic 	int flags, nflags;
39856992Sbostic 
39956992Sbostic 	if (rev) {
40056992Sbostic 		flags = R_LAST;
40156992Sbostic 		nflags = R_PREV;
40256992Sbostic 	} else {
40356992Sbostic 		flags = R_FIRST;
40456992Sbostic 		nflags = R_NEXT;
40556992Sbostic 	}
40656992Sbostic 	for (;; flags = nflags)
40756992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
40856992Sbostic 		case 0:
40956992Sbostic 			(void)write(ofd, data.data, data.size);
41056992Sbostic 			break;
41156992Sbostic 		case 1:
41256992Sbostic 			goto done;
41356992Sbostic 		case -1:
41456992Sbostic 			err("line %lu: (dump) seq: %s",
41556992Sbostic 			    lineno, strerror(errno));
41656992Sbostic 			/* NOTREACHED */
41756992Sbostic 		}
41856992Sbostic done:	return;
41956992Sbostic }
42056039Sbostic 
42156039Sbostic u_int
setflags(s)42256039Sbostic setflags(s)
42356039Sbostic 	char *s;
42456039Sbostic {
425*66211Sbostic 	char *p, *index();
42656039Sbostic 
42756039Sbostic 	for (; isspace(*s); ++s);
42856039Sbostic 	if (*s == '\n')
42956039Sbostic 		return (0);
43056039Sbostic 	if ((p = index(s, '\n')) != NULL)
43156039Sbostic 		*p = '\0';
43256039Sbostic 	if (!strcmp(s, "R_CURSOR"))
43356039Sbostic 		return (R_CURSOR);
43456749Sbostic 	if (!strcmp(s, "R_FIRST"))
43556749Sbostic 		return (R_FIRST);
43656039Sbostic 	if (!strcmp(s, "R_IAFTER"))
43756039Sbostic 		return (R_IAFTER);
43856039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
43956039Sbostic 		return (R_IBEFORE);
44056039Sbostic 	if (!strcmp(s, "R_LAST"))
44156039Sbostic 		return (R_LAST);
44256039Sbostic 	if (!strcmp(s, "R_NEXT"))
44356039Sbostic 		return (R_NEXT);
44456749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
44556749Sbostic 		return (R_NOOVERWRITE);
44656039Sbostic 	if (!strcmp(s, "R_PREV"))
44756039Sbostic 		return (R_PREV);
44856749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
44956749Sbostic 		return (R_SETCURSOR);
45056039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
45156039Sbostic 	/* NOTREACHED */
45256039Sbostic }
45356039Sbostic 
45456039Sbostic DBTYPE
dbtype(s)45556039Sbostic dbtype(s)
45656039Sbostic 	char *s;
45756039Sbostic {
45856039Sbostic 	if (!strcmp(s, "btree"))
45956039Sbostic 		return (DB_BTREE);
46056039Sbostic 	if (!strcmp(s, "hash"))
46156039Sbostic 		return (DB_HASH);
46256039Sbostic 	if (!strcmp(s, "recno"))
46356039Sbostic 		return (DB_RECNO);
46456039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
46556039Sbostic 	/* NOTREACHED */
46656039Sbostic }
46756039Sbostic 
46856039Sbostic void *
setinfo(type,s)46956039Sbostic setinfo(type, s)
47056039Sbostic 	DBTYPE type;
47156039Sbostic 	char *s;
47256039Sbostic {
47356039Sbostic 	static BTREEINFO ib;
47456039Sbostic 	static HASHINFO ih;
47556039Sbostic 	static RECNOINFO rh;
476*66211Sbostic 	char *eq, *index();
47756039Sbostic 
47856039Sbostic 	if ((eq = index(s, '=')) == NULL)
47956039Sbostic 		err("%s: illegal structure set statement", s);
48056039Sbostic 	*eq++ = '\0';
48156039Sbostic 	if (!isdigit(*eq))
48256039Sbostic 		err("%s: structure set statement must be a number", s);
48356039Sbostic 
48456039Sbostic 	switch(type) {
48556039Sbostic 	case DB_BTREE:
48656039Sbostic 		if (!strcmp("flags", s)) {
48764288Sbostic 			ib.flags = atoi(eq);
48856039Sbostic 			return (&ib);
48956039Sbostic 		}
49056039Sbostic 		if (!strcmp("cachesize", s)) {
49164288Sbostic 			ib.cachesize = atoi(eq);
49256039Sbostic 			return (&ib);
49356039Sbostic 		}
49456039Sbostic 		if (!strcmp("maxkeypage", s)) {
49564288Sbostic 			ib.maxkeypage = atoi(eq);
49656039Sbostic 			return (&ib);
49756039Sbostic 		}
49856039Sbostic 		if (!strcmp("minkeypage", s)) {
49964288Sbostic 			ib.minkeypage = atoi(eq);
50056039Sbostic 			return (&ib);
50156039Sbostic 		}
50256039Sbostic 		if (!strcmp("lorder", s)) {
50364288Sbostic 			ib.lorder = atoi(eq);
50456039Sbostic 			return (&ib);
50556039Sbostic 		}
50657456Sbostic 		if (!strcmp("psize", s)) {
50764288Sbostic 			ib.psize = atoi(eq);
50857456Sbostic 			return (&ib);
50957456Sbostic 		}
51056039Sbostic 		break;
51156039Sbostic 	case DB_HASH:
51256039Sbostic 		if (!strcmp("bsize", s)) {
51364288Sbostic 			ih.bsize = atoi(eq);
51460130Sbostic 			return (&ih);
51556039Sbostic 		}
51656039Sbostic 		if (!strcmp("ffactor", s)) {
51764288Sbostic 			ih.ffactor = atoi(eq);
51860130Sbostic 			return (&ih);
51956039Sbostic 		}
52056039Sbostic 		if (!strcmp("nelem", s)) {
52164288Sbostic 			ih.nelem = atoi(eq);
52260130Sbostic 			return (&ih);
52356039Sbostic 		}
52456039Sbostic 		if (!strcmp("cachesize", s)) {
52564288Sbostic 			ih.cachesize = atoi(eq);
52660130Sbostic 			return (&ih);
52756039Sbostic 		}
52856039Sbostic 		if (!strcmp("lorder", s)) {
52964288Sbostic 			ih.lorder = atoi(eq);
53060130Sbostic 			return (&ih);
53156039Sbostic 		}
53256039Sbostic 		break;
53356039Sbostic 	case DB_RECNO:
53456039Sbostic 		if (!strcmp("flags", s)) {
53564288Sbostic 			rh.flags = atoi(eq);
53660130Sbostic 			return (&rh);
53756039Sbostic 		}
53856039Sbostic 		if (!strcmp("cachesize", s)) {
53964288Sbostic 			rh.cachesize = atoi(eq);
54060130Sbostic 			return (&rh);
54156039Sbostic 		}
54256039Sbostic 		if (!strcmp("lorder", s)) {
54364288Sbostic 			rh.lorder = atoi(eq);
54460130Sbostic 			return (&rh);
54556039Sbostic 		}
54656039Sbostic 		if (!strcmp("reclen", s)) {
54764288Sbostic 			rh.reclen = atoi(eq);
54860130Sbostic 			return (&rh);
54956039Sbostic 		}
55056039Sbostic 		if (!strcmp("bval", s)) {
55164288Sbostic 			rh.bval = atoi(eq);
55260130Sbostic 			return (&rh);
55356039Sbostic 		}
55460229Sbostic 		if (!strcmp("psize", s)) {
55564288Sbostic 			rh.psize = atoi(eq);
55660229Sbostic 			return (&rh);
55760229Sbostic 		}
55856039Sbostic 		break;
55956039Sbostic 	}
56056039Sbostic 	err("%s: unknown structure value", s);
56156039Sbostic 	/* NOTREACHED */
56256039Sbostic }
56356039Sbostic 
56456039Sbostic void *
rfile(name,lenp)56556039Sbostic rfile(name, lenp)
56656039Sbostic 	char *name;
56756039Sbostic 	size_t *lenp;
56856039Sbostic {
56956039Sbostic 	struct stat sb;
57056039Sbostic 	void *p;
57156039Sbostic 	int fd;
572*66211Sbostic 	char *np, *index();
57356039Sbostic 
57456039Sbostic 	for (; isspace(*name); ++name);
57556039Sbostic 	if ((np = index(name, '\n')) != NULL)
57656039Sbostic 		*np = '\0';
57756039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
57856039Sbostic 	    fstat(fd, &sb))
57956039Sbostic 		err("%s: %s\n", name, strerror(errno));
58064367Sbostic #ifdef NOT_PORTABLE
58158312Sbostic 	if (sb.st_size > (off_t)SIZE_T_MAX)
58256039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
58364367Sbostic #endif
584*66211Sbostic 	if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
58556039Sbostic 		err("%s", strerror(errno));
58656039Sbostic 	(void)read(fd, p, (int)sb.st_size);
58756039Sbostic 	*lenp = sb.st_size;
58856489Sbostic 	(void)close(fd);
58956039Sbostic 	return (p);
59056039Sbostic }
59156039Sbostic 
59256039Sbostic void *
xmalloc(text,len)59356039Sbostic xmalloc(text, len)
59456039Sbostic 	char *text;
59556039Sbostic 	size_t len;
59656039Sbostic {
59756039Sbostic 	void *p;
59856039Sbostic 
599*66211Sbostic 	if ((p = (void *)malloc(len)) == NULL)
60056039Sbostic 		err("%s", strerror(errno));
60158015Sbostic 	memmove(p, text, len);
60256039Sbostic 	return (p);
60356039Sbostic }
60456039Sbostic 
60556039Sbostic void
usage()60656039Sbostic usage()
60756039Sbostic {
60856039Sbostic 	(void)fprintf(stderr,
60964453Sbostic 	    "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
61056039Sbostic 	exit(1);
61156039Sbostic }
61256039Sbostic 
61356039Sbostic #if __STDC__
61456039Sbostic #include <stdarg.h>
61556039Sbostic #else
61656039Sbostic #include <varargs.h>
61756039Sbostic #endif
61856039Sbostic 
61956039Sbostic void
62056039Sbostic #if __STDC__
err(const char * fmt,...)62156039Sbostic err(const char *fmt, ...)
62256039Sbostic #else
62356039Sbostic err(fmt, va_alist)
62456039Sbostic 	char *fmt;
62556039Sbostic         va_dcl
62656039Sbostic #endif
62756039Sbostic {
62856039Sbostic 	va_list ap;
62956039Sbostic #if __STDC__
63056039Sbostic 	va_start(ap, fmt);
63156039Sbostic #else
63256039Sbostic 	va_start(ap);
63356039Sbostic #endif
63456039Sbostic 	(void)fprintf(stderr, "dbtest: ");
63556039Sbostic 	(void)vfprintf(stderr, fmt, ap);
63656039Sbostic 	va_end(ap);
63756039Sbostic 	(void)fprintf(stderr, "\n");
63856039Sbostic 	exit(1);
63956039Sbostic 	/* NOTREACHED */
64056039Sbostic }
641