xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 59626)
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*59626Sbostic static char sccsid[] = "@(#)dbtest.c	5.14 (Berkeley) 05/01/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>
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
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;
6856039Sbostic 	int ch;
69*59626Sbostic 	char *fname, *infoarg, *p, buf[8 * 1024];
7056039Sbostic 
7156039Sbostic 	infoarg = NULL;
72*59626Sbostic 	fname = NULL;
73*59626Sbostic 	while ((ch = getopt(argc, argv, "f:i:o:")) != EOF)
7456039Sbostic 		switch(ch) {
75*59626Sbostic 		case 'f':
76*59626Sbostic 			fname = optarg;
77*59626Sbostic 			break;
7856039Sbostic 		case 'i':
7956039Sbostic 			infoarg = optarg;
8056039Sbostic 			break;
8156039Sbostic 		case 'o':
8256039Sbostic 			if ((ofd = open(optarg,
8356039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
8456039Sbostic 				err("%s: %s", optarg, strerror(errno));
8556039Sbostic 			break;
8656039Sbostic 		case '?':
8756039Sbostic 		default:
8856039Sbostic 			usage();
8956039Sbostic 		}
9056039Sbostic 	argc -= optind;
9156039Sbostic 	argv += optind;
9256039Sbostic 
9356039Sbostic 	if (argc != 2)
9456039Sbostic 		usage();
9556039Sbostic 
9656039Sbostic 	/* Set the type. */
9756039Sbostic 	type = dbtype(*argv++);
9856039Sbostic 
9956039Sbostic 	/* Open the descriptor file. */
10056039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
10156039Sbostic 		err("%s: %s", *argv, strerror(errno));
10256039Sbostic 
10356039Sbostic 	/* Set up the db structure as necessary. */
10456039Sbostic 	if (infoarg == NULL)
10556039Sbostic 		infop = NULL;
10656039Sbostic 	else
10757463Sbostic 		for (p = strtok(infoarg, ",\t "); p != NULL;
10857463Sbostic 		    p = strtok(0, ",\t "))
10956039Sbostic 			if (*p != '\0')
11056039Sbostic 				infop = setinfo(type, p);
11156039Sbostic 
112*59626Sbostic 	/* Open the DB. */
11356039Sbostic #define	BACKINGFILE	"/tmp/__dbtest"
114*59626Sbostic 	if (fname == NULL) {
115*59626Sbostic 		fname = BACKINGFILE;
116*59626Sbostic 		(void)unlink(BACKINGFILE);
117*59626Sbostic 	}
118*59626Sbostic 	if ((dbp = dbopen(fname,
11956039Sbostic 	    O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
12056039Sbostic 		err("dbopen: %s", strerror(errno));
12157456Sbostic 	XXdbp = dbp;
12256039Sbostic 
12356039Sbostic 	state = COMMAND;
12456039Sbostic 	for (lineno = 1;
12556039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
12656039Sbostic 		len = strlen(buf);
12756039Sbostic 		switch(*p) {
12856557Sbostic 		case 'c':			/* compare */
12956557Sbostic 			if (state != COMMAND)
13056557Sbostic 				err("line %lu: not expecting command", lineno);
13156557Sbostic 			state = KEY;
13256557Sbostic 			command = COMPARE;
13356557Sbostic 			break;
13456059Sbostic 		case 'e':			/* echo */
13556059Sbostic 			if (state != COMMAND)
13656059Sbostic 				err("line %lu: not expecting command", lineno);
13756557Sbostic 			/* Don't display the newline, if CR at EOL. */
13856557Sbostic 			if (p[len - 2] == '\r')
13956557Sbostic 				--len;
14056557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
14156557Sbostic 				err("write: %s", strerror(errno));
14256059Sbostic 			break;
14356039Sbostic 		case 'g':			/* get */
14456039Sbostic 			if (state != COMMAND)
14556039Sbostic 				err("line %lu: not expecting command", lineno);
14656039Sbostic 			state = KEY;
14756039Sbostic 			command = GET;
14856039Sbostic 			break;
14956039Sbostic 		case 'p':			/* put */
15056039Sbostic 			if (state != COMMAND)
15156039Sbostic 				err("line %lu: not expecting command", lineno);
15256039Sbostic 			state = KEY;
15356039Sbostic 			command = PUT;
15456039Sbostic 			break;
15556039Sbostic 		case 'r':			/* remove */
15656039Sbostic 			if (state != COMMAND)
15756039Sbostic 				err("line %lu: not expecting command", lineno);
15856039Sbostic 			state = KEY;
15956039Sbostic 			command = REMOVE;
16056039Sbostic 			break;
16156039Sbostic 		case 's':			/* seq */
16256039Sbostic 			if (state != COMMAND)
16356039Sbostic 				err("line %lu: not expecting command", lineno);
16456059Sbostic 			if (flags == R_CURSOR) {
16556059Sbostic 				state = KEY;
16656059Sbostic 				command = SEQ;
16756059Sbostic 			} else
16856059Sbostic 				seq(dbp, &key);
16956039Sbostic 			break;
17056039Sbostic 		case 'f':
17156059Sbostic 			flags = setflags(p + 1);
17256039Sbostic 			break;
17356039Sbostic 		case 'D':			/* data file */
17456039Sbostic 			if (state != DATA)
17556039Sbostic 				err("line %lu: not expecting data", lineno);
17656039Sbostic 			data.data = rfile(p + 1, &data.size);
17756992Sbostic 			goto ldata;
17856039Sbostic 		case 'd':			/* data */
17956039Sbostic 			if (state != DATA)
18056039Sbostic 				err("line %lu: not expecting data", lineno);
18156557Sbostic 			data.data = xmalloc(p + 1, len - 1);
18256557Sbostic 			data.size = len - 1;
18356992Sbostic ldata:			switch(command) {
18456557Sbostic 			case COMPARE:
18556557Sbostic 				compare(&keydata, &data);
18656557Sbostic 				break;
18756557Sbostic 			case PUT:
18856557Sbostic 				put(dbp, &key, &data);
18956557Sbostic 				break;
19056557Sbostic 			default:
19156039Sbostic 				err("line %lu: command doesn't take data",
19256039Sbostic 				    lineno);
19356557Sbostic 			}
19459493Sbostic 			if (type != DB_RECNO)
19559493Sbostic 				free(key.data);
19656039Sbostic 			free(data.data);
19756039Sbostic 			state = COMMAND;
19856039Sbostic 			break;
19956039Sbostic 		case 'K':			/* key file */
20056039Sbostic 			if (state != KEY)
20156039Sbostic 				err("line %lu: not expecting a key", lineno);
20256039Sbostic 			if (type == DB_RECNO)
20356039Sbostic 				err("line %lu: 'K' not available for recno",
20456039Sbostic 				    lineno);
20556039Sbostic 			key.data = rfile(p + 1, &key.size);
20656992Sbostic 			goto lkey;
20756039Sbostic 		case 'k':			/* key */
20856039Sbostic 			if (state != KEY)
20956039Sbostic 				err("line %lu: not expecting a key", lineno);
21056039Sbostic 			if (type == DB_RECNO) {
21156039Sbostic 				static recno_t recno;
21256039Sbostic 				recno = strtol(p + 1, NULL, 0);
21356039Sbostic 				key.data = &recno;
21456039Sbostic 				key.size = sizeof(recno);
21556039Sbostic 			} else {
21656039Sbostic 				key.data = xmalloc(p + 1, len - 1);
21756039Sbostic 				key.size = len - 1;
21856039Sbostic 			}
21956992Sbostic lkey:			switch(command) {
22056557Sbostic 			case COMPARE:
22156557Sbostic 				getdata(dbp, &key, &keydata);
22256557Sbostic 				state = DATA;
22356557Sbostic 				break;
22456039Sbostic 			case GET:
22556039Sbostic 				get(dbp, &key);
22656039Sbostic 				if (type != DB_RECNO)
22756039Sbostic 					free(key.data);
22856039Sbostic 				state = COMMAND;
22956039Sbostic 				break;
23056039Sbostic 			case PUT:
23156039Sbostic 				state = DATA;
23256039Sbostic 				break;
23356039Sbostic 			case REMOVE:
23456039Sbostic 				rem(dbp, &key);
23556039Sbostic 				if (type != DB_RECNO)
23656039Sbostic 					free(key.data);
23756039Sbostic 				state = COMMAND;
23856039Sbostic 				break;
23956059Sbostic 			case SEQ:
24056059Sbostic 				seq(dbp, &key);
24156059Sbostic 				if (type != DB_RECNO)
24256059Sbostic 					free(key.data);
24356059Sbostic 				state = COMMAND;
24456059Sbostic 				break;
24556039Sbostic 			default:
24656039Sbostic 				err("line %lu: command doesn't take a key",
24756039Sbostic 				    lineno);
24856039Sbostic 			}
24956039Sbostic 			break;
25056992Sbostic 		case 'o':
25156992Sbostic 			dump(dbp, p[1] == 'r');
25256992Sbostic 			break;
25356039Sbostic 		default:
25456039Sbostic 			err("line %lu: %s: unknown command character",
25556059Sbostic 			    p, lineno);
25656039Sbostic 		}
25756039Sbostic 	}
258*59626Sbostic 	if (dbp->close(dbp))
259*59626Sbostic 		err("db->close: %s", strerror(errno));
26056039Sbostic 	(void)close(ofd);
26156039Sbostic 	exit(0);
26256039Sbostic }
26356039Sbostic 
26456059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
26556059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
26656059Sbostic 
26756039Sbostic void
26856557Sbostic compare(db1, db2)
26956557Sbostic 	DBT *db1, *db2;
27056557Sbostic {
27156557Sbostic 	register size_t len;
27256557Sbostic 	register u_char *p1, *p2;
27356557Sbostic 
27456557Sbostic 	if (db1->size != db2->size)
27556557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
27656557Sbostic 		    db1->size, db2->size);
27756557Sbostic 
27856557Sbostic 	len = MIN(db1->size, db2->size);
27956557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
28056557Sbostic 		if (*p1++ != *p2++) {
28156557Sbostic 			printf("compare failed at offset %d\n",
28256557Sbostic 			    p1 - (u_char *)db1->data);
28356557Sbostic 			break;
28456557Sbostic 		}
28556557Sbostic }
28656557Sbostic 
28756557Sbostic void
28856039Sbostic get(dbp, kp)
28956039Sbostic 	DB *dbp;
29056039Sbostic 	DBT *kp;
29156039Sbostic {
29256039Sbostic 	DBT data;
29356039Sbostic 
29456059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
29556059Sbostic 	case 0:
29656059Sbostic 		(void)write(ofd, data.data, data.size);
29756059Sbostic 		break;
29856059Sbostic 	case -1:
29956039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
30056059Sbostic 		/* NOTREACHED */
30156059Sbostic 	case 1:
30256059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
30357456Sbostic 		(void)fprintf(stderr, "%d: %.*s: %s\n",
30457456Sbostic 		    lineno, kp->size, kp->data, NOSUCHKEY);
30556059Sbostic 		break;
30656059Sbostic 	}
30756039Sbostic }
30856039Sbostic 
30956039Sbostic void
31056557Sbostic getdata(dbp, kp, dp)
31156557Sbostic 	DB *dbp;
31256557Sbostic 	DBT *kp, *dp;
31356557Sbostic {
31456557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
31556557Sbostic 	case 0:
31656557Sbostic 		return;
31756557Sbostic 	case -1:
31856557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
31956557Sbostic 		/* NOTREACHED */
32056557Sbostic 	case 1:
32156557Sbostic 		err("line %lu: get failed, no such key", lineno);
32256557Sbostic 		/* NOTREACHED */
32356557Sbostic 	}
32456557Sbostic }
32556557Sbostic 
32656557Sbostic void
32756039Sbostic put(dbp, kp, dp)
32856039Sbostic 	DB *dbp;
32956039Sbostic 	DBT *kp, *dp;
33056039Sbostic {
33156059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
33256059Sbostic 	case 0:
33356059Sbostic 		break;
33456059Sbostic 	case -1:
33556039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
33656059Sbostic 		/* NOTREACHED */
33756059Sbostic 	case 1:
33856059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
33956059Sbostic 		break;
34056059Sbostic 	}
34156039Sbostic }
34256039Sbostic 
34356039Sbostic void
34456039Sbostic rem(dbp, kp)
34556039Sbostic 	DB *dbp;
34656039Sbostic 	DBT *kp;
34756039Sbostic {
34856059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
34956059Sbostic 	case 0:
35056059Sbostic 		break;
35156059Sbostic 	case -1:
35256039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
35356059Sbostic 		/* NOTREACHED */
35456059Sbostic 	case 1:
35556059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
35656059Sbostic 		break;
35756059Sbostic 	}
35856039Sbostic }
35956039Sbostic 
36056039Sbostic void
36156059Sbostic seq(dbp, kp)
36256039Sbostic 	DB *dbp;
36356059Sbostic 	DBT *kp;
36456039Sbostic {
36556059Sbostic 	DBT data;
36656039Sbostic 
36756059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
36856059Sbostic 	case 0:
36956059Sbostic 		(void)write(ofd, data.data, data.size);
37056059Sbostic 		break;
37156059Sbostic 	case -1:
37256039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
37356059Sbostic 		/* NOTREACHED */
37456059Sbostic 	case 1:
37556059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
37656059Sbostic 		break;
37756059Sbostic 	}
37856039Sbostic }
37956992Sbostic 
38056992Sbostic void
38156992Sbostic dump(dbp, rev)
38256992Sbostic 	DB *dbp;
38356992Sbostic 	int rev;
38456992Sbostic {
38556992Sbostic 	DBT key, data;
38656992Sbostic 	int flags, nflags;
38756992Sbostic 
38856992Sbostic 	if (rev) {
38956992Sbostic 		flags = R_LAST;
39056992Sbostic 		nflags = R_PREV;
39156992Sbostic 	} else {
39256992Sbostic 		flags = R_FIRST;
39356992Sbostic 		nflags = R_NEXT;
39456992Sbostic 	}
39556992Sbostic 	for (;; flags = nflags)
39656992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
39756992Sbostic 		case 0:
39856992Sbostic 			(void)write(ofd, data.data, data.size);
39956992Sbostic 			break;
40056992Sbostic 		case 1:
40156992Sbostic 			goto done;
40256992Sbostic 		case -1:
40356992Sbostic 			err("line %lu: (dump) seq: %s",
40456992Sbostic 			    lineno, strerror(errno));
40556992Sbostic 			/* NOTREACHED */
40656992Sbostic 		}
40756992Sbostic done:	return;
40856992Sbostic }
40956039Sbostic 
41056039Sbostic u_int
41156039Sbostic setflags(s)
41256039Sbostic 	char *s;
41356039Sbostic {
41456039Sbostic 	char *p;
41556039Sbostic 
41656039Sbostic 	for (; isspace(*s); ++s);
41756039Sbostic 	if (*s == '\n')
41856039Sbostic 		return (0);
41956039Sbostic 	if ((p = index(s, '\n')) != NULL)
42056039Sbostic 		*p = '\0';
42156039Sbostic 	if (!strcmp(s, "R_CURSOR"))
42256039Sbostic 		return (R_CURSOR);
42356749Sbostic 	if (!strcmp(s, "R_CURSORLOG"))
42456749Sbostic 		return (R_CURSORLOG);
42556749Sbostic 	if (!strcmp(s, "R_FIRST"))
42656749Sbostic 		return (R_FIRST);
42756039Sbostic 	if (!strcmp(s, "R_IAFTER"))
42856039Sbostic 		return (R_IAFTER);
42956039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
43056039Sbostic 		return (R_IBEFORE);
43156039Sbostic 	if (!strcmp(s, "R_LAST"))
43256039Sbostic 		return (R_LAST);
43356039Sbostic 	if (!strcmp(s, "R_NEXT"))
43456039Sbostic 		return (R_NEXT);
43556749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
43656749Sbostic 		return (R_NOOVERWRITE);
43756039Sbostic 	if (!strcmp(s, "R_PREV"))
43856039Sbostic 		return (R_PREV);
43956749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
44056749Sbostic 		return (R_SETCURSOR);
44156039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
44256039Sbostic 	/* NOTREACHED */
44356039Sbostic }
44456039Sbostic 
44556039Sbostic DBTYPE
44656039Sbostic dbtype(s)
44756039Sbostic 	char *s;
44856039Sbostic {
44956039Sbostic 	if (!strcmp(s, "btree"))
45056039Sbostic 		return (DB_BTREE);
45156039Sbostic 	if (!strcmp(s, "hash"))
45256039Sbostic 		return (DB_HASH);
45356039Sbostic 	if (!strcmp(s, "recno"))
45456039Sbostic 		return (DB_RECNO);
45556039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
45656039Sbostic 	/* NOTREACHED */
45756039Sbostic }
45856039Sbostic 
45956039Sbostic void *
46056039Sbostic setinfo(type, s)
46156039Sbostic 	DBTYPE type;
46256039Sbostic 	char *s;
46356039Sbostic {
46456039Sbostic 	static BTREEINFO ib;
46556039Sbostic 	static HASHINFO ih;
46656039Sbostic 	static RECNOINFO rh;
46756039Sbostic 	char *eq;
46856039Sbostic 
46956039Sbostic 	if ((eq = index(s, '=')) == NULL)
47056039Sbostic 		err("%s: illegal structure set statement", s);
47156039Sbostic 	*eq++ = '\0';
47256039Sbostic 	if (!isdigit(*eq))
47356039Sbostic 		err("%s: structure set statement must be a number", s);
47456039Sbostic 
47556039Sbostic 	switch(type) {
47656039Sbostic 	case DB_BTREE:
47756039Sbostic 		if (!strcmp("flags", s)) {
47856039Sbostic 			ib.flags = strtoul(eq, NULL, 0);
47956039Sbostic 			return (&ib);
48056039Sbostic 		}
48156039Sbostic 		if (!strcmp("cachesize", s)) {
48256039Sbostic 			ib.cachesize = strtoul(eq, NULL, 0);
48356039Sbostic 			return (&ib);
48456039Sbostic 		}
48556039Sbostic 		if (!strcmp("maxkeypage", s)) {
48656039Sbostic 			ib.maxkeypage = strtoul(eq, NULL, 0);
48756039Sbostic 			return (&ib);
48856039Sbostic 		}
48956039Sbostic 		if (!strcmp("minkeypage", s)) {
49056039Sbostic 			ib.minkeypage = strtoul(eq, NULL, 0);
49156039Sbostic 			return (&ib);
49256039Sbostic 		}
49356039Sbostic 		if (!strcmp("lorder", s)) {
49456039Sbostic 			ib.lorder = strtoul(eq, NULL, 0);
49556039Sbostic 			return (&ib);
49656039Sbostic 		}
49757456Sbostic 		if (!strcmp("psize", s)) {
49857456Sbostic 			ib.psize = strtoul(eq, NULL, 0);
49957456Sbostic 			return (&ib);
50057456Sbostic 		}
50156039Sbostic 		break;
50256039Sbostic 	case DB_HASH:
50356039Sbostic 		if (!strcmp("bsize", s)) {
50456039Sbostic 			ih.bsize = strtoul(eq, NULL, 0);
50556039Sbostic 			return (&ib);
50656039Sbostic 		}
50756039Sbostic 		if (!strcmp("ffactor", s)) {
50856039Sbostic 			ih.ffactor = strtoul(eq, NULL, 0);
50956039Sbostic 			return (&ib);
51056039Sbostic 		}
51156039Sbostic 		if (!strcmp("nelem", s)) {
51256039Sbostic 			ih.nelem = strtoul(eq, NULL, 0);
51356039Sbostic 			return (&ib);
51456039Sbostic 		}
51556039Sbostic 		if (!strcmp("cachesize", s)) {
51656039Sbostic 			ih.cachesize = strtoul(eq, NULL, 0);
51756039Sbostic 			return (&ib);
51856039Sbostic 		}
51956039Sbostic 		if (!strcmp("lorder", s)) {
52056039Sbostic 			ih.lorder = strtoul(eq, NULL, 0);
52156039Sbostic 			return (&ib);
52256039Sbostic 		}
52356039Sbostic 		break;
52456039Sbostic 	case DB_RECNO:
52556039Sbostic 		if (!strcmp("flags", s)) {
52656039Sbostic 			rh.flags = strtoul(eq, NULL, 0);
52756039Sbostic 			return (&ib);
52856039Sbostic 		}
52956039Sbostic 		if (!strcmp("cachesize", s)) {
53056039Sbostic 			rh.cachesize = strtoul(eq, NULL, 0);
53156039Sbostic 			return (&ib);
53256039Sbostic 		}
53356039Sbostic 		if (!strcmp("lorder", s)) {
53456039Sbostic 			rh.lorder = strtoul(eq, NULL, 0);
53556039Sbostic 			return (&ib);
53656039Sbostic 		}
53756039Sbostic 		if (!strcmp("reclen", s)) {
53856039Sbostic 			rh.reclen = strtoul(eq, NULL, 0);
53956039Sbostic 			return (&ib);
54056039Sbostic 		}
54156039Sbostic 		if (!strcmp("bval", s)) {
54256039Sbostic 			rh.bval = strtoul(eq, NULL, 0);
54356039Sbostic 			return (&ib);
54456039Sbostic 		}
54556039Sbostic 		break;
54656039Sbostic 	}
54756039Sbostic 	err("%s: unknown structure value", s);
54856039Sbostic 	/* NOTREACHED */
54956039Sbostic }
55056039Sbostic 
55156039Sbostic void *
55256039Sbostic rfile(name, lenp)
55356039Sbostic 	char *name;
55456039Sbostic 	size_t *lenp;
55556039Sbostic {
55656039Sbostic 	struct stat sb;
55756039Sbostic 	void *p;
55856039Sbostic 	int fd;
55956039Sbostic 	char *np;
56056039Sbostic 
56156039Sbostic 	for (; isspace(*name); ++name);
56256039Sbostic 	if ((np = index(name, '\n')) != NULL)
56356039Sbostic 		*np = '\0';
56456039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
56556039Sbostic 	    fstat(fd, &sb))
56656039Sbostic 		err("%s: %s\n", name, strerror(errno));
56758312Sbostic 	if (sb.st_size > (off_t)SIZE_T_MAX)
56856039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
56956039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
57056039Sbostic 		err("%s", strerror(errno));
57156039Sbostic 	(void)read(fd, p, (int)sb.st_size);
57256039Sbostic 	*lenp = sb.st_size;
57356489Sbostic 	(void)close(fd);
57456039Sbostic 	return (p);
57556039Sbostic }
57656039Sbostic 
57756039Sbostic void *
57856039Sbostic xmalloc(text, len)
57956039Sbostic 	char *text;
58056039Sbostic 	size_t len;
58156039Sbostic {
58256039Sbostic 	void *p;
58356039Sbostic 
58456039Sbostic 	if ((p = malloc(len)) == NULL)
58556039Sbostic 		err("%s", strerror(errno));
58658015Sbostic 	memmove(p, text, len);
58756039Sbostic 	return (p);
58856039Sbostic }
58956039Sbostic 
59056039Sbostic void
59156039Sbostic usage()
59256039Sbostic {
59356039Sbostic 	(void)fprintf(stderr,
594*59626Sbostic 	    "usage: dbtest [-f file] [-i info] [-o file] type script\n");
59556039Sbostic 	exit(1);
59656039Sbostic }
59756039Sbostic 
59856039Sbostic #if __STDC__
59956039Sbostic #include <stdarg.h>
60056039Sbostic #else
60156039Sbostic #include <varargs.h>
60256039Sbostic #endif
60356039Sbostic 
60456039Sbostic void
60556039Sbostic #if __STDC__
60656039Sbostic err(const char *fmt, ...)
60756039Sbostic #else
60856039Sbostic err(fmt, va_alist)
60956039Sbostic 	char *fmt;
61056039Sbostic         va_dcl
61156039Sbostic #endif
61256039Sbostic {
61356039Sbostic 	va_list ap;
61456039Sbostic #if __STDC__
61556039Sbostic 	va_start(ap, fmt);
61656039Sbostic #else
61756039Sbostic 	va_start(ap);
61856039Sbostic #endif
61956039Sbostic 	(void)fprintf(stderr, "dbtest: ");
62056039Sbostic 	(void)vfprintf(stderr, fmt, ap);
62156039Sbostic 	va_end(ap);
62256039Sbostic 	(void)fprintf(stderr, "\n");
62356039Sbostic 	exit(1);
62456039Sbostic 	/* NOTREACHED */
62556039Sbostic }
626