xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 64486)
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*64486Sbostic static char sccsid[] = "@(#)dbtest.c	8.6 (Berkeley) 09/16/93";
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>
3164288Sbostic #include "../PORT/include/compat.h"
3259492Sbostic 
3356557Sbostic enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
3456039Sbostic 
3556557Sbostic void	 compare __P((DBT *, DBT *));
3656039Sbostic DBTYPE	 dbtype __P((char *));
3756992Sbostic void	 dump __P((DB *, int));
3856039Sbostic void	 err __P((const char *, ...));
3956039Sbostic void	 get __P((DB *, DBT *));
4056557Sbostic void	 getdata __P((DB *, DBT *, DBT *));
4156039Sbostic void	 put __P((DB *, DBT *, DBT *));
4256039Sbostic void	 rem __P((DB *, DBT *));
4356039Sbostic void	*rfile __P((char *, size_t *));
4456059Sbostic void	 seq __P((DB *, DBT *));
4556039Sbostic u_int	 setflags __P((char *));
4656039Sbostic void	*setinfo __P((DBTYPE, char *));
4756039Sbostic void	 usage __P((void));
4856039Sbostic void	*xmalloc __P((char *, size_t));
4956039Sbostic 
5056039Sbostic DBTYPE type;
5156039Sbostic void *infop;
5256039Sbostic u_long lineno;
5356039Sbostic u_int flags;
5456039Sbostic int ofd = STDOUT_FILENO;
5556039Sbostic 
5657456Sbostic DB *XXdbp;				/* Global for gdb. */
5757456Sbostic 
5856039Sbostic int
5956039Sbostic main(argc, argv)
6056039Sbostic 	int argc;
6156039Sbostic 	char *argv[];
6256039Sbostic {
6357461Sbostic 	extern int optind;
6457461Sbostic 	extern char *optarg;
6556039Sbostic 	enum S command, state;
6656039Sbostic 	DB *dbp;
6756557Sbostic 	DBT data, key, keydata;
6856039Sbostic 	size_t len;
6964453Sbostic 	int ch, oflags;
7059626Sbostic 	char *fname, *infoarg, *p, buf[8 * 1024];
7156039Sbostic 
7256039Sbostic 	infoarg = NULL;
7359626Sbostic 	fname = NULL;
7464453Sbostic 	oflags = O_CREAT | O_RDWR;
7564453Sbostic 	while ((ch = getopt(argc, argv, "f:i:lo:")) != EOF)
7656039Sbostic 		switch(ch) {
7759626Sbostic 		case 'f':
7859626Sbostic 			fname = optarg;
7959626Sbostic 			break;
8056039Sbostic 		case 'i':
8156039Sbostic 			infoarg = optarg;
8256039Sbostic 			break;
8364453Sbostic 		case 'l':
8464453Sbostic 			oflags |= DB_LOCK;
8564453Sbostic 			break;
8656039Sbostic 		case 'o':
8756039Sbostic 			if ((ofd = open(optarg,
8856039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
8956039Sbostic 				err("%s: %s", optarg, strerror(errno));
9056039Sbostic 			break;
9156039Sbostic 		case '?':
9256039Sbostic 		default:
9356039Sbostic 			usage();
9456039Sbostic 		}
9556039Sbostic 	argc -= optind;
9656039Sbostic 	argv += optind;
9756039Sbostic 
9856039Sbostic 	if (argc != 2)
9956039Sbostic 		usage();
10056039Sbostic 
10156039Sbostic 	/* Set the type. */
10256039Sbostic 	type = dbtype(*argv++);
10356039Sbostic 
10456039Sbostic 	/* Open the descriptor file. */
10556039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
10656039Sbostic 		err("%s: %s", *argv, strerror(errno));
10756039Sbostic 
10856039Sbostic 	/* Set up the db structure as necessary. */
10956039Sbostic 	if (infoarg == NULL)
11056039Sbostic 		infop = NULL;
11156039Sbostic 	else
11257463Sbostic 		for (p = strtok(infoarg, ",\t "); p != NULL;
11357463Sbostic 		    p = strtok(0, ",\t "))
11456039Sbostic 			if (*p != '\0')
11556039Sbostic 				infop = setinfo(type, p);
11656039Sbostic 
11759626Sbostic 	/* Open the DB. */
11859626Sbostic 	if (fname == NULL) {
11960129Smargo 		p = getenv("TMPDIR");
12060129Smargo 		if (p == NULL)
12160129Smargo 			p = "/var/tmp";
12260129Smargo 		(void)sprintf(buf, "%s/__dbtest", p);
12360129Smargo 		fname = buf;
12460129Smargo 		(void)unlink(buf);
12559626Sbostic 	}
12659626Sbostic 	if ((dbp = dbopen(fname,
12764453Sbostic 	    oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
12856039Sbostic 		err("dbopen: %s", strerror(errno));
12957456Sbostic 	XXdbp = dbp;
13056039Sbostic 
13156039Sbostic 	state = COMMAND;
13256039Sbostic 	for (lineno = 1;
13356039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
13456039Sbostic 		len = strlen(buf);
13556039Sbostic 		switch(*p) {
13656557Sbostic 		case 'c':			/* compare */
13756557Sbostic 			if (state != COMMAND)
13856557Sbostic 				err("line %lu: not expecting command", lineno);
13956557Sbostic 			state = KEY;
14056557Sbostic 			command = COMPARE;
14156557Sbostic 			break;
14256059Sbostic 		case 'e':			/* echo */
14356059Sbostic 			if (state != COMMAND)
14456059Sbostic 				err("line %lu: not expecting command", lineno);
14556557Sbostic 			/* Don't display the newline, if CR at EOL. */
14656557Sbostic 			if (p[len - 2] == '\r')
14756557Sbostic 				--len;
14856557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
14956557Sbostic 				err("write: %s", strerror(errno));
15056059Sbostic 			break;
15156039Sbostic 		case 'g':			/* get */
15256039Sbostic 			if (state != COMMAND)
15356039Sbostic 				err("line %lu: not expecting command", lineno);
15456039Sbostic 			state = KEY;
15556039Sbostic 			command = GET;
15656039Sbostic 			break;
15756039Sbostic 		case 'p':			/* put */
15856039Sbostic 			if (state != COMMAND)
15956039Sbostic 				err("line %lu: not expecting command", lineno);
16056039Sbostic 			state = KEY;
16156039Sbostic 			command = PUT;
16256039Sbostic 			break;
16356039Sbostic 		case 'r':			/* remove */
16456039Sbostic 			if (state != COMMAND)
16556039Sbostic 				err("line %lu: not expecting command", lineno);
16656039Sbostic 			state = KEY;
16756039Sbostic 			command = REMOVE;
16856039Sbostic 			break;
16956039Sbostic 		case 's':			/* seq */
17056039Sbostic 			if (state != COMMAND)
17156039Sbostic 				err("line %lu: not expecting command", lineno);
17256059Sbostic 			if (flags == R_CURSOR) {
17356059Sbostic 				state = KEY;
17456059Sbostic 				command = SEQ;
17556059Sbostic 			} else
17656059Sbostic 				seq(dbp, &key);
17756039Sbostic 			break;
17856039Sbostic 		case 'f':
17956059Sbostic 			flags = setflags(p + 1);
18056039Sbostic 			break;
18156039Sbostic 		case 'D':			/* data file */
18256039Sbostic 			if (state != DATA)
18356039Sbostic 				err("line %lu: not expecting data", lineno);
18456039Sbostic 			data.data = rfile(p + 1, &data.size);
18556992Sbostic 			goto ldata;
18656039Sbostic 		case 'd':			/* data */
18756039Sbostic 			if (state != DATA)
18856039Sbostic 				err("line %lu: not expecting data", lineno);
18956557Sbostic 			data.data = xmalloc(p + 1, len - 1);
19056557Sbostic 			data.size = len - 1;
19156992Sbostic ldata:			switch(command) {
19256557Sbostic 			case COMPARE:
19356557Sbostic 				compare(&keydata, &data);
19456557Sbostic 				break;
19556557Sbostic 			case PUT:
19656557Sbostic 				put(dbp, &key, &data);
19756557Sbostic 				break;
19856557Sbostic 			default:
19956039Sbostic 				err("line %lu: command doesn't take data",
20056039Sbostic 				    lineno);
20156557Sbostic 			}
20259493Sbostic 			if (type != DB_RECNO)
20359493Sbostic 				free(key.data);
20456039Sbostic 			free(data.data);
20556039Sbostic 			state = COMMAND;
20656039Sbostic 			break;
20756039Sbostic 		case 'K':			/* key file */
20856039Sbostic 			if (state != KEY)
20956039Sbostic 				err("line %lu: not expecting a key", lineno);
21056039Sbostic 			if (type == DB_RECNO)
21156039Sbostic 				err("line %lu: 'K' not available for recno",
21256039Sbostic 				    lineno);
21356039Sbostic 			key.data = rfile(p + 1, &key.size);
21456992Sbostic 			goto lkey;
21556039Sbostic 		case 'k':			/* key */
21656039Sbostic 			if (state != KEY)
21756039Sbostic 				err("line %lu: not expecting a key", lineno);
21856039Sbostic 			if (type == DB_RECNO) {
21956039Sbostic 				static recno_t recno;
22064443Sbostic 				recno = atoi(p + 1);
22156039Sbostic 				key.data = &recno;
22256039Sbostic 				key.size = sizeof(recno);
22356039Sbostic 			} else {
22456039Sbostic 				key.data = xmalloc(p + 1, len - 1);
22556039Sbostic 				key.size = len - 1;
22656039Sbostic 			}
22756992Sbostic lkey:			switch(command) {
22856557Sbostic 			case COMPARE:
22956557Sbostic 				getdata(dbp, &key, &keydata);
23056557Sbostic 				state = DATA;
23156557Sbostic 				break;
23256039Sbostic 			case GET:
23356039Sbostic 				get(dbp, &key);
23456039Sbostic 				if (type != DB_RECNO)
23556039Sbostic 					free(key.data);
23656039Sbostic 				state = COMMAND;
23756039Sbostic 				break;
23856039Sbostic 			case PUT:
23956039Sbostic 				state = DATA;
24056039Sbostic 				break;
24156039Sbostic 			case REMOVE:
24256039Sbostic 				rem(dbp, &key);
24356039Sbostic 				if (type != DB_RECNO)
24456039Sbostic 					free(key.data);
24556039Sbostic 				state = COMMAND;
24656039Sbostic 				break;
24756059Sbostic 			case SEQ:
24856059Sbostic 				seq(dbp, &key);
24956059Sbostic 				if (type != DB_RECNO)
25056059Sbostic 					free(key.data);
25156059Sbostic 				state = COMMAND;
25256059Sbostic 				break;
25356039Sbostic 			default:
25456039Sbostic 				err("line %lu: command doesn't take a key",
25556039Sbostic 				    lineno);
25656039Sbostic 			}
25756039Sbostic 			break;
25856992Sbostic 		case 'o':
25956992Sbostic 			dump(dbp, p[1] == 'r');
26056992Sbostic 			break;
26156039Sbostic 		default:
26256039Sbostic 			err("line %lu: %s: unknown command character",
26356059Sbostic 			    p, lineno);
26456039Sbostic 		}
26556039Sbostic 	}
266*64486Sbostic #ifdef STATISTICS
267*64486Sbostic 	if (type == DB_BTREE)
268*64486Sbostic 		__bt_stat(dbp);
269*64486Sbostic #endif
27059626Sbostic 	if (dbp->close(dbp))
27159626Sbostic 		err("db->close: %s", strerror(errno));
27256039Sbostic 	(void)close(ofd);
27356039Sbostic 	exit(0);
27456039Sbostic }
27556039Sbostic 
27656059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
27756059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
27856059Sbostic 
27956039Sbostic void
28056557Sbostic compare(db1, db2)
28156557Sbostic 	DBT *db1, *db2;
28256557Sbostic {
28356557Sbostic 	register size_t len;
28456557Sbostic 	register u_char *p1, *p2;
28556557Sbostic 
28656557Sbostic 	if (db1->size != db2->size)
28756557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
28856557Sbostic 		    db1->size, db2->size);
28956557Sbostic 
29056557Sbostic 	len = MIN(db1->size, db2->size);
29156557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
29256557Sbostic 		if (*p1++ != *p2++) {
29356557Sbostic 			printf("compare failed at offset %d\n",
29456557Sbostic 			    p1 - (u_char *)db1->data);
29556557Sbostic 			break;
29656557Sbostic 		}
29756557Sbostic }
29856557Sbostic 
29956557Sbostic void
30056039Sbostic get(dbp, kp)
30156039Sbostic 	DB *dbp;
30256039Sbostic 	DBT *kp;
30356039Sbostic {
30456039Sbostic 	DBT data;
30556039Sbostic 
30656059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
30756059Sbostic 	case 0:
30856059Sbostic 		(void)write(ofd, data.data, data.size);
30956059Sbostic 		break;
31056059Sbostic 	case -1:
31156039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
31256059Sbostic 		/* NOTREACHED */
31356059Sbostic 	case 1:
31456059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
31557456Sbostic 		(void)fprintf(stderr, "%d: %.*s: %s\n",
31657456Sbostic 		    lineno, kp->size, kp->data, NOSUCHKEY);
31756059Sbostic 		break;
31856059Sbostic 	}
31956039Sbostic }
32056039Sbostic 
32156039Sbostic void
32256557Sbostic getdata(dbp, kp, dp)
32356557Sbostic 	DB *dbp;
32456557Sbostic 	DBT *kp, *dp;
32556557Sbostic {
32656557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
32756557Sbostic 	case 0:
32856557Sbostic 		return;
32956557Sbostic 	case -1:
33056557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
33156557Sbostic 		/* NOTREACHED */
33256557Sbostic 	case 1:
33356557Sbostic 		err("line %lu: get failed, no such key", lineno);
33456557Sbostic 		/* NOTREACHED */
33556557Sbostic 	}
33656557Sbostic }
33756557Sbostic 
33856557Sbostic void
33956039Sbostic put(dbp, kp, dp)
34056039Sbostic 	DB *dbp;
34156039Sbostic 	DBT *kp, *dp;
34256039Sbostic {
34356059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
34456059Sbostic 	case 0:
34556059Sbostic 		break;
34656059Sbostic 	case -1:
34756039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
34856059Sbostic 		/* NOTREACHED */
34956059Sbostic 	case 1:
35056059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
35156059Sbostic 		break;
35256059Sbostic 	}
35356039Sbostic }
35456039Sbostic 
35556039Sbostic void
35656039Sbostic rem(dbp, kp)
35756039Sbostic 	DB *dbp;
35856039Sbostic 	DBT *kp;
35956039Sbostic {
36056059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
36156059Sbostic 	case 0:
36256059Sbostic 		break;
36356059Sbostic 	case -1:
36456039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
36556059Sbostic 		/* NOTREACHED */
36656059Sbostic 	case 1:
36756059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
36856059Sbostic 		break;
36956059Sbostic 	}
37056039Sbostic }
37156039Sbostic 
37256039Sbostic void
37356059Sbostic seq(dbp, kp)
37456039Sbostic 	DB *dbp;
37556059Sbostic 	DBT *kp;
37656039Sbostic {
37756059Sbostic 	DBT data;
37856039Sbostic 
37956059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
38056059Sbostic 	case 0:
38156059Sbostic 		(void)write(ofd, data.data, data.size);
38256059Sbostic 		break;
38356059Sbostic 	case -1:
38456039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
38556059Sbostic 		/* NOTREACHED */
38656059Sbostic 	case 1:
38756059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
38856059Sbostic 		break;
38956059Sbostic 	}
39056039Sbostic }
39156992Sbostic 
39256992Sbostic void
39356992Sbostic dump(dbp, rev)
39456992Sbostic 	DB *dbp;
39556992Sbostic 	int rev;
39656992Sbostic {
39756992Sbostic 	DBT key, data;
39856992Sbostic 	int flags, nflags;
39956992Sbostic 
40056992Sbostic 	if (rev) {
40156992Sbostic 		flags = R_LAST;
40256992Sbostic 		nflags = R_PREV;
40356992Sbostic 	} else {
40456992Sbostic 		flags = R_FIRST;
40556992Sbostic 		nflags = R_NEXT;
40656992Sbostic 	}
40756992Sbostic 	for (;; flags = nflags)
40856992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
40956992Sbostic 		case 0:
41056992Sbostic 			(void)write(ofd, data.data, data.size);
41156992Sbostic 			break;
41256992Sbostic 		case 1:
41356992Sbostic 			goto done;
41456992Sbostic 		case -1:
41556992Sbostic 			err("line %lu: (dump) seq: %s",
41656992Sbostic 			    lineno, strerror(errno));
41756992Sbostic 			/* NOTREACHED */
41856992Sbostic 		}
41956992Sbostic done:	return;
42056992Sbostic }
42156039Sbostic 
42256039Sbostic u_int
42356039Sbostic setflags(s)
42456039Sbostic 	char *s;
42556039Sbostic {
42656039Sbostic 	char *p;
42756039Sbostic 
42856039Sbostic 	for (; isspace(*s); ++s);
42956039Sbostic 	if (*s == '\n')
43056039Sbostic 		return (0);
43156039Sbostic 	if ((p = index(s, '\n')) != NULL)
43256039Sbostic 		*p = '\0';
43356039Sbostic 	if (!strcmp(s, "R_CURSOR"))
43456039Sbostic 		return (R_CURSOR);
43556749Sbostic 	if (!strcmp(s, "R_FIRST"))
43656749Sbostic 		return (R_FIRST);
43756039Sbostic 	if (!strcmp(s, "R_IAFTER"))
43856039Sbostic 		return (R_IAFTER);
43956039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
44056039Sbostic 		return (R_IBEFORE);
44156039Sbostic 	if (!strcmp(s, "R_LAST"))
44256039Sbostic 		return (R_LAST);
44356039Sbostic 	if (!strcmp(s, "R_NEXT"))
44456039Sbostic 		return (R_NEXT);
44556749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
44656749Sbostic 		return (R_NOOVERWRITE);
44756039Sbostic 	if (!strcmp(s, "R_PREV"))
44856039Sbostic 		return (R_PREV);
44956749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
45056749Sbostic 		return (R_SETCURSOR);
45156039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
45256039Sbostic 	/* NOTREACHED */
45356039Sbostic }
45456039Sbostic 
45556039Sbostic DBTYPE
45656039Sbostic dbtype(s)
45756039Sbostic 	char *s;
45856039Sbostic {
45956039Sbostic 	if (!strcmp(s, "btree"))
46056039Sbostic 		return (DB_BTREE);
46156039Sbostic 	if (!strcmp(s, "hash"))
46256039Sbostic 		return (DB_HASH);
46356039Sbostic 	if (!strcmp(s, "recno"))
46456039Sbostic 		return (DB_RECNO);
46556039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
46656039Sbostic 	/* NOTREACHED */
46756039Sbostic }
46856039Sbostic 
46956039Sbostic void *
47056039Sbostic setinfo(type, s)
47156039Sbostic 	DBTYPE type;
47256039Sbostic 	char *s;
47356039Sbostic {
47456039Sbostic 	static BTREEINFO ib;
47556039Sbostic 	static HASHINFO ih;
47656039Sbostic 	static RECNOINFO rh;
47756039Sbostic 	char *eq;
47856039Sbostic 
47956039Sbostic 	if ((eq = index(s, '=')) == NULL)
48056039Sbostic 		err("%s: illegal structure set statement", s);
48156039Sbostic 	*eq++ = '\0';
48256039Sbostic 	if (!isdigit(*eq))
48356039Sbostic 		err("%s: structure set statement must be a number", s);
48456039Sbostic 
48556039Sbostic 	switch(type) {
48656039Sbostic 	case DB_BTREE:
48756039Sbostic 		if (!strcmp("flags", s)) {
48864288Sbostic 			ib.flags = atoi(eq);
48956039Sbostic 			return (&ib);
49056039Sbostic 		}
49156039Sbostic 		if (!strcmp("cachesize", s)) {
49264288Sbostic 			ib.cachesize = atoi(eq);
49356039Sbostic 			return (&ib);
49456039Sbostic 		}
49556039Sbostic 		if (!strcmp("maxkeypage", s)) {
49664288Sbostic 			ib.maxkeypage = atoi(eq);
49756039Sbostic 			return (&ib);
49856039Sbostic 		}
49956039Sbostic 		if (!strcmp("minkeypage", s)) {
50064288Sbostic 			ib.minkeypage = atoi(eq);
50156039Sbostic 			return (&ib);
50256039Sbostic 		}
50356039Sbostic 		if (!strcmp("lorder", s)) {
50464288Sbostic 			ib.lorder = atoi(eq);
50556039Sbostic 			return (&ib);
50656039Sbostic 		}
50757456Sbostic 		if (!strcmp("psize", s)) {
50864288Sbostic 			ib.psize = atoi(eq);
50957456Sbostic 			return (&ib);
51057456Sbostic 		}
51156039Sbostic 		break;
51256039Sbostic 	case DB_HASH:
51356039Sbostic 		if (!strcmp("bsize", s)) {
51464288Sbostic 			ih.bsize = atoi(eq);
51560130Sbostic 			return (&ih);
51656039Sbostic 		}
51756039Sbostic 		if (!strcmp("ffactor", s)) {
51864288Sbostic 			ih.ffactor = atoi(eq);
51960130Sbostic 			return (&ih);
52056039Sbostic 		}
52156039Sbostic 		if (!strcmp("nelem", s)) {
52264288Sbostic 			ih.nelem = atoi(eq);
52360130Sbostic 			return (&ih);
52456039Sbostic 		}
52556039Sbostic 		if (!strcmp("cachesize", s)) {
52664288Sbostic 			ih.cachesize = atoi(eq);
52760130Sbostic 			return (&ih);
52856039Sbostic 		}
52956039Sbostic 		if (!strcmp("lorder", s)) {
53064288Sbostic 			ih.lorder = atoi(eq);
53160130Sbostic 			return (&ih);
53256039Sbostic 		}
53356039Sbostic 		break;
53456039Sbostic 	case DB_RECNO:
53556039Sbostic 		if (!strcmp("flags", s)) {
53664288Sbostic 			rh.flags = atoi(eq);
53760130Sbostic 			return (&rh);
53856039Sbostic 		}
53956039Sbostic 		if (!strcmp("cachesize", s)) {
54064288Sbostic 			rh.cachesize = atoi(eq);
54160130Sbostic 			return (&rh);
54256039Sbostic 		}
54356039Sbostic 		if (!strcmp("lorder", s)) {
54464288Sbostic 			rh.lorder = atoi(eq);
54560130Sbostic 			return (&rh);
54656039Sbostic 		}
54756039Sbostic 		if (!strcmp("reclen", s)) {
54864288Sbostic 			rh.reclen = atoi(eq);
54960130Sbostic 			return (&rh);
55056039Sbostic 		}
55156039Sbostic 		if (!strcmp("bval", s)) {
55264288Sbostic 			rh.bval = atoi(eq);
55360130Sbostic 			return (&rh);
55456039Sbostic 		}
55560229Sbostic 		if (!strcmp("psize", s)) {
55664288Sbostic 			rh.psize = atoi(eq);
55760229Sbostic 			return (&rh);
55860229Sbostic 		}
55956039Sbostic 		break;
56056039Sbostic 	}
56156039Sbostic 	err("%s: unknown structure value", s);
56256039Sbostic 	/* NOTREACHED */
56356039Sbostic }
56456039Sbostic 
56556039Sbostic void *
56656039Sbostic rfile(name, lenp)
56756039Sbostic 	char *name;
56856039Sbostic 	size_t *lenp;
56956039Sbostic {
57056039Sbostic 	struct stat sb;
57156039Sbostic 	void *p;
57256039Sbostic 	int fd;
57356039Sbostic 	char *np;
57456039Sbostic 
57556039Sbostic 	for (; isspace(*name); ++name);
57656039Sbostic 	if ((np = index(name, '\n')) != NULL)
57756039Sbostic 		*np = '\0';
57856039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
57956039Sbostic 	    fstat(fd, &sb))
58056039Sbostic 		err("%s: %s\n", name, strerror(errno));
58164367Sbostic #ifdef NOT_PORTABLE
58258312Sbostic 	if (sb.st_size > (off_t)SIZE_T_MAX)
58356039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
58464367Sbostic #endif
58556039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
58656039Sbostic 		err("%s", strerror(errno));
58756039Sbostic 	(void)read(fd, p, (int)sb.st_size);
58856039Sbostic 	*lenp = sb.st_size;
58956489Sbostic 	(void)close(fd);
59056039Sbostic 	return (p);
59156039Sbostic }
59256039Sbostic 
59356039Sbostic void *
59456039Sbostic xmalloc(text, len)
59556039Sbostic 	char *text;
59656039Sbostic 	size_t len;
59756039Sbostic {
59856039Sbostic 	void *p;
59956039Sbostic 
60056039Sbostic 	if ((p = malloc(len)) == NULL)
60156039Sbostic 		err("%s", strerror(errno));
60258015Sbostic 	memmove(p, text, len);
60356039Sbostic 	return (p);
60456039Sbostic }
60556039Sbostic 
60656039Sbostic void
60756039Sbostic usage()
60856039Sbostic {
60956039Sbostic 	(void)fprintf(stderr,
61064453Sbostic 	    "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
61156039Sbostic 	exit(1);
61256039Sbostic }
61356039Sbostic 
61456039Sbostic #if __STDC__
61556039Sbostic #include <stdarg.h>
61656039Sbostic #else
61756039Sbostic #include <varargs.h>
61856039Sbostic #endif
61956039Sbostic 
62056039Sbostic void
62156039Sbostic #if __STDC__
62256039Sbostic err(const char *fmt, ...)
62356039Sbostic #else
62456039Sbostic err(fmt, va_alist)
62556039Sbostic 	char *fmt;
62656039Sbostic         va_dcl
62756039Sbostic #endif
62856039Sbostic {
62956039Sbostic 	va_list ap;
63056039Sbostic #if __STDC__
63156039Sbostic 	va_start(ap, fmt);
63256039Sbostic #else
63356039Sbostic 	va_start(ap);
63456039Sbostic #endif
63556039Sbostic 	(void)fprintf(stderr, "dbtest: ");
63656039Sbostic 	(void)vfprintf(stderr, fmt, ap);
63756039Sbostic 	va_end(ap);
63856039Sbostic 	(void)fprintf(stderr, "\n");
63956039Sbostic 	exit(1);
64056039Sbostic 	/* NOTREACHED */
64156039Sbostic }
642