xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 64367)
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*64367Sbostic static char sccsid[] = "@(#)dbtest.c	8.3 (Berkeley) 08/27/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;
6956039Sbostic 	int ch;
7059626Sbostic 	char *fname, *infoarg, *p, buf[8 * 1024];
7156039Sbostic 
7256039Sbostic 	infoarg = NULL;
7359626Sbostic 	fname = NULL;
7459626Sbostic 	while ((ch = getopt(argc, argv, "f:i:o:")) != EOF)
7556039Sbostic 		switch(ch) {
7659626Sbostic 		case 'f':
7759626Sbostic 			fname = optarg;
7859626Sbostic 			break;
7956039Sbostic 		case 'i':
8056039Sbostic 			infoarg = optarg;
8156039Sbostic 			break;
8256039Sbostic 		case 'o':
8356039Sbostic 			if ((ofd = open(optarg,
8456039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
8556039Sbostic 				err("%s: %s", optarg, strerror(errno));
8656039Sbostic 			break;
8756039Sbostic 		case '?':
8856039Sbostic 		default:
8956039Sbostic 			usage();
9056039Sbostic 		}
9156039Sbostic 	argc -= optind;
9256039Sbostic 	argv += optind;
9356039Sbostic 
9456039Sbostic 	if (argc != 2)
9556039Sbostic 		usage();
9656039Sbostic 
9756039Sbostic 	/* Set the type. */
9856039Sbostic 	type = dbtype(*argv++);
9956039Sbostic 
10056039Sbostic 	/* Open the descriptor file. */
10156039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
10256039Sbostic 		err("%s: %s", *argv, strerror(errno));
10356039Sbostic 
10456039Sbostic 	/* Set up the db structure as necessary. */
10556039Sbostic 	if (infoarg == NULL)
10656039Sbostic 		infop = NULL;
10756039Sbostic 	else
10857463Sbostic 		for (p = strtok(infoarg, ",\t "); p != NULL;
10957463Sbostic 		    p = strtok(0, ",\t "))
11056039Sbostic 			if (*p != '\0')
11156039Sbostic 				infop = setinfo(type, p);
11256039Sbostic 
11359626Sbostic 	/* Open the DB. */
11459626Sbostic 	if (fname == NULL) {
11560129Smargo 		p = getenv("TMPDIR");
11660129Smargo 		if (p == NULL)
11760129Smargo 			p = "/var/tmp";
11860129Smargo 		(void)sprintf(buf, "%s/__dbtest", p);
11960129Smargo 		fname = buf;
12060129Smargo 		(void)unlink(buf);
12159626Sbostic 	}
12259626Sbostic 	if ((dbp = dbopen(fname,
12356039Sbostic 	    O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
12456039Sbostic 		err("dbopen: %s", strerror(errno));
12557456Sbostic 	XXdbp = dbp;
12656039Sbostic 
12756039Sbostic 	state = COMMAND;
12856039Sbostic 	for (lineno = 1;
12956039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
13056039Sbostic 		len = strlen(buf);
13156039Sbostic 		switch(*p) {
13256557Sbostic 		case 'c':			/* compare */
13356557Sbostic 			if (state != COMMAND)
13456557Sbostic 				err("line %lu: not expecting command", lineno);
13556557Sbostic 			state = KEY;
13656557Sbostic 			command = COMPARE;
13756557Sbostic 			break;
13856059Sbostic 		case 'e':			/* echo */
13956059Sbostic 			if (state != COMMAND)
14056059Sbostic 				err("line %lu: not expecting command", lineno);
14156557Sbostic 			/* Don't display the newline, if CR at EOL. */
14256557Sbostic 			if (p[len - 2] == '\r')
14356557Sbostic 				--len;
14456557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
14556557Sbostic 				err("write: %s", strerror(errno));
14656059Sbostic 			break;
14756039Sbostic 		case 'g':			/* get */
14856039Sbostic 			if (state != COMMAND)
14956039Sbostic 				err("line %lu: not expecting command", lineno);
15056039Sbostic 			state = KEY;
15156039Sbostic 			command = GET;
15256039Sbostic 			break;
15356039Sbostic 		case 'p':			/* put */
15456039Sbostic 			if (state != COMMAND)
15556039Sbostic 				err("line %lu: not expecting command", lineno);
15656039Sbostic 			state = KEY;
15756039Sbostic 			command = PUT;
15856039Sbostic 			break;
15956039Sbostic 		case 'r':			/* remove */
16056039Sbostic 			if (state != COMMAND)
16156039Sbostic 				err("line %lu: not expecting command", lineno);
16256039Sbostic 			state = KEY;
16356039Sbostic 			command = REMOVE;
16456039Sbostic 			break;
16556039Sbostic 		case 's':			/* seq */
16656039Sbostic 			if (state != COMMAND)
16756039Sbostic 				err("line %lu: not expecting command", lineno);
16856059Sbostic 			if (flags == R_CURSOR) {
16956059Sbostic 				state = KEY;
17056059Sbostic 				command = SEQ;
17156059Sbostic 			} else
17256059Sbostic 				seq(dbp, &key);
17356039Sbostic 			break;
17456039Sbostic 		case 'f':
17556059Sbostic 			flags = setflags(p + 1);
17656039Sbostic 			break;
17756039Sbostic 		case 'D':			/* data file */
17856039Sbostic 			if (state != DATA)
17956039Sbostic 				err("line %lu: not expecting data", lineno);
18056039Sbostic 			data.data = rfile(p + 1, &data.size);
18156992Sbostic 			goto ldata;
18256039Sbostic 		case 'd':			/* data */
18356039Sbostic 			if (state != DATA)
18456039Sbostic 				err("line %lu: not expecting data", lineno);
18556557Sbostic 			data.data = xmalloc(p + 1, len - 1);
18656557Sbostic 			data.size = len - 1;
18756992Sbostic ldata:			switch(command) {
18856557Sbostic 			case COMPARE:
18956557Sbostic 				compare(&keydata, &data);
19056557Sbostic 				break;
19156557Sbostic 			case PUT:
19256557Sbostic 				put(dbp, &key, &data);
19356557Sbostic 				break;
19456557Sbostic 			default:
19556039Sbostic 				err("line %lu: command doesn't take data",
19656039Sbostic 				    lineno);
19756557Sbostic 			}
19859493Sbostic 			if (type != DB_RECNO)
19959493Sbostic 				free(key.data);
20056039Sbostic 			free(data.data);
20156039Sbostic 			state = COMMAND;
20256039Sbostic 			break;
20356039Sbostic 		case 'K':			/* key file */
20456039Sbostic 			if (state != KEY)
20556039Sbostic 				err("line %lu: not expecting a key", lineno);
20656039Sbostic 			if (type == DB_RECNO)
20756039Sbostic 				err("line %lu: 'K' not available for recno",
20856039Sbostic 				    lineno);
20956039Sbostic 			key.data = rfile(p + 1, &key.size);
21056992Sbostic 			goto lkey;
21156039Sbostic 		case 'k':			/* key */
21256039Sbostic 			if (state != KEY)
21356039Sbostic 				err("line %lu: not expecting a key", lineno);
21456039Sbostic 			if (type == DB_RECNO) {
21556039Sbostic 				static recno_t recno;
21656039Sbostic 				recno = strtol(p + 1, NULL, 0);
21756039Sbostic 				key.data = &recno;
21856039Sbostic 				key.size = sizeof(recno);
21956039Sbostic 			} else {
22056039Sbostic 				key.data = xmalloc(p + 1, len - 1);
22156039Sbostic 				key.size = len - 1;
22256039Sbostic 			}
22356992Sbostic lkey:			switch(command) {
22456557Sbostic 			case COMPARE:
22556557Sbostic 				getdata(dbp, &key, &keydata);
22656557Sbostic 				state = DATA;
22756557Sbostic 				break;
22856039Sbostic 			case GET:
22956039Sbostic 				get(dbp, &key);
23056039Sbostic 				if (type != DB_RECNO)
23156039Sbostic 					free(key.data);
23256039Sbostic 				state = COMMAND;
23356039Sbostic 				break;
23456039Sbostic 			case PUT:
23556039Sbostic 				state = DATA;
23656039Sbostic 				break;
23756039Sbostic 			case REMOVE:
23856039Sbostic 				rem(dbp, &key);
23956039Sbostic 				if (type != DB_RECNO)
24056039Sbostic 					free(key.data);
24156039Sbostic 				state = COMMAND;
24256039Sbostic 				break;
24356059Sbostic 			case SEQ:
24456059Sbostic 				seq(dbp, &key);
24556059Sbostic 				if (type != DB_RECNO)
24656059Sbostic 					free(key.data);
24756059Sbostic 				state = COMMAND;
24856059Sbostic 				break;
24956039Sbostic 			default:
25056039Sbostic 				err("line %lu: command doesn't take a key",
25156039Sbostic 				    lineno);
25256039Sbostic 			}
25356039Sbostic 			break;
25456992Sbostic 		case 'o':
25556992Sbostic 			dump(dbp, p[1] == 'r');
25656992Sbostic 			break;
25756039Sbostic 		default:
25856039Sbostic 			err("line %lu: %s: unknown command character",
25956059Sbostic 			    p, lineno);
26056039Sbostic 		}
26156039Sbostic 	}
26259626Sbostic 	if (dbp->close(dbp))
26359626Sbostic 		err("db->close: %s", strerror(errno));
26456039Sbostic 	(void)close(ofd);
26556039Sbostic 	exit(0);
26656039Sbostic }
26756039Sbostic 
26856059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
26956059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
27056059Sbostic 
27156039Sbostic void
27256557Sbostic compare(db1, db2)
27356557Sbostic 	DBT *db1, *db2;
27456557Sbostic {
27556557Sbostic 	register size_t len;
27656557Sbostic 	register u_char *p1, *p2;
27756557Sbostic 
27856557Sbostic 	if (db1->size != db2->size)
27956557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
28056557Sbostic 		    db1->size, db2->size);
28156557Sbostic 
28256557Sbostic 	len = MIN(db1->size, db2->size);
28356557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
28456557Sbostic 		if (*p1++ != *p2++) {
28556557Sbostic 			printf("compare failed at offset %d\n",
28656557Sbostic 			    p1 - (u_char *)db1->data);
28756557Sbostic 			break;
28856557Sbostic 		}
28956557Sbostic }
29056557Sbostic 
29156557Sbostic void
29256039Sbostic get(dbp, kp)
29356039Sbostic 	DB *dbp;
29456039Sbostic 	DBT *kp;
29556039Sbostic {
29656039Sbostic 	DBT data;
29756039Sbostic 
29856059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
29956059Sbostic 	case 0:
30056059Sbostic 		(void)write(ofd, data.data, data.size);
30156059Sbostic 		break;
30256059Sbostic 	case -1:
30356039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
30456059Sbostic 		/* NOTREACHED */
30556059Sbostic 	case 1:
30656059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
30757456Sbostic 		(void)fprintf(stderr, "%d: %.*s: %s\n",
30857456Sbostic 		    lineno, kp->size, kp->data, NOSUCHKEY);
30956059Sbostic 		break;
31056059Sbostic 	}
31156039Sbostic }
31256039Sbostic 
31356039Sbostic void
31456557Sbostic getdata(dbp, kp, dp)
31556557Sbostic 	DB *dbp;
31656557Sbostic 	DBT *kp, *dp;
31756557Sbostic {
31856557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
31956557Sbostic 	case 0:
32056557Sbostic 		return;
32156557Sbostic 	case -1:
32256557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
32356557Sbostic 		/* NOTREACHED */
32456557Sbostic 	case 1:
32556557Sbostic 		err("line %lu: get failed, no such key", lineno);
32656557Sbostic 		/* NOTREACHED */
32756557Sbostic 	}
32856557Sbostic }
32956557Sbostic 
33056557Sbostic void
33156039Sbostic put(dbp, kp, dp)
33256039Sbostic 	DB *dbp;
33356039Sbostic 	DBT *kp, *dp;
33456039Sbostic {
33556059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
33656059Sbostic 	case 0:
33756059Sbostic 		break;
33856059Sbostic 	case -1:
33956039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
34056059Sbostic 		/* NOTREACHED */
34156059Sbostic 	case 1:
34256059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
34356059Sbostic 		break;
34456059Sbostic 	}
34556039Sbostic }
34656039Sbostic 
34756039Sbostic void
34856039Sbostic rem(dbp, kp)
34956039Sbostic 	DB *dbp;
35056039Sbostic 	DBT *kp;
35156039Sbostic {
35256059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
35356059Sbostic 	case 0:
35456059Sbostic 		break;
35556059Sbostic 	case -1:
35656039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
35756059Sbostic 		/* NOTREACHED */
35856059Sbostic 	case 1:
35956059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
36056059Sbostic 		break;
36156059Sbostic 	}
36256039Sbostic }
36356039Sbostic 
36456039Sbostic void
36556059Sbostic seq(dbp, kp)
36656039Sbostic 	DB *dbp;
36756059Sbostic 	DBT *kp;
36856039Sbostic {
36956059Sbostic 	DBT data;
37056039Sbostic 
37156059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
37256059Sbostic 	case 0:
37356059Sbostic 		(void)write(ofd, data.data, data.size);
37456059Sbostic 		break;
37556059Sbostic 	case -1:
37656039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
37756059Sbostic 		/* NOTREACHED */
37856059Sbostic 	case 1:
37956059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
38056059Sbostic 		break;
38156059Sbostic 	}
38256039Sbostic }
38356992Sbostic 
38456992Sbostic void
38556992Sbostic dump(dbp, rev)
38656992Sbostic 	DB *dbp;
38756992Sbostic 	int rev;
38856992Sbostic {
38956992Sbostic 	DBT key, data;
39056992Sbostic 	int flags, nflags;
39156992Sbostic 
39256992Sbostic 	if (rev) {
39356992Sbostic 		flags = R_LAST;
39456992Sbostic 		nflags = R_PREV;
39556992Sbostic 	} else {
39656992Sbostic 		flags = R_FIRST;
39756992Sbostic 		nflags = R_NEXT;
39856992Sbostic 	}
39956992Sbostic 	for (;; flags = nflags)
40056992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
40156992Sbostic 		case 0:
40256992Sbostic 			(void)write(ofd, data.data, data.size);
40356992Sbostic 			break;
40456992Sbostic 		case 1:
40556992Sbostic 			goto done;
40656992Sbostic 		case -1:
40756992Sbostic 			err("line %lu: (dump) seq: %s",
40856992Sbostic 			    lineno, strerror(errno));
40956992Sbostic 			/* NOTREACHED */
41056992Sbostic 		}
41156992Sbostic done:	return;
41256992Sbostic }
41356039Sbostic 
41456039Sbostic u_int
41556039Sbostic setflags(s)
41656039Sbostic 	char *s;
41756039Sbostic {
41856039Sbostic 	char *p;
41956039Sbostic 
42056039Sbostic 	for (; isspace(*s); ++s);
42156039Sbostic 	if (*s == '\n')
42256039Sbostic 		return (0);
42356039Sbostic 	if ((p = index(s, '\n')) != NULL)
42456039Sbostic 		*p = '\0';
42556039Sbostic 	if (!strcmp(s, "R_CURSOR"))
42656039Sbostic 		return (R_CURSOR);
42756749Sbostic 	if (!strcmp(s, "R_FIRST"))
42856749Sbostic 		return (R_FIRST);
42956039Sbostic 	if (!strcmp(s, "R_IAFTER"))
43056039Sbostic 		return (R_IAFTER);
43156039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
43256039Sbostic 		return (R_IBEFORE);
43356039Sbostic 	if (!strcmp(s, "R_LAST"))
43456039Sbostic 		return (R_LAST);
43556039Sbostic 	if (!strcmp(s, "R_NEXT"))
43656039Sbostic 		return (R_NEXT);
43756749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
43856749Sbostic 		return (R_NOOVERWRITE);
43956039Sbostic 	if (!strcmp(s, "R_PREV"))
44056039Sbostic 		return (R_PREV);
44156749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
44256749Sbostic 		return (R_SETCURSOR);
44356039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
44456039Sbostic 	/* NOTREACHED */
44556039Sbostic }
44656039Sbostic 
44756039Sbostic DBTYPE
44856039Sbostic dbtype(s)
44956039Sbostic 	char *s;
45056039Sbostic {
45156039Sbostic 	if (!strcmp(s, "btree"))
45256039Sbostic 		return (DB_BTREE);
45356039Sbostic 	if (!strcmp(s, "hash"))
45456039Sbostic 		return (DB_HASH);
45556039Sbostic 	if (!strcmp(s, "recno"))
45656039Sbostic 		return (DB_RECNO);
45756039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
45856039Sbostic 	/* NOTREACHED */
45956039Sbostic }
46056039Sbostic 
46156039Sbostic void *
46256039Sbostic setinfo(type, s)
46356039Sbostic 	DBTYPE type;
46456039Sbostic 	char *s;
46556039Sbostic {
46656039Sbostic 	static BTREEINFO ib;
46756039Sbostic 	static HASHINFO ih;
46856039Sbostic 	static RECNOINFO rh;
46956039Sbostic 	char *eq;
47056039Sbostic 
47156039Sbostic 	if ((eq = index(s, '=')) == NULL)
47256039Sbostic 		err("%s: illegal structure set statement", s);
47356039Sbostic 	*eq++ = '\0';
47456039Sbostic 	if (!isdigit(*eq))
47556039Sbostic 		err("%s: structure set statement must be a number", s);
47656039Sbostic 
47756039Sbostic 	switch(type) {
47856039Sbostic 	case DB_BTREE:
47956039Sbostic 		if (!strcmp("flags", s)) {
48064288Sbostic 			ib.flags = atoi(eq);
48156039Sbostic 			return (&ib);
48256039Sbostic 		}
48356039Sbostic 		if (!strcmp("cachesize", s)) {
48464288Sbostic 			ib.cachesize = atoi(eq);
48556039Sbostic 			return (&ib);
48656039Sbostic 		}
48756039Sbostic 		if (!strcmp("maxkeypage", s)) {
48864288Sbostic 			ib.maxkeypage = atoi(eq);
48956039Sbostic 			return (&ib);
49056039Sbostic 		}
49156039Sbostic 		if (!strcmp("minkeypage", s)) {
49264288Sbostic 			ib.minkeypage = atoi(eq);
49356039Sbostic 			return (&ib);
49456039Sbostic 		}
49556039Sbostic 		if (!strcmp("lorder", s)) {
49664288Sbostic 			ib.lorder = atoi(eq);
49756039Sbostic 			return (&ib);
49856039Sbostic 		}
49957456Sbostic 		if (!strcmp("psize", s)) {
50064288Sbostic 			ib.psize = atoi(eq);
50157456Sbostic 			return (&ib);
50257456Sbostic 		}
50356039Sbostic 		break;
50456039Sbostic 	case DB_HASH:
50556039Sbostic 		if (!strcmp("bsize", s)) {
50664288Sbostic 			ih.bsize = atoi(eq);
50760130Sbostic 			return (&ih);
50856039Sbostic 		}
50956039Sbostic 		if (!strcmp("ffactor", s)) {
51064288Sbostic 			ih.ffactor = atoi(eq);
51160130Sbostic 			return (&ih);
51256039Sbostic 		}
51356039Sbostic 		if (!strcmp("nelem", s)) {
51464288Sbostic 			ih.nelem = atoi(eq);
51560130Sbostic 			return (&ih);
51656039Sbostic 		}
51756039Sbostic 		if (!strcmp("cachesize", s)) {
51864288Sbostic 			ih.cachesize = atoi(eq);
51960130Sbostic 			return (&ih);
52056039Sbostic 		}
52156039Sbostic 		if (!strcmp("lorder", s)) {
52264288Sbostic 			ih.lorder = atoi(eq);
52360130Sbostic 			return (&ih);
52456039Sbostic 		}
52556039Sbostic 		break;
52656039Sbostic 	case DB_RECNO:
52756039Sbostic 		if (!strcmp("flags", s)) {
52864288Sbostic 			rh.flags = atoi(eq);
52960130Sbostic 			return (&rh);
53056039Sbostic 		}
53156039Sbostic 		if (!strcmp("cachesize", s)) {
53264288Sbostic 			rh.cachesize = atoi(eq);
53360130Sbostic 			return (&rh);
53456039Sbostic 		}
53556039Sbostic 		if (!strcmp("lorder", s)) {
53664288Sbostic 			rh.lorder = atoi(eq);
53760130Sbostic 			return (&rh);
53856039Sbostic 		}
53956039Sbostic 		if (!strcmp("reclen", s)) {
54064288Sbostic 			rh.reclen = atoi(eq);
54160130Sbostic 			return (&rh);
54256039Sbostic 		}
54356039Sbostic 		if (!strcmp("bval", s)) {
54464288Sbostic 			rh.bval = atoi(eq);
54560130Sbostic 			return (&rh);
54656039Sbostic 		}
54760229Sbostic 		if (!strcmp("psize", s)) {
54864288Sbostic 			rh.psize = atoi(eq);
54960229Sbostic 			return (&rh);
55060229Sbostic 		}
55156039Sbostic 		break;
55256039Sbostic 	}
55356039Sbostic 	err("%s: unknown structure value", s);
55456039Sbostic 	/* NOTREACHED */
55556039Sbostic }
55656039Sbostic 
55756039Sbostic void *
55856039Sbostic rfile(name, lenp)
55956039Sbostic 	char *name;
56056039Sbostic 	size_t *lenp;
56156039Sbostic {
56256039Sbostic 	struct stat sb;
56356039Sbostic 	void *p;
56456039Sbostic 	int fd;
56556039Sbostic 	char *np;
56656039Sbostic 
56756039Sbostic 	for (; isspace(*name); ++name);
56856039Sbostic 	if ((np = index(name, '\n')) != NULL)
56956039Sbostic 		*np = '\0';
57056039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
57156039Sbostic 	    fstat(fd, &sb))
57256039Sbostic 		err("%s: %s\n", name, strerror(errno));
573*64367Sbostic #ifdef NOT_PORTABLE
57458312Sbostic 	if (sb.st_size > (off_t)SIZE_T_MAX)
57556039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
576*64367Sbostic #endif
57756039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
57856039Sbostic 		err("%s", strerror(errno));
57956039Sbostic 	(void)read(fd, p, (int)sb.st_size);
58056039Sbostic 	*lenp = sb.st_size;
58156489Sbostic 	(void)close(fd);
58256039Sbostic 	return (p);
58356039Sbostic }
58456039Sbostic 
58556039Sbostic void *
58656039Sbostic xmalloc(text, len)
58756039Sbostic 	char *text;
58856039Sbostic 	size_t len;
58956039Sbostic {
59056039Sbostic 	void *p;
59156039Sbostic 
59256039Sbostic 	if ((p = malloc(len)) == NULL)
59356039Sbostic 		err("%s", strerror(errno));
59458015Sbostic 	memmove(p, text, len);
59556039Sbostic 	return (p);
59656039Sbostic }
59756039Sbostic 
59856039Sbostic void
59956039Sbostic usage()
60056039Sbostic {
60156039Sbostic 	(void)fprintf(stderr,
60259626Sbostic 	    "usage: dbtest [-f file] [-i info] [-o file] type script\n");
60356039Sbostic 	exit(1);
60456039Sbostic }
60556039Sbostic 
60656039Sbostic #if __STDC__
60756039Sbostic #include <stdarg.h>
60856039Sbostic #else
60956039Sbostic #include <varargs.h>
61056039Sbostic #endif
61156039Sbostic 
61256039Sbostic void
61356039Sbostic #if __STDC__
61456039Sbostic err(const char *fmt, ...)
61556039Sbostic #else
61656039Sbostic err(fmt, va_alist)
61756039Sbostic 	char *fmt;
61856039Sbostic         va_dcl
61956039Sbostic #endif
62056039Sbostic {
62156039Sbostic 	va_list ap;
62256039Sbostic #if __STDC__
62356039Sbostic 	va_start(ap, fmt);
62456039Sbostic #else
62556039Sbostic 	va_start(ap);
62656039Sbostic #endif
62756039Sbostic 	(void)fprintf(stderr, "dbtest: ");
62856039Sbostic 	(void)vfprintf(stderr, fmt, ap);
62956039Sbostic 	va_end(ap);
63056039Sbostic 	(void)fprintf(stderr, "\n");
63156039Sbostic 	exit(1);
63256039Sbostic 	/* NOTREACHED */
63356039Sbostic }
634