xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 58312)
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*58312Sbostic static char sccsid[] = "@(#)dbtest.c	5.11 (Berkeley) 02/28/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 
5457456Sbostic DB *XXdbp;				/* Global for gdb. */
5557456Sbostic 
5656039Sbostic int
5756039Sbostic main(argc, argv)
5856039Sbostic 	int argc;
5956039Sbostic 	char *argv[];
6056039Sbostic {
6157461Sbostic 	extern int optind;
6257461Sbostic 	extern char *optarg;
6356039Sbostic 	enum S command, state;
6456039Sbostic 	DB *dbp;
6556557Sbostic 	DBT data, key, keydata;
6656039Sbostic 	size_t len;
6756039Sbostic 	int ch;
6856039Sbostic 	char *infoarg, *p, buf[8 * 1024];
6956039Sbostic 
7056039Sbostic 	infoarg = NULL;
7156039Sbostic 	while ((ch = getopt(argc, argv, "i:o:")) != EOF)
7256039Sbostic 		switch(ch) {
7356039Sbostic 		case 'i':
7456039Sbostic 			infoarg = optarg;
7556039Sbostic 			break;
7656039Sbostic 		case 'o':
7756039Sbostic 			if ((ofd = open(optarg,
7856039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
7956039Sbostic 				err("%s: %s", optarg, strerror(errno));
8056039Sbostic 			break;
8156039Sbostic 		case '?':
8256039Sbostic 		default:
8356039Sbostic 			usage();
8456039Sbostic 		}
8556039Sbostic 	argc -= optind;
8656039Sbostic 	argv += optind;
8756039Sbostic 
8856039Sbostic 	if (argc != 2)
8956039Sbostic 		usage();
9056039Sbostic 
9156039Sbostic 	/* Set the type. */
9256039Sbostic 	type = dbtype(*argv++);
9356039Sbostic 
9456039Sbostic 	/* Open the descriptor file. */
9556039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
9656039Sbostic 		err("%s: %s", *argv, strerror(errno));
9756039Sbostic 
9856039Sbostic 	/* Set up the db structure as necessary. */
9956039Sbostic 	if (infoarg == NULL)
10056039Sbostic 		infop = NULL;
10156039Sbostic 	else
10257463Sbostic 		for (p = strtok(infoarg, ",\t "); p != NULL;
10357463Sbostic 		    p = strtok(0, ",\t "))
10456039Sbostic 			if (*p != '\0')
10556039Sbostic 				infop = setinfo(type, p);
10656039Sbostic 
10756039Sbostic #define	BACKINGFILE	"/tmp/__dbtest"
10856039Sbostic 	/* Open the DB. */
10956039Sbostic 	(void)unlink(BACKINGFILE);
11056039Sbostic 	if ((dbp = dbopen(BACKINGFILE,
11156039Sbostic 	    O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
11256039Sbostic 		err("dbopen: %s", strerror(errno));
11357456Sbostic 	XXdbp = dbp;
11456039Sbostic 
11556039Sbostic 	state = COMMAND;
11656039Sbostic 	for (lineno = 1;
11756039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
11856039Sbostic 		len = strlen(buf);
11956039Sbostic 		switch(*p) {
12056557Sbostic 		case 'c':			/* compare */
12156557Sbostic 			if (state != COMMAND)
12256557Sbostic 				err("line %lu: not expecting command", lineno);
12356557Sbostic 			state = KEY;
12456557Sbostic 			command = COMPARE;
12556557Sbostic 			break;
12656059Sbostic 		case 'e':			/* echo */
12756059Sbostic 			if (state != COMMAND)
12856059Sbostic 				err("line %lu: not expecting command", lineno);
12956557Sbostic 			/* Don't display the newline, if CR at EOL. */
13056557Sbostic 			if (p[len - 2] == '\r')
13156557Sbostic 				--len;
13256557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
13356557Sbostic 				err("write: %s", strerror(errno));
13456059Sbostic 			break;
13556039Sbostic 		case 'g':			/* get */
13656039Sbostic 			if (state != COMMAND)
13756039Sbostic 				err("line %lu: not expecting command", lineno);
13856039Sbostic 			state = KEY;
13956039Sbostic 			command = GET;
14056039Sbostic 			break;
14156039Sbostic 		case 'p':			/* put */
14256039Sbostic 			if (state != COMMAND)
14356039Sbostic 				err("line %lu: not expecting command", lineno);
14456039Sbostic 			state = KEY;
14556039Sbostic 			command = PUT;
14656039Sbostic 			break;
14756039Sbostic 		case 'r':			/* remove */
14856039Sbostic 			if (state != COMMAND)
14956039Sbostic 				err("line %lu: not expecting command", lineno);
15056039Sbostic 			state = KEY;
15156039Sbostic 			command = REMOVE;
15256039Sbostic 			break;
15356039Sbostic 		case 's':			/* seq */
15456039Sbostic 			if (state != COMMAND)
15556039Sbostic 				err("line %lu: not expecting command", lineno);
15656059Sbostic 			if (flags == R_CURSOR) {
15756059Sbostic 				state = KEY;
15856059Sbostic 				command = SEQ;
15956059Sbostic 			} else
16056059Sbostic 				seq(dbp, &key);
16156039Sbostic 			break;
16256039Sbostic 		case 'f':
16356059Sbostic 			flags = setflags(p + 1);
16456039Sbostic 			break;
16556039Sbostic 		case 'D':			/* data file */
16656039Sbostic 			if (state != DATA)
16756039Sbostic 				err("line %lu: not expecting data", lineno);
16856039Sbostic 			data.data = rfile(p + 1, &data.size);
16956992Sbostic 			goto ldata;
17056039Sbostic 		case 'd':			/* data */
17156039Sbostic 			if (state != DATA)
17256039Sbostic 				err("line %lu: not expecting data", lineno);
17356557Sbostic 			data.data = xmalloc(p + 1, len - 1);
17456557Sbostic 			data.size = len - 1;
17556992Sbostic ldata:			switch(command) {
17656557Sbostic 			case COMPARE:
17756557Sbostic 				compare(&keydata, &data);
17856557Sbostic 				break;
17956557Sbostic 			case PUT:
18056557Sbostic 				put(dbp, &key, &data);
18156557Sbostic 				break;
18256557Sbostic 			default:
18356039Sbostic 				err("line %lu: command doesn't take data",
18456039Sbostic 				    lineno);
18556557Sbostic 			}
18656039Sbostic 			free(key.data);
18756039Sbostic 			free(data.data);
18856039Sbostic 			state = COMMAND;
18956039Sbostic 			break;
19056039Sbostic 		case 'K':			/* key file */
19156039Sbostic 			if (state != KEY)
19256039Sbostic 				err("line %lu: not expecting a key", lineno);
19356039Sbostic 			if (type == DB_RECNO)
19456039Sbostic 				err("line %lu: 'K' not available for recno",
19556039Sbostic 				    lineno);
19656039Sbostic 			key.data = rfile(p + 1, &key.size);
19756992Sbostic 			goto lkey;
19856039Sbostic 		case 'k':			/* key */
19956039Sbostic 			if (state != KEY)
20056039Sbostic 				err("line %lu: not expecting a key", lineno);
20156039Sbostic 			if (type == DB_RECNO) {
20256039Sbostic 				static recno_t recno;
20356039Sbostic 				recno = strtol(p + 1, NULL, 0);
20456039Sbostic 				key.data = &recno;
20556039Sbostic 				key.size = sizeof(recno);
20656039Sbostic 			} else {
20756039Sbostic 				key.data = xmalloc(p + 1, len - 1);
20856039Sbostic 				key.size = len - 1;
20956039Sbostic 			}
21056992Sbostic lkey:			switch(command) {
21156557Sbostic 			case COMPARE:
21256557Sbostic 				getdata(dbp, &key, &keydata);
21356557Sbostic 				state = DATA;
21456557Sbostic 				break;
21556039Sbostic 			case GET:
21656039Sbostic 				get(dbp, &key);
21756039Sbostic 				if (type != DB_RECNO)
21856039Sbostic 					free(key.data);
21956039Sbostic 				state = COMMAND;
22056039Sbostic 				break;
22156039Sbostic 			case PUT:
22256039Sbostic 				state = DATA;
22356039Sbostic 				break;
22456039Sbostic 			case REMOVE:
22556039Sbostic 				rem(dbp, &key);
22656039Sbostic 				if (type != DB_RECNO)
22756039Sbostic 					free(key.data);
22856039Sbostic 				state = COMMAND;
22956039Sbostic 				break;
23056059Sbostic 			case SEQ:
23156059Sbostic 				seq(dbp, &key);
23256059Sbostic 				if (type != DB_RECNO)
23356059Sbostic 					free(key.data);
23456059Sbostic 				state = COMMAND;
23556059Sbostic 				break;
23656039Sbostic 			default:
23756039Sbostic 				err("line %lu: command doesn't take a key",
23856039Sbostic 				    lineno);
23956039Sbostic 			}
24056039Sbostic 			break;
24156992Sbostic 		case 'o':
24256992Sbostic 			dump(dbp, p[1] == 'r');
24356992Sbostic 			break;
24456039Sbostic 		default:
24556039Sbostic 			err("line %lu: %s: unknown command character",
24656059Sbostic 			    p, lineno);
24756039Sbostic 		}
24856039Sbostic 	}
24956039Sbostic 	(void)close(ofd);
25056039Sbostic 	exit(0);
25156039Sbostic }
25256039Sbostic 
25356059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
25456059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
25556059Sbostic 
25656039Sbostic void
25756557Sbostic compare(db1, db2)
25856557Sbostic 	DBT *db1, *db2;
25956557Sbostic {
26056557Sbostic 	register size_t len;
26156557Sbostic 	register u_char *p1, *p2;
26256557Sbostic 
26356557Sbostic 	if (db1->size != db2->size)
26456557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
26556557Sbostic 		    db1->size, db2->size);
26656557Sbostic 
26756557Sbostic 	len = MIN(db1->size, db2->size);
26856557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
26956557Sbostic 		if (*p1++ != *p2++) {
27056557Sbostic 			printf("compare failed at offset %d\n",
27156557Sbostic 			    p1 - (u_char *)db1->data);
27256557Sbostic 			break;
27356557Sbostic 		}
27456557Sbostic }
27556557Sbostic 
27656557Sbostic void
27756039Sbostic get(dbp, kp)
27856039Sbostic 	DB *dbp;
27956039Sbostic 	DBT *kp;
28056039Sbostic {
28156039Sbostic 	DBT data;
28256039Sbostic 
28356059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
28456059Sbostic 	case 0:
28556059Sbostic 		(void)write(ofd, data.data, data.size);
28656059Sbostic 		break;
28756059Sbostic 	case -1:
28856039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
28956059Sbostic 		/* NOTREACHED */
29056059Sbostic 	case 1:
29156059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
29257456Sbostic 		(void)fprintf(stderr, "%d: %.*s: %s\n",
29357456Sbostic 		    lineno, kp->size, kp->data, NOSUCHKEY);
29456059Sbostic 		break;
29556059Sbostic 	}
29656039Sbostic }
29756039Sbostic 
29856039Sbostic void
29956557Sbostic getdata(dbp, kp, dp)
30056557Sbostic 	DB *dbp;
30156557Sbostic 	DBT *kp, *dp;
30256557Sbostic {
30356557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
30456557Sbostic 	case 0:
30556557Sbostic 		return;
30656557Sbostic 	case -1:
30756557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
30856557Sbostic 		/* NOTREACHED */
30956557Sbostic 	case 1:
31056557Sbostic 		err("line %lu: get failed, no such key", lineno);
31156557Sbostic 		/* NOTREACHED */
31256557Sbostic 	}
31356557Sbostic }
31456557Sbostic 
31556557Sbostic void
31656039Sbostic put(dbp, kp, dp)
31756039Sbostic 	DB *dbp;
31856039Sbostic 	DBT *kp, *dp;
31956039Sbostic {
32056059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
32156059Sbostic 	case 0:
32256059Sbostic 		break;
32356059Sbostic 	case -1:
32456039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
32556059Sbostic 		/* NOTREACHED */
32656059Sbostic 	case 1:
32756059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
32856059Sbostic 		break;
32956059Sbostic 	}
33056039Sbostic }
33156039Sbostic 
33256039Sbostic void
33356039Sbostic rem(dbp, kp)
33456039Sbostic 	DB *dbp;
33556039Sbostic 	DBT *kp;
33656039Sbostic {
33756059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
33856059Sbostic 	case 0:
33956059Sbostic 		break;
34056059Sbostic 	case -1:
34156039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
34256059Sbostic 		/* NOTREACHED */
34356059Sbostic 	case 1:
34456059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
34556059Sbostic 		break;
34656059Sbostic 	}
34756039Sbostic }
34856039Sbostic 
34956039Sbostic void
35056059Sbostic seq(dbp, kp)
35156039Sbostic 	DB *dbp;
35256059Sbostic 	DBT *kp;
35356039Sbostic {
35456059Sbostic 	DBT data;
35556039Sbostic 
35656059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
35756059Sbostic 	case 0:
35856059Sbostic 		(void)write(ofd, data.data, data.size);
35956059Sbostic 		break;
36056059Sbostic 	case -1:
36156039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
36256059Sbostic 		/* NOTREACHED */
36356059Sbostic 	case 1:
36456059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
36556059Sbostic 		break;
36656059Sbostic 	}
36756039Sbostic }
36856992Sbostic 
36956992Sbostic void
37056992Sbostic dump(dbp, rev)
37156992Sbostic 	DB *dbp;
37256992Sbostic 	int rev;
37356992Sbostic {
37456992Sbostic 	DBT key, data;
37556992Sbostic 	int flags, nflags;
37656992Sbostic 
37756992Sbostic 	if (rev) {
37856992Sbostic 		flags = R_LAST;
37956992Sbostic 		nflags = R_PREV;
38056992Sbostic 	} else {
38156992Sbostic 		flags = R_FIRST;
38256992Sbostic 		nflags = R_NEXT;
38356992Sbostic 	}
38456992Sbostic 	for (;; flags = nflags)
38556992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
38656992Sbostic 		case 0:
38756992Sbostic 			(void)write(ofd, data.data, data.size);
38856992Sbostic 			break;
38956992Sbostic 		case 1:
39056992Sbostic 			goto done;
39156992Sbostic 		case -1:
39256992Sbostic 			err("line %lu: (dump) seq: %s",
39356992Sbostic 			    lineno, strerror(errno));
39456992Sbostic 			/* NOTREACHED */
39556992Sbostic 		}
39656992Sbostic done:	return;
39756992Sbostic }
39856039Sbostic 
39956039Sbostic u_int
40056039Sbostic setflags(s)
40156039Sbostic 	char *s;
40256039Sbostic {
40356039Sbostic 	char *p;
40456039Sbostic 
40556039Sbostic 	for (; isspace(*s); ++s);
40656039Sbostic 	if (*s == '\n')
40756039Sbostic 		return (0);
40856039Sbostic 	if ((p = index(s, '\n')) != NULL)
40956039Sbostic 		*p = '\0';
41056039Sbostic 	if (!strcmp(s, "R_CURSOR"))
41156039Sbostic 		return (R_CURSOR);
41256749Sbostic 	if (!strcmp(s, "R_CURSORLOG"))
41356749Sbostic 		return (R_CURSORLOG);
41456749Sbostic 	if (!strcmp(s, "R_FIRST"))
41556749Sbostic 		return (R_FIRST);
41656039Sbostic 	if (!strcmp(s, "R_IAFTER"))
41756039Sbostic 		return (R_IAFTER);
41856039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
41956039Sbostic 		return (R_IBEFORE);
42056039Sbostic 	if (!strcmp(s, "R_LAST"))
42156039Sbostic 		return (R_LAST);
42256039Sbostic 	if (!strcmp(s, "R_NEXT"))
42356039Sbostic 		return (R_NEXT);
42456749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
42556749Sbostic 		return (R_NOOVERWRITE);
42656039Sbostic 	if (!strcmp(s, "R_PREV"))
42756039Sbostic 		return (R_PREV);
42856749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
42956749Sbostic 		return (R_SETCURSOR);
43056039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
43156039Sbostic 	/* NOTREACHED */
43256039Sbostic }
43356039Sbostic 
43456039Sbostic DBTYPE
43556039Sbostic dbtype(s)
43656039Sbostic 	char *s;
43756039Sbostic {
43856039Sbostic 	if (!strcmp(s, "btree"))
43956039Sbostic 		return (DB_BTREE);
44056039Sbostic 	if (!strcmp(s, "hash"))
44156039Sbostic 		return (DB_HASH);
44256039Sbostic 	if (!strcmp(s, "recno"))
44356039Sbostic 		return (DB_RECNO);
44456039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
44556039Sbostic 	/* NOTREACHED */
44656039Sbostic }
44756039Sbostic 
44856039Sbostic void *
44956039Sbostic setinfo(type, s)
45056039Sbostic 	DBTYPE type;
45156039Sbostic 	char *s;
45256039Sbostic {
45356039Sbostic 	static BTREEINFO ib;
45456039Sbostic 	static HASHINFO ih;
45556039Sbostic 	static RECNOINFO rh;
45656039Sbostic 	char *eq;
45756039Sbostic 
45856039Sbostic 	if ((eq = index(s, '=')) == NULL)
45956039Sbostic 		err("%s: illegal structure set statement", s);
46056039Sbostic 	*eq++ = '\0';
46156039Sbostic 	if (!isdigit(*eq))
46256039Sbostic 		err("%s: structure set statement must be a number", s);
46356039Sbostic 
46456039Sbostic 	switch(type) {
46556039Sbostic 	case DB_BTREE:
46656039Sbostic 		if (!strcmp("flags", s)) {
46756039Sbostic 			ib.flags = strtoul(eq, NULL, 0);
46856039Sbostic 			return (&ib);
46956039Sbostic 		}
47056039Sbostic 		if (!strcmp("cachesize", s)) {
47156039Sbostic 			ib.cachesize = strtoul(eq, NULL, 0);
47256039Sbostic 			return (&ib);
47356039Sbostic 		}
47456039Sbostic 		if (!strcmp("maxkeypage", s)) {
47556039Sbostic 			ib.maxkeypage = strtoul(eq, NULL, 0);
47656039Sbostic 			return (&ib);
47756039Sbostic 		}
47856039Sbostic 		if (!strcmp("minkeypage", s)) {
47956039Sbostic 			ib.minkeypage = strtoul(eq, NULL, 0);
48056039Sbostic 			return (&ib);
48156039Sbostic 		}
48256039Sbostic 		if (!strcmp("lorder", s)) {
48356039Sbostic 			ib.lorder = strtoul(eq, NULL, 0);
48456039Sbostic 			return (&ib);
48556039Sbostic 		}
48657456Sbostic 		if (!strcmp("psize", s)) {
48757456Sbostic 			ib.psize = strtoul(eq, NULL, 0);
48857456Sbostic 			return (&ib);
48957456Sbostic 		}
49056039Sbostic 		break;
49156039Sbostic 	case DB_HASH:
49256039Sbostic 		if (!strcmp("bsize", s)) {
49356039Sbostic 			ih.bsize = strtoul(eq, NULL, 0);
49456039Sbostic 			return (&ib);
49556039Sbostic 		}
49656039Sbostic 		if (!strcmp("ffactor", s)) {
49756039Sbostic 			ih.ffactor = strtoul(eq, NULL, 0);
49856039Sbostic 			return (&ib);
49956039Sbostic 		}
50056039Sbostic 		if (!strcmp("nelem", s)) {
50156039Sbostic 			ih.nelem = strtoul(eq, NULL, 0);
50256039Sbostic 			return (&ib);
50356039Sbostic 		}
50456039Sbostic 		if (!strcmp("cachesize", s)) {
50556039Sbostic 			ih.cachesize = strtoul(eq, NULL, 0);
50656039Sbostic 			return (&ib);
50756039Sbostic 		}
50856039Sbostic 		if (!strcmp("lorder", s)) {
50956039Sbostic 			ih.lorder = strtoul(eq, NULL, 0);
51056039Sbostic 			return (&ib);
51156039Sbostic 		}
51256039Sbostic 		break;
51356039Sbostic 	case DB_RECNO:
51456039Sbostic 		if (!strcmp("flags", s)) {
51556039Sbostic 			rh.flags = strtoul(eq, NULL, 0);
51656039Sbostic 			return (&ib);
51756039Sbostic 		}
51856039Sbostic 		if (!strcmp("cachesize", s)) {
51956039Sbostic 			rh.cachesize = strtoul(eq, NULL, 0);
52056039Sbostic 			return (&ib);
52156039Sbostic 		}
52256039Sbostic 		if (!strcmp("lorder", s)) {
52356039Sbostic 			rh.lorder = strtoul(eq, NULL, 0);
52456039Sbostic 			return (&ib);
52556039Sbostic 		}
52656039Sbostic 		if (!strcmp("reclen", s)) {
52756039Sbostic 			rh.reclen = strtoul(eq, NULL, 0);
52856039Sbostic 			return (&ib);
52956039Sbostic 		}
53056039Sbostic 		if (!strcmp("bval", s)) {
53156039Sbostic 			rh.bval = strtoul(eq, NULL, 0);
53256039Sbostic 			return (&ib);
53356039Sbostic 		}
53456039Sbostic 		break;
53556039Sbostic 	}
53656039Sbostic 	err("%s: unknown structure value", s);
53756039Sbostic 	/* NOTREACHED */
53856039Sbostic }
53956039Sbostic 
54056039Sbostic void *
54156039Sbostic rfile(name, lenp)
54256039Sbostic 	char *name;
54356039Sbostic 	size_t *lenp;
54456039Sbostic {
54556039Sbostic 	struct stat sb;
54656039Sbostic 	void *p;
54756039Sbostic 	int fd;
54856039Sbostic 	char *np;
54956039Sbostic 
55056039Sbostic 	for (; isspace(*name); ++name);
55156039Sbostic 	if ((np = index(name, '\n')) != NULL)
55256039Sbostic 		*np = '\0';
55356039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
55456039Sbostic 	    fstat(fd, &sb))
55556039Sbostic 		err("%s: %s\n", name, strerror(errno));
556*58312Sbostic 	if (sb.st_size > (off_t)SIZE_T_MAX)
55756039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
55856039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
55956039Sbostic 		err("%s", strerror(errno));
56056039Sbostic 	(void)read(fd, p, (int)sb.st_size);
56156039Sbostic 	*lenp = sb.st_size;
56256489Sbostic 	(void)close(fd);
56356039Sbostic 	return (p);
56456039Sbostic }
56556039Sbostic 
56656039Sbostic void *
56756039Sbostic xmalloc(text, len)
56856039Sbostic 	char *text;
56956039Sbostic 	size_t len;
57056039Sbostic {
57156039Sbostic 	void *p;
57256039Sbostic 
57356039Sbostic 	if ((p = malloc(len)) == NULL)
57456039Sbostic 		err("%s", strerror(errno));
57558015Sbostic 	memmove(p, text, len);
57656039Sbostic 	return (p);
57756039Sbostic }
57856039Sbostic 
57956039Sbostic void
58056039Sbostic usage()
58156039Sbostic {
58256039Sbostic 	(void)fprintf(stderr,
58356039Sbostic 	    "usage: dbtest [-i info] [-o file] type script\n");
58456039Sbostic 	exit(1);
58556039Sbostic }
58656039Sbostic 
58756039Sbostic #if __STDC__
58856039Sbostic #include <stdarg.h>
58956039Sbostic #else
59056039Sbostic #include <varargs.h>
59156039Sbostic #endif
59256039Sbostic 
59356039Sbostic void
59456039Sbostic #if __STDC__
59556039Sbostic err(const char *fmt, ...)
59656039Sbostic #else
59756039Sbostic err(fmt, va_alist)
59856039Sbostic 	char *fmt;
59956039Sbostic         va_dcl
60056039Sbostic #endif
60156039Sbostic {
60256039Sbostic 	va_list ap;
60356039Sbostic #if __STDC__
60456039Sbostic 	va_start(ap, fmt);
60556039Sbostic #else
60656039Sbostic 	va_start(ap);
60756039Sbostic #endif
60856039Sbostic 	(void)fprintf(stderr, "dbtest: ");
60956039Sbostic 	(void)vfprintf(stderr, fmt, ap);
61056039Sbostic 	va_end(ap);
61156039Sbostic 	(void)fprintf(stderr, "\n");
61256039Sbostic 	exit(1);
61356039Sbostic 	/* NOTREACHED */
61456039Sbostic }
615