xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 57456)
156039Sbostic /*-
256039Sbostic  * Copyright (c) 1992 The Regents of the University of California.
356039Sbostic  * All rights reserved.
456039Sbostic  *
556039Sbostic  * %sccs.include.redist.c%
656039Sbostic  */
756039Sbostic 
856039Sbostic #ifndef lint
956039Sbostic char copyright[] =
1056039Sbostic "@(#) Copyright (c) 1992 The Regents of the University of California.\n\
1156039Sbostic  All rights reserved.\n";
1256039Sbostic #endif /* not lint */
1356039Sbostic 
1456039Sbostic #ifndef lint
15*57456Sbostic static char sccsid[] = "@(#)dbtest.c	5.7 (Berkeley) 01/10/93";
1656039Sbostic #endif /* not lint */
1756039Sbostic 
1856039Sbostic #include <sys/param.h>
1956039Sbostic #include <sys/stat.h>
2056039Sbostic 
2156039Sbostic #include <ctype.h>
2256039Sbostic #include <db.h>
2356039Sbostic #include <errno.h>
2456039Sbostic #include <fcntl.h>
2556039Sbostic #include <limits.h>
2656039Sbostic #include <stdio.h>
2756039Sbostic #include <stdlib.h>
2856039Sbostic #include <string.h>
2956039Sbostic #include <unistd.h>
3056039Sbostic 
3156557Sbostic enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
3256039Sbostic 
3356557Sbostic void	 compare __P((DBT *, DBT *));
3456039Sbostic DBTYPE	 dbtype __P((char *));
3556992Sbostic void	 dump __P((DB *, int));
3656039Sbostic void	 err __P((const char *, ...));
3756039Sbostic void	 get __P((DB *, DBT *));
3856557Sbostic void	 getdata __P((DB *, DBT *, DBT *));
3956039Sbostic void	 put __P((DB *, DBT *, DBT *));
4056039Sbostic void	 rem __P((DB *, DBT *));
4156039Sbostic void	*rfile __P((char *, size_t *));
4256059Sbostic void	 seq __P((DB *, DBT *));
4356039Sbostic u_int	 setflags __P((char *));
4456039Sbostic void	*setinfo __P((DBTYPE, char *));
4556039Sbostic void	 usage __P((void));
4656039Sbostic void	*xmalloc __P((char *, size_t));
4756039Sbostic 
4856039Sbostic DBTYPE type;
4956039Sbostic void *infop;
5056039Sbostic u_long lineno;
5156039Sbostic u_int flags;
5256039Sbostic int ofd = STDOUT_FILENO;
5356039Sbostic 
54*57456Sbostic DB *XXdbp;				/* Global for gdb. */
55*57456Sbostic 
5656039Sbostic int
5756039Sbostic main(argc, argv)
5856039Sbostic 	int argc;
5956039Sbostic 	char *argv[];
6056039Sbostic {
6156039Sbostic 	enum S command, state;
6256039Sbostic 	DB *dbp;
6356557Sbostic 	DBT data, key, keydata;
6456039Sbostic 	size_t len;
6556039Sbostic 	int ch;
6656039Sbostic 	char *infoarg, *p, buf[8 * 1024];
6756039Sbostic 
6856039Sbostic 	infoarg = NULL;
6956039Sbostic 	while ((ch = getopt(argc, argv, "i:o:")) != EOF)
7056039Sbostic 		switch(ch) {
7156039Sbostic 		case 'i':
7256039Sbostic 			infoarg = optarg;
7356039Sbostic 			break;
7456039Sbostic 		case 'o':
7556039Sbostic 			if ((ofd = open(optarg,
7656039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
7756039Sbostic 				err("%s: %s", optarg, strerror(errno));
7856039Sbostic 			break;
7956039Sbostic 		case '?':
8056039Sbostic 		default:
8156039Sbostic 			usage();
8256039Sbostic 		}
8356039Sbostic 	argc -= optind;
8456039Sbostic 	argv += optind;
8556039Sbostic 
8656039Sbostic 	if (argc != 2)
8756039Sbostic 		usage();
8856039Sbostic 
8956039Sbostic 	/* Set the type. */
9056039Sbostic 	type = dbtype(*argv++);
9156039Sbostic 
9256039Sbostic 	/* Open the descriptor file. */
9356039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
9456039Sbostic 		err("%s: %s", *argv, strerror(errno));
9556039Sbostic 
9656039Sbostic 	/* Set up the db structure as necessary. */
9756039Sbostic 	if (infoarg == NULL)
9856039Sbostic 		infop = NULL;
9956039Sbostic 	else
10056039Sbostic 		while ((p = strsep(&infoarg, ",\t ")) != NULL)
10156039Sbostic 			if (*p != '\0')
10256039Sbostic 				infop = setinfo(type, p);
10356039Sbostic 
10456039Sbostic #define	BACKINGFILE	"/tmp/__dbtest"
10556039Sbostic 	/* Open the DB. */
10656039Sbostic 	(void)unlink(BACKINGFILE);
10756039Sbostic 	if ((dbp = dbopen(BACKINGFILE,
10856039Sbostic 	    O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
10956039Sbostic 		err("dbopen: %s", strerror(errno));
110*57456Sbostic 	XXdbp = dbp;
11156039Sbostic 
11256039Sbostic 	state = COMMAND;
11356039Sbostic 	for (lineno = 1;
11456039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
11556039Sbostic 		len = strlen(buf);
11656039Sbostic 		switch(*p) {
11756557Sbostic 		case 'c':			/* compare */
11856557Sbostic 			if (state != COMMAND)
11956557Sbostic 				err("line %lu: not expecting command", lineno);
12056557Sbostic 			state = KEY;
12156557Sbostic 			command = COMPARE;
12256557Sbostic 			break;
12356059Sbostic 		case 'e':			/* echo */
12456059Sbostic 			if (state != COMMAND)
12556059Sbostic 				err("line %lu: not expecting command", lineno);
12656557Sbostic 			/* Don't display the newline, if CR at EOL. */
12756557Sbostic 			if (p[len - 2] == '\r')
12856557Sbostic 				--len;
12956557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
13056557Sbostic 				err("write: %s", strerror(errno));
13156059Sbostic 			break;
13256039Sbostic 		case 'g':			/* get */
13356039Sbostic 			if (state != COMMAND)
13456039Sbostic 				err("line %lu: not expecting command", lineno);
13556039Sbostic 			state = KEY;
13656039Sbostic 			command = GET;
13756039Sbostic 			break;
13856039Sbostic 		case 'p':			/* put */
13956039Sbostic 			if (state != COMMAND)
14056039Sbostic 				err("line %lu: not expecting command", lineno);
14156039Sbostic 			state = KEY;
14256039Sbostic 			command = PUT;
14356039Sbostic 			break;
14456039Sbostic 		case 'r':			/* remove */
14556039Sbostic 			if (state != COMMAND)
14656039Sbostic 				err("line %lu: not expecting command", lineno);
14756039Sbostic 			state = KEY;
14856039Sbostic 			command = REMOVE;
14956039Sbostic 			break;
15056039Sbostic 		case 's':			/* seq */
15156039Sbostic 			if (state != COMMAND)
15256039Sbostic 				err("line %lu: not expecting command", lineno);
15356059Sbostic 			if (flags == R_CURSOR) {
15456059Sbostic 				state = KEY;
15556059Sbostic 				command = SEQ;
15656059Sbostic 			} else
15756059Sbostic 				seq(dbp, &key);
15856039Sbostic 			break;
15956039Sbostic 		case 'f':
16056059Sbostic 			flags = setflags(p + 1);
16156039Sbostic 			break;
16256039Sbostic 		case 'D':			/* data file */
16356039Sbostic 			if (state != DATA)
16456039Sbostic 				err("line %lu: not expecting data", lineno);
16556039Sbostic 			data.data = rfile(p + 1, &data.size);
16656992Sbostic 			goto ldata;
16756039Sbostic 		case 'd':			/* data */
16856039Sbostic 			if (state != DATA)
16956039Sbostic 				err("line %lu: not expecting data", lineno);
17056557Sbostic 			data.data = xmalloc(p + 1, len - 1);
17156557Sbostic 			data.size = len - 1;
17256992Sbostic ldata:			switch(command) {
17356557Sbostic 			case COMPARE:
17456557Sbostic 				compare(&keydata, &data);
17556557Sbostic 				break;
17656557Sbostic 			case PUT:
17756557Sbostic 				put(dbp, &key, &data);
17856557Sbostic 				break;
17956557Sbostic 			default:
18056039Sbostic 				err("line %lu: command doesn't take data",
18156039Sbostic 				    lineno);
18256557Sbostic 			}
18356039Sbostic 			free(key.data);
18456039Sbostic 			free(data.data);
18556039Sbostic 			state = COMMAND;
18656039Sbostic 			break;
18756039Sbostic 		case 'K':			/* key file */
18856039Sbostic 			if (state != KEY)
18956039Sbostic 				err("line %lu: not expecting a key", lineno);
19056039Sbostic 			if (type == DB_RECNO)
19156039Sbostic 				err("line %lu: 'K' not available for recno",
19256039Sbostic 				    lineno);
19356039Sbostic 			key.data = rfile(p + 1, &key.size);
19456992Sbostic 			goto lkey;
19556039Sbostic 		case 'k':			/* key */
19656039Sbostic 			if (state != KEY)
19756039Sbostic 				err("line %lu: not expecting a key", lineno);
19856039Sbostic 			if (type == DB_RECNO) {
19956039Sbostic 				static recno_t recno;
20056039Sbostic 				recno = strtol(p + 1, NULL, 0);
20156039Sbostic 				key.data = &recno;
20256039Sbostic 				key.size = sizeof(recno);
20356039Sbostic 			} else {
20456039Sbostic 				key.data = xmalloc(p + 1, len - 1);
20556039Sbostic 				key.size = len - 1;
20656039Sbostic 			}
20756992Sbostic lkey:			switch(command) {
20856557Sbostic 			case COMPARE:
20956557Sbostic 				getdata(dbp, &key, &keydata);
21056557Sbostic 				state = DATA;
21156557Sbostic 				break;
21256039Sbostic 			case GET:
21356039Sbostic 				get(dbp, &key);
21456039Sbostic 				if (type != DB_RECNO)
21556039Sbostic 					free(key.data);
21656039Sbostic 				state = COMMAND;
21756039Sbostic 				break;
21856039Sbostic 			case PUT:
21956039Sbostic 				state = DATA;
22056039Sbostic 				break;
22156039Sbostic 			case REMOVE:
22256039Sbostic 				rem(dbp, &key);
22356039Sbostic 				if (type != DB_RECNO)
22456039Sbostic 					free(key.data);
22556039Sbostic 				state = COMMAND;
22656039Sbostic 				break;
22756059Sbostic 			case SEQ:
22856059Sbostic 				seq(dbp, &key);
22956059Sbostic 				if (type != DB_RECNO)
23056059Sbostic 					free(key.data);
23156059Sbostic 				state = COMMAND;
23256059Sbostic 				break;
23356039Sbostic 			default:
23456039Sbostic 				err("line %lu: command doesn't take a key",
23556039Sbostic 				    lineno);
23656039Sbostic 			}
23756039Sbostic 			break;
23856992Sbostic 		case 'o':
23956992Sbostic 			dump(dbp, p[1] == 'r');
24056992Sbostic 			break;
24156039Sbostic 		default:
24256039Sbostic 			err("line %lu: %s: unknown command character",
24356059Sbostic 			    p, lineno);
24456039Sbostic 		}
24556039Sbostic 	}
24656039Sbostic 	(void)close(ofd);
24756039Sbostic 	exit(0);
24856039Sbostic }
24956039Sbostic 
25056059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
25156059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
25256059Sbostic 
25356039Sbostic void
25456557Sbostic compare(db1, db2)
25556557Sbostic 	DBT *db1, *db2;
25656557Sbostic {
25756557Sbostic 	register size_t len;
25856557Sbostic 	register u_char *p1, *p2;
25956557Sbostic 
26056557Sbostic 	if (db1->size != db2->size)
26156557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
26256557Sbostic 		    db1->size, db2->size);
26356557Sbostic 
26456557Sbostic 	len = MIN(db1->size, db2->size);
26556557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
26656557Sbostic 		if (*p1++ != *p2++) {
26756557Sbostic 			printf("compare failed at offset %d\n",
26856557Sbostic 			    p1 - (u_char *)db1->data);
26956557Sbostic 			break;
27056557Sbostic 		}
27156557Sbostic }
27256557Sbostic 
27356557Sbostic void
27456039Sbostic get(dbp, kp)
27556039Sbostic 	DB *dbp;
27656039Sbostic 	DBT *kp;
27756039Sbostic {
27856039Sbostic 	DBT data;
27956039Sbostic 
28056059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
28156059Sbostic 	case 0:
28256059Sbostic 		(void)write(ofd, data.data, data.size);
28356059Sbostic 		break;
28456059Sbostic 	case -1:
28556039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
28656059Sbostic 		/* NOTREACHED */
28756059Sbostic 	case 1:
28856059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
289*57456Sbostic 		(void)fprintf(stderr, "%d: %.*s: %s\n",
290*57456Sbostic 		    lineno, kp->size, kp->data, NOSUCHKEY);
29156059Sbostic 		break;
29256059Sbostic 	}
29356039Sbostic }
29456039Sbostic 
29556039Sbostic void
29656557Sbostic getdata(dbp, kp, dp)
29756557Sbostic 	DB *dbp;
29856557Sbostic 	DBT *kp, *dp;
29956557Sbostic {
30056557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
30156557Sbostic 	case 0:
30256557Sbostic 		return;
30356557Sbostic 	case -1:
30456557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
30556557Sbostic 		/* NOTREACHED */
30656557Sbostic 	case 1:
30756557Sbostic 		err("line %lu: get failed, no such key", lineno);
30856557Sbostic 		/* NOTREACHED */
30956557Sbostic 	}
31056557Sbostic }
31156557Sbostic 
31256557Sbostic void
31356039Sbostic put(dbp, kp, dp)
31456039Sbostic 	DB *dbp;
31556039Sbostic 	DBT *kp, *dp;
31656039Sbostic {
31756059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
31856059Sbostic 	case 0:
31956059Sbostic 		break;
32056059Sbostic 	case -1:
32156039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
32256059Sbostic 		/* NOTREACHED */
32356059Sbostic 	case 1:
32456059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
32556059Sbostic 		break;
32656059Sbostic 	}
32756039Sbostic }
32856039Sbostic 
32956039Sbostic void
33056039Sbostic rem(dbp, kp)
33156039Sbostic 	DB *dbp;
33256039Sbostic 	DBT *kp;
33356039Sbostic {
33456059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
33556059Sbostic 	case 0:
33656059Sbostic 		break;
33756059Sbostic 	case -1:
33856039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
33956059Sbostic 		/* NOTREACHED */
34056059Sbostic 	case 1:
34156059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
34256059Sbostic 		break;
34356059Sbostic 	}
34456039Sbostic }
34556039Sbostic 
34656039Sbostic void
34756059Sbostic seq(dbp, kp)
34856039Sbostic 	DB *dbp;
34956059Sbostic 	DBT *kp;
35056039Sbostic {
35156059Sbostic 	DBT data;
35256039Sbostic 
35356059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
35456059Sbostic 	case 0:
35556059Sbostic 		(void)write(ofd, data.data, data.size);
35656059Sbostic 		break;
35756059Sbostic 	case -1:
35856039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
35956059Sbostic 		/* NOTREACHED */
36056059Sbostic 	case 1:
36156059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
36256059Sbostic 		break;
36356059Sbostic 	}
36456039Sbostic }
36556992Sbostic 
36656992Sbostic void
36756992Sbostic dump(dbp, rev)
36856992Sbostic 	DB *dbp;
36956992Sbostic 	int rev;
37056992Sbostic {
37156992Sbostic 	DBT key, data;
37256992Sbostic 	int flags, nflags;
37356992Sbostic 
37456992Sbostic 	if (rev) {
37556992Sbostic 		flags = R_LAST;
37656992Sbostic 		nflags = R_PREV;
37756992Sbostic 	} else {
37856992Sbostic 		flags = R_FIRST;
37956992Sbostic 		nflags = R_NEXT;
38056992Sbostic 	}
38156992Sbostic 	for (;; flags = nflags)
38256992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
38356992Sbostic 		case 0:
38456992Sbostic 			(void)write(ofd, data.data, data.size);
38556992Sbostic 			break;
38656992Sbostic 		case 1:
38756992Sbostic 			goto done;
38856992Sbostic 		case -1:
38956992Sbostic 			err("line %lu: (dump) seq: %s",
39056992Sbostic 			    lineno, strerror(errno));
39156992Sbostic 			/* NOTREACHED */
39256992Sbostic 		}
39356992Sbostic done:	return;
39456992Sbostic }
39556039Sbostic 
39656039Sbostic u_int
39756039Sbostic setflags(s)
39856039Sbostic 	char *s;
39956039Sbostic {
40056039Sbostic 	char *p;
40156039Sbostic 
40256039Sbostic 	for (; isspace(*s); ++s);
40356039Sbostic 	if (*s == '\n')
40456039Sbostic 		return (0);
40556039Sbostic 	if ((p = index(s, '\n')) != NULL)
40656039Sbostic 		*p = '\0';
40756039Sbostic 	if (!strcmp(s, "R_CURSOR"))
40856039Sbostic 		return (R_CURSOR);
40956749Sbostic 	if (!strcmp(s, "R_CURSORLOG"))
41056749Sbostic 		return (R_CURSORLOG);
41156749Sbostic 	if (!strcmp(s, "R_FIRST"))
41256749Sbostic 		return (R_FIRST);
41356039Sbostic 	if (!strcmp(s, "R_IAFTER"))
41456039Sbostic 		return (R_IAFTER);
41556039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
41656039Sbostic 		return (R_IBEFORE);
41756039Sbostic 	if (!strcmp(s, "R_LAST"))
41856039Sbostic 		return (R_LAST);
41956039Sbostic 	if (!strcmp(s, "R_NEXT"))
42056039Sbostic 		return (R_NEXT);
42156749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
42256749Sbostic 		return (R_NOOVERWRITE);
42356039Sbostic 	if (!strcmp(s, "R_PREV"))
42456039Sbostic 		return (R_PREV);
42556749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
42656749Sbostic 		return (R_SETCURSOR);
42756039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
42856039Sbostic 	/* NOTREACHED */
42956039Sbostic }
43056039Sbostic 
43156039Sbostic DBTYPE
43256039Sbostic dbtype(s)
43356039Sbostic 	char *s;
43456039Sbostic {
43556039Sbostic 	if (!strcmp(s, "btree"))
43656039Sbostic 		return (DB_BTREE);
43756039Sbostic 	if (!strcmp(s, "hash"))
43856039Sbostic 		return (DB_HASH);
43956039Sbostic 	if (!strcmp(s, "recno"))
44056039Sbostic 		return (DB_RECNO);
44156039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
44256039Sbostic 	/* NOTREACHED */
44356039Sbostic }
44456039Sbostic 
44556039Sbostic void *
44656039Sbostic setinfo(type, s)
44756039Sbostic 	DBTYPE type;
44856039Sbostic 	char *s;
44956039Sbostic {
45056039Sbostic 	static BTREEINFO ib;
45156039Sbostic 	static HASHINFO ih;
45256039Sbostic 	static RECNOINFO rh;
45356039Sbostic 	char *eq;
45456039Sbostic 
45556039Sbostic 	if ((eq = index(s, '=')) == NULL)
45656039Sbostic 		err("%s: illegal structure set statement", s);
45756039Sbostic 	*eq++ = '\0';
45856039Sbostic 	if (!isdigit(*eq))
45956039Sbostic 		err("%s: structure set statement must be a number", s);
46056039Sbostic 
46156039Sbostic 	switch(type) {
46256039Sbostic 	case DB_BTREE:
46356039Sbostic 		if (!strcmp("flags", s)) {
46456039Sbostic 			ib.flags = strtoul(eq, NULL, 0);
46556039Sbostic 			return (&ib);
46656039Sbostic 		}
46756039Sbostic 		if (!strcmp("cachesize", s)) {
46856039Sbostic 			ib.cachesize = strtoul(eq, NULL, 0);
46956039Sbostic 			return (&ib);
47056039Sbostic 		}
47156039Sbostic 		if (!strcmp("maxkeypage", s)) {
47256039Sbostic 			ib.maxkeypage = strtoul(eq, NULL, 0);
47356039Sbostic 			return (&ib);
47456039Sbostic 		}
47556039Sbostic 		if (!strcmp("minkeypage", s)) {
47656039Sbostic 			ib.minkeypage = strtoul(eq, NULL, 0);
47756039Sbostic 			return (&ib);
47856039Sbostic 		}
47956039Sbostic 		if (!strcmp("lorder", s)) {
48056039Sbostic 			ib.lorder = strtoul(eq, NULL, 0);
48156039Sbostic 			return (&ib);
48256039Sbostic 		}
483*57456Sbostic 		if (!strcmp("psize", s)) {
484*57456Sbostic 			ib.psize = strtoul(eq, NULL, 0);
485*57456Sbostic 			return (&ib);
486*57456Sbostic 		}
48756039Sbostic 		break;
48856039Sbostic 	case DB_HASH:
48956039Sbostic 		if (!strcmp("bsize", s)) {
49056039Sbostic 			ih.bsize = strtoul(eq, NULL, 0);
49156039Sbostic 			return (&ib);
49256039Sbostic 		}
49356039Sbostic 		if (!strcmp("ffactor", s)) {
49456039Sbostic 			ih.ffactor = strtoul(eq, NULL, 0);
49556039Sbostic 			return (&ib);
49656039Sbostic 		}
49756039Sbostic 		if (!strcmp("nelem", s)) {
49856039Sbostic 			ih.nelem = strtoul(eq, NULL, 0);
49956039Sbostic 			return (&ib);
50056039Sbostic 		}
50156039Sbostic 		if (!strcmp("cachesize", s)) {
50256039Sbostic 			ih.cachesize = strtoul(eq, NULL, 0);
50356039Sbostic 			return (&ib);
50456039Sbostic 		}
50556039Sbostic 		if (!strcmp("lorder", s)) {
50656039Sbostic 			ih.lorder = strtoul(eq, NULL, 0);
50756039Sbostic 			return (&ib);
50856039Sbostic 		}
50956039Sbostic 		break;
51056039Sbostic 	case DB_RECNO:
51156039Sbostic 		if (!strcmp("flags", s)) {
51256039Sbostic 			rh.flags = strtoul(eq, NULL, 0);
51356039Sbostic 			return (&ib);
51456039Sbostic 		}
51556039Sbostic 		if (!strcmp("cachesize", s)) {
51656039Sbostic 			rh.cachesize = strtoul(eq, NULL, 0);
51756039Sbostic 			return (&ib);
51856039Sbostic 		}
51956039Sbostic 		if (!strcmp("lorder", s)) {
52056039Sbostic 			rh.lorder = strtoul(eq, NULL, 0);
52156039Sbostic 			return (&ib);
52256039Sbostic 		}
52356039Sbostic 		if (!strcmp("reclen", s)) {
52456039Sbostic 			rh.reclen = strtoul(eq, NULL, 0);
52556039Sbostic 			return (&ib);
52656039Sbostic 		}
52756039Sbostic 		if (!strcmp("bval", s)) {
52856039Sbostic 			rh.bval = strtoul(eq, NULL, 0);
52956039Sbostic 			return (&ib);
53056039Sbostic 		}
53156039Sbostic 		break;
53256039Sbostic 	}
53356039Sbostic 	err("%s: unknown structure value", s);
53456039Sbostic 	/* NOTREACHED */
53556039Sbostic }
53656039Sbostic 
53756039Sbostic void *
53856039Sbostic rfile(name, lenp)
53956039Sbostic 	char *name;
54056039Sbostic 	size_t *lenp;
54156039Sbostic {
54256039Sbostic 	struct stat sb;
54356039Sbostic 	void *p;
54456039Sbostic 	int fd;
54556039Sbostic 	char *np;
54656039Sbostic 
54756039Sbostic 	for (; isspace(*name); ++name);
54856039Sbostic 	if ((np = index(name, '\n')) != NULL)
54956039Sbostic 		*np = '\0';
55056039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
55156039Sbostic 	    fstat(fd, &sb))
55256039Sbostic 		err("%s: %s\n", name, strerror(errno));
55356039Sbostic 	if (sb.st_size > SIZE_T_MAX)
55456039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
55556039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
55656039Sbostic 		err("%s", strerror(errno));
55756039Sbostic 	(void)read(fd, p, (int)sb.st_size);
55856039Sbostic 	*lenp = sb.st_size;
55956489Sbostic 	(void)close(fd);
56056039Sbostic 	return (p);
56156039Sbostic }
56256039Sbostic 
56356039Sbostic void *
56456039Sbostic xmalloc(text, len)
56556039Sbostic 	char *text;
56656039Sbostic 	size_t len;
56756039Sbostic {
56856039Sbostic 	void *p;
56956039Sbostic 
57056039Sbostic 	if ((p = malloc(len)) == NULL)
57156039Sbostic 		err("%s", strerror(errno));
57256039Sbostic 	bcopy(text, p, len);
57356039Sbostic 	return (p);
57456039Sbostic }
57556039Sbostic 
57656039Sbostic void
57756039Sbostic usage()
57856039Sbostic {
57956039Sbostic 	(void)fprintf(stderr,
58056039Sbostic 	    "usage: dbtest [-i info] [-o file] type script\n");
58156039Sbostic 	exit(1);
58256039Sbostic }
58356039Sbostic 
58456039Sbostic #if __STDC__
58556039Sbostic #include <stdarg.h>
58656039Sbostic #else
58756039Sbostic #include <varargs.h>
58856039Sbostic #endif
58956039Sbostic 
59056039Sbostic void
59156039Sbostic #if __STDC__
59256039Sbostic err(const char *fmt, ...)
59356039Sbostic #else
59456039Sbostic err(fmt, va_alist)
59556039Sbostic 	char *fmt;
59656039Sbostic         va_dcl
59756039Sbostic #endif
59856039Sbostic {
59956039Sbostic 	va_list ap;
60056039Sbostic #if __STDC__
60156039Sbostic 	va_start(ap, fmt);
60256039Sbostic #else
60356039Sbostic 	va_start(ap);
60456039Sbostic #endif
60556039Sbostic 	(void)fprintf(stderr, "dbtest: ");
60656039Sbostic 	(void)vfprintf(stderr, fmt, ap);
60756039Sbostic 	va_end(ap);
60856039Sbostic 	(void)fprintf(stderr, "\n");
60956039Sbostic 	exit(1);
61056039Sbostic 	/* NOTREACHED */
61156039Sbostic }
612