xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 57461)
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*57461Sbostic static char sccsid[] = "@(#)dbtest.c	5.8 (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 
5457456Sbostic DB *XXdbp;				/* Global for gdb. */
5557456Sbostic 
5656039Sbostic int
5756039Sbostic main(argc, argv)
5856039Sbostic 	int argc;
5956039Sbostic 	char *argv[];
6056039Sbostic {
61*57461Sbostic 	extern int optind;
62*57461Sbostic 	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
10256039Sbostic 		while ((p = strsep(&infoarg, ",\t ")) != NULL)
10356039Sbostic 			if (*p != '\0')
10456039Sbostic 				infop = setinfo(type, p);
10556039Sbostic 
10656039Sbostic #define	BACKINGFILE	"/tmp/__dbtest"
10756039Sbostic 	/* Open the DB. */
10856039Sbostic 	(void)unlink(BACKINGFILE);
10956039Sbostic 	if ((dbp = dbopen(BACKINGFILE,
11056039Sbostic 	    O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
11156039Sbostic 		err("dbopen: %s", strerror(errno));
11257456Sbostic 	XXdbp = dbp;
11356039Sbostic 
11456039Sbostic 	state = COMMAND;
11556039Sbostic 	for (lineno = 1;
11656039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
11756039Sbostic 		len = strlen(buf);
11856039Sbostic 		switch(*p) {
11956557Sbostic 		case 'c':			/* compare */
12056557Sbostic 			if (state != COMMAND)
12156557Sbostic 				err("line %lu: not expecting command", lineno);
12256557Sbostic 			state = KEY;
12356557Sbostic 			command = COMPARE;
12456557Sbostic 			break;
12556059Sbostic 		case 'e':			/* echo */
12656059Sbostic 			if (state != COMMAND)
12756059Sbostic 				err("line %lu: not expecting command", lineno);
12856557Sbostic 			/* Don't display the newline, if CR at EOL. */
12956557Sbostic 			if (p[len - 2] == '\r')
13056557Sbostic 				--len;
13156557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
13256557Sbostic 				err("write: %s", strerror(errno));
13356059Sbostic 			break;
13456039Sbostic 		case 'g':			/* get */
13556039Sbostic 			if (state != COMMAND)
13656039Sbostic 				err("line %lu: not expecting command", lineno);
13756039Sbostic 			state = KEY;
13856039Sbostic 			command = GET;
13956039Sbostic 			break;
14056039Sbostic 		case 'p':			/* put */
14156039Sbostic 			if (state != COMMAND)
14256039Sbostic 				err("line %lu: not expecting command", lineno);
14356039Sbostic 			state = KEY;
14456039Sbostic 			command = PUT;
14556039Sbostic 			break;
14656039Sbostic 		case 'r':			/* remove */
14756039Sbostic 			if (state != COMMAND)
14856039Sbostic 				err("line %lu: not expecting command", lineno);
14956039Sbostic 			state = KEY;
15056039Sbostic 			command = REMOVE;
15156039Sbostic 			break;
15256039Sbostic 		case 's':			/* seq */
15356039Sbostic 			if (state != COMMAND)
15456039Sbostic 				err("line %lu: not expecting command", lineno);
15556059Sbostic 			if (flags == R_CURSOR) {
15656059Sbostic 				state = KEY;
15756059Sbostic 				command = SEQ;
15856059Sbostic 			} else
15956059Sbostic 				seq(dbp, &key);
16056039Sbostic 			break;
16156039Sbostic 		case 'f':
16256059Sbostic 			flags = setflags(p + 1);
16356039Sbostic 			break;
16456039Sbostic 		case 'D':			/* data file */
16556039Sbostic 			if (state != DATA)
16656039Sbostic 				err("line %lu: not expecting data", lineno);
16756039Sbostic 			data.data = rfile(p + 1, &data.size);
16856992Sbostic 			goto ldata;
16956039Sbostic 		case 'd':			/* data */
17056039Sbostic 			if (state != DATA)
17156039Sbostic 				err("line %lu: not expecting data", lineno);
17256557Sbostic 			data.data = xmalloc(p + 1, len - 1);
17356557Sbostic 			data.size = len - 1;
17456992Sbostic ldata:			switch(command) {
17556557Sbostic 			case COMPARE:
17656557Sbostic 				compare(&keydata, &data);
17756557Sbostic 				break;
17856557Sbostic 			case PUT:
17956557Sbostic 				put(dbp, &key, &data);
18056557Sbostic 				break;
18156557Sbostic 			default:
18256039Sbostic 				err("line %lu: command doesn't take data",
18356039Sbostic 				    lineno);
18456557Sbostic 			}
18556039Sbostic 			free(key.data);
18656039Sbostic 			free(data.data);
18756039Sbostic 			state = COMMAND;
18856039Sbostic 			break;
18956039Sbostic 		case 'K':			/* key file */
19056039Sbostic 			if (state != KEY)
19156039Sbostic 				err("line %lu: not expecting a key", lineno);
19256039Sbostic 			if (type == DB_RECNO)
19356039Sbostic 				err("line %lu: 'K' not available for recno",
19456039Sbostic 				    lineno);
19556039Sbostic 			key.data = rfile(p + 1, &key.size);
19656992Sbostic 			goto lkey;
19756039Sbostic 		case 'k':			/* key */
19856039Sbostic 			if (state != KEY)
19956039Sbostic 				err("line %lu: not expecting a key", lineno);
20056039Sbostic 			if (type == DB_RECNO) {
20156039Sbostic 				static recno_t recno;
20256039Sbostic 				recno = strtol(p + 1, NULL, 0);
20356039Sbostic 				key.data = &recno;
20456039Sbostic 				key.size = sizeof(recno);
20556039Sbostic 			} else {
20656039Sbostic 				key.data = xmalloc(p + 1, len - 1);
20756039Sbostic 				key.size = len - 1;
20856039Sbostic 			}
20956992Sbostic lkey:			switch(command) {
21056557Sbostic 			case COMPARE:
21156557Sbostic 				getdata(dbp, &key, &keydata);
21256557Sbostic 				state = DATA;
21356557Sbostic 				break;
21456039Sbostic 			case GET:
21556039Sbostic 				get(dbp, &key);
21656039Sbostic 				if (type != DB_RECNO)
21756039Sbostic 					free(key.data);
21856039Sbostic 				state = COMMAND;
21956039Sbostic 				break;
22056039Sbostic 			case PUT:
22156039Sbostic 				state = DATA;
22256039Sbostic 				break;
22356039Sbostic 			case REMOVE:
22456039Sbostic 				rem(dbp, &key);
22556039Sbostic 				if (type != DB_RECNO)
22656039Sbostic 					free(key.data);
22756039Sbostic 				state = COMMAND;
22856039Sbostic 				break;
22956059Sbostic 			case SEQ:
23056059Sbostic 				seq(dbp, &key);
23156059Sbostic 				if (type != DB_RECNO)
23256059Sbostic 					free(key.data);
23356059Sbostic 				state = COMMAND;
23456059Sbostic 				break;
23556039Sbostic 			default:
23656039Sbostic 				err("line %lu: command doesn't take a key",
23756039Sbostic 				    lineno);
23856039Sbostic 			}
23956039Sbostic 			break;
24056992Sbostic 		case 'o':
24156992Sbostic 			dump(dbp, p[1] == 'r');
24256992Sbostic 			break;
24356039Sbostic 		default:
24456039Sbostic 			err("line %lu: %s: unknown command character",
24556059Sbostic 			    p, lineno);
24656039Sbostic 		}
24756039Sbostic 	}
24856039Sbostic 	(void)close(ofd);
24956039Sbostic 	exit(0);
25056039Sbostic }
25156039Sbostic 
25256059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
25356059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
25456059Sbostic 
25556039Sbostic void
25656557Sbostic compare(db1, db2)
25756557Sbostic 	DBT *db1, *db2;
25856557Sbostic {
25956557Sbostic 	register size_t len;
26056557Sbostic 	register u_char *p1, *p2;
26156557Sbostic 
26256557Sbostic 	if (db1->size != db2->size)
26356557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
26456557Sbostic 		    db1->size, db2->size);
26556557Sbostic 
26656557Sbostic 	len = MIN(db1->size, db2->size);
26756557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
26856557Sbostic 		if (*p1++ != *p2++) {
26956557Sbostic 			printf("compare failed at offset %d\n",
27056557Sbostic 			    p1 - (u_char *)db1->data);
27156557Sbostic 			break;
27256557Sbostic 		}
27356557Sbostic }
27456557Sbostic 
27556557Sbostic void
27656039Sbostic get(dbp, kp)
27756039Sbostic 	DB *dbp;
27856039Sbostic 	DBT *kp;
27956039Sbostic {
28056039Sbostic 	DBT data;
28156039Sbostic 
28256059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
28356059Sbostic 	case 0:
28456059Sbostic 		(void)write(ofd, data.data, data.size);
28556059Sbostic 		break;
28656059Sbostic 	case -1:
28756039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
28856059Sbostic 		/* NOTREACHED */
28956059Sbostic 	case 1:
29056059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
29157456Sbostic 		(void)fprintf(stderr, "%d: %.*s: %s\n",
29257456Sbostic 		    lineno, kp->size, kp->data, NOSUCHKEY);
29356059Sbostic 		break;
29456059Sbostic 	}
29556039Sbostic }
29656039Sbostic 
29756039Sbostic void
29856557Sbostic getdata(dbp, kp, dp)
29956557Sbostic 	DB *dbp;
30056557Sbostic 	DBT *kp, *dp;
30156557Sbostic {
30256557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
30356557Sbostic 	case 0:
30456557Sbostic 		return;
30556557Sbostic 	case -1:
30656557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
30756557Sbostic 		/* NOTREACHED */
30856557Sbostic 	case 1:
30956557Sbostic 		err("line %lu: get failed, no such key", lineno);
31056557Sbostic 		/* NOTREACHED */
31156557Sbostic 	}
31256557Sbostic }
31356557Sbostic 
31456557Sbostic void
31556039Sbostic put(dbp, kp, dp)
31656039Sbostic 	DB *dbp;
31756039Sbostic 	DBT *kp, *dp;
31856039Sbostic {
31956059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
32056059Sbostic 	case 0:
32156059Sbostic 		break;
32256059Sbostic 	case -1:
32356039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
32456059Sbostic 		/* NOTREACHED */
32556059Sbostic 	case 1:
32656059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
32756059Sbostic 		break;
32856059Sbostic 	}
32956039Sbostic }
33056039Sbostic 
33156039Sbostic void
33256039Sbostic rem(dbp, kp)
33356039Sbostic 	DB *dbp;
33456039Sbostic 	DBT *kp;
33556039Sbostic {
33656059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
33756059Sbostic 	case 0:
33856059Sbostic 		break;
33956059Sbostic 	case -1:
34056039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
34156059Sbostic 		/* NOTREACHED */
34256059Sbostic 	case 1:
34356059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
34456059Sbostic 		break;
34556059Sbostic 	}
34656039Sbostic }
34756039Sbostic 
34856039Sbostic void
34956059Sbostic seq(dbp, kp)
35056039Sbostic 	DB *dbp;
35156059Sbostic 	DBT *kp;
35256039Sbostic {
35356059Sbostic 	DBT data;
35456039Sbostic 
35556059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
35656059Sbostic 	case 0:
35756059Sbostic 		(void)write(ofd, data.data, data.size);
35856059Sbostic 		break;
35956059Sbostic 	case -1:
36056039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
36156059Sbostic 		/* NOTREACHED */
36256059Sbostic 	case 1:
36356059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
36456059Sbostic 		break;
36556059Sbostic 	}
36656039Sbostic }
36756992Sbostic 
36856992Sbostic void
36956992Sbostic dump(dbp, rev)
37056992Sbostic 	DB *dbp;
37156992Sbostic 	int rev;
37256992Sbostic {
37356992Sbostic 	DBT key, data;
37456992Sbostic 	int flags, nflags;
37556992Sbostic 
37656992Sbostic 	if (rev) {
37756992Sbostic 		flags = R_LAST;
37856992Sbostic 		nflags = R_PREV;
37956992Sbostic 	} else {
38056992Sbostic 		flags = R_FIRST;
38156992Sbostic 		nflags = R_NEXT;
38256992Sbostic 	}
38356992Sbostic 	for (;; flags = nflags)
38456992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
38556992Sbostic 		case 0:
38656992Sbostic 			(void)write(ofd, data.data, data.size);
38756992Sbostic 			break;
38856992Sbostic 		case 1:
38956992Sbostic 			goto done;
39056992Sbostic 		case -1:
39156992Sbostic 			err("line %lu: (dump) seq: %s",
39256992Sbostic 			    lineno, strerror(errno));
39356992Sbostic 			/* NOTREACHED */
39456992Sbostic 		}
39556992Sbostic done:	return;
39656992Sbostic }
39756039Sbostic 
39856039Sbostic u_int
39956039Sbostic setflags(s)
40056039Sbostic 	char *s;
40156039Sbostic {
40256039Sbostic 	char *p;
40356039Sbostic 
40456039Sbostic 	for (; isspace(*s); ++s);
40556039Sbostic 	if (*s == '\n')
40656039Sbostic 		return (0);
40756039Sbostic 	if ((p = index(s, '\n')) != NULL)
40856039Sbostic 		*p = '\0';
40956039Sbostic 	if (!strcmp(s, "R_CURSOR"))
41056039Sbostic 		return (R_CURSOR);
41156749Sbostic 	if (!strcmp(s, "R_CURSORLOG"))
41256749Sbostic 		return (R_CURSORLOG);
41356749Sbostic 	if (!strcmp(s, "R_FIRST"))
41456749Sbostic 		return (R_FIRST);
41556039Sbostic 	if (!strcmp(s, "R_IAFTER"))
41656039Sbostic 		return (R_IAFTER);
41756039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
41856039Sbostic 		return (R_IBEFORE);
41956039Sbostic 	if (!strcmp(s, "R_LAST"))
42056039Sbostic 		return (R_LAST);
42156039Sbostic 	if (!strcmp(s, "R_NEXT"))
42256039Sbostic 		return (R_NEXT);
42356749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
42456749Sbostic 		return (R_NOOVERWRITE);
42556039Sbostic 	if (!strcmp(s, "R_PREV"))
42656039Sbostic 		return (R_PREV);
42756749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
42856749Sbostic 		return (R_SETCURSOR);
42956039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
43056039Sbostic 	/* NOTREACHED */
43156039Sbostic }
43256039Sbostic 
43356039Sbostic DBTYPE
43456039Sbostic dbtype(s)
43556039Sbostic 	char *s;
43656039Sbostic {
43756039Sbostic 	if (!strcmp(s, "btree"))
43856039Sbostic 		return (DB_BTREE);
43956039Sbostic 	if (!strcmp(s, "hash"))
44056039Sbostic 		return (DB_HASH);
44156039Sbostic 	if (!strcmp(s, "recno"))
44256039Sbostic 		return (DB_RECNO);
44356039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
44456039Sbostic 	/* NOTREACHED */
44556039Sbostic }
44656039Sbostic 
44756039Sbostic void *
44856039Sbostic setinfo(type, s)
44956039Sbostic 	DBTYPE type;
45056039Sbostic 	char *s;
45156039Sbostic {
45256039Sbostic 	static BTREEINFO ib;
45356039Sbostic 	static HASHINFO ih;
45456039Sbostic 	static RECNOINFO rh;
45556039Sbostic 	char *eq;
45656039Sbostic 
45756039Sbostic 	if ((eq = index(s, '=')) == NULL)
45856039Sbostic 		err("%s: illegal structure set statement", s);
45956039Sbostic 	*eq++ = '\0';
46056039Sbostic 	if (!isdigit(*eq))
46156039Sbostic 		err("%s: structure set statement must be a number", s);
46256039Sbostic 
46356039Sbostic 	switch(type) {
46456039Sbostic 	case DB_BTREE:
46556039Sbostic 		if (!strcmp("flags", s)) {
46656039Sbostic 			ib.flags = strtoul(eq, NULL, 0);
46756039Sbostic 			return (&ib);
46856039Sbostic 		}
46956039Sbostic 		if (!strcmp("cachesize", s)) {
47056039Sbostic 			ib.cachesize = strtoul(eq, NULL, 0);
47156039Sbostic 			return (&ib);
47256039Sbostic 		}
47356039Sbostic 		if (!strcmp("maxkeypage", s)) {
47456039Sbostic 			ib.maxkeypage = strtoul(eq, NULL, 0);
47556039Sbostic 			return (&ib);
47656039Sbostic 		}
47756039Sbostic 		if (!strcmp("minkeypage", s)) {
47856039Sbostic 			ib.minkeypage = strtoul(eq, NULL, 0);
47956039Sbostic 			return (&ib);
48056039Sbostic 		}
48156039Sbostic 		if (!strcmp("lorder", s)) {
48256039Sbostic 			ib.lorder = strtoul(eq, NULL, 0);
48356039Sbostic 			return (&ib);
48456039Sbostic 		}
48557456Sbostic 		if (!strcmp("psize", s)) {
48657456Sbostic 			ib.psize = strtoul(eq, NULL, 0);
48757456Sbostic 			return (&ib);
48857456Sbostic 		}
48956039Sbostic 		break;
49056039Sbostic 	case DB_HASH:
49156039Sbostic 		if (!strcmp("bsize", s)) {
49256039Sbostic 			ih.bsize = strtoul(eq, NULL, 0);
49356039Sbostic 			return (&ib);
49456039Sbostic 		}
49556039Sbostic 		if (!strcmp("ffactor", s)) {
49656039Sbostic 			ih.ffactor = strtoul(eq, NULL, 0);
49756039Sbostic 			return (&ib);
49856039Sbostic 		}
49956039Sbostic 		if (!strcmp("nelem", s)) {
50056039Sbostic 			ih.nelem = strtoul(eq, NULL, 0);
50156039Sbostic 			return (&ib);
50256039Sbostic 		}
50356039Sbostic 		if (!strcmp("cachesize", s)) {
50456039Sbostic 			ih.cachesize = strtoul(eq, NULL, 0);
50556039Sbostic 			return (&ib);
50656039Sbostic 		}
50756039Sbostic 		if (!strcmp("lorder", s)) {
50856039Sbostic 			ih.lorder = strtoul(eq, NULL, 0);
50956039Sbostic 			return (&ib);
51056039Sbostic 		}
51156039Sbostic 		break;
51256039Sbostic 	case DB_RECNO:
51356039Sbostic 		if (!strcmp("flags", s)) {
51456039Sbostic 			rh.flags = strtoul(eq, NULL, 0);
51556039Sbostic 			return (&ib);
51656039Sbostic 		}
51756039Sbostic 		if (!strcmp("cachesize", s)) {
51856039Sbostic 			rh.cachesize = strtoul(eq, NULL, 0);
51956039Sbostic 			return (&ib);
52056039Sbostic 		}
52156039Sbostic 		if (!strcmp("lorder", s)) {
52256039Sbostic 			rh.lorder = strtoul(eq, NULL, 0);
52356039Sbostic 			return (&ib);
52456039Sbostic 		}
52556039Sbostic 		if (!strcmp("reclen", s)) {
52656039Sbostic 			rh.reclen = strtoul(eq, NULL, 0);
52756039Sbostic 			return (&ib);
52856039Sbostic 		}
52956039Sbostic 		if (!strcmp("bval", s)) {
53056039Sbostic 			rh.bval = strtoul(eq, NULL, 0);
53156039Sbostic 			return (&ib);
53256039Sbostic 		}
53356039Sbostic 		break;
53456039Sbostic 	}
53556039Sbostic 	err("%s: unknown structure value", s);
53656039Sbostic 	/* NOTREACHED */
53756039Sbostic }
53856039Sbostic 
53956039Sbostic void *
54056039Sbostic rfile(name, lenp)
54156039Sbostic 	char *name;
54256039Sbostic 	size_t *lenp;
54356039Sbostic {
54456039Sbostic 	struct stat sb;
54556039Sbostic 	void *p;
54656039Sbostic 	int fd;
54756039Sbostic 	char *np;
54856039Sbostic 
54956039Sbostic 	for (; isspace(*name); ++name);
55056039Sbostic 	if ((np = index(name, '\n')) != NULL)
55156039Sbostic 		*np = '\0';
55256039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
55356039Sbostic 	    fstat(fd, &sb))
55456039Sbostic 		err("%s: %s\n", name, strerror(errno));
55556039Sbostic 	if (sb.st_size > SIZE_T_MAX)
55656039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
55756039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
55856039Sbostic 		err("%s", strerror(errno));
55956039Sbostic 	(void)read(fd, p, (int)sb.st_size);
56056039Sbostic 	*lenp = sb.st_size;
56156489Sbostic 	(void)close(fd);
56256039Sbostic 	return (p);
56356039Sbostic }
56456039Sbostic 
56556039Sbostic void *
56656039Sbostic xmalloc(text, len)
56756039Sbostic 	char *text;
56856039Sbostic 	size_t len;
56956039Sbostic {
57056039Sbostic 	void *p;
57156039Sbostic 
57256039Sbostic 	if ((p = malloc(len)) == NULL)
57356039Sbostic 		err("%s", strerror(errno));
57456039Sbostic 	bcopy(text, p, len);
57556039Sbostic 	return (p);
57656039Sbostic }
57756039Sbostic 
57856039Sbostic void
57956039Sbostic usage()
58056039Sbostic {
58156039Sbostic 	(void)fprintf(stderr,
58256039Sbostic 	    "usage: dbtest [-i info] [-o file] type script\n");
58356039Sbostic 	exit(1);
58456039Sbostic }
58556039Sbostic 
58656039Sbostic #if __STDC__
58756039Sbostic #include <stdarg.h>
58856039Sbostic #else
58956039Sbostic #include <varargs.h>
59056039Sbostic #endif
59156039Sbostic 
59256039Sbostic void
59356039Sbostic #if __STDC__
59456039Sbostic err(const char *fmt, ...)
59556039Sbostic #else
59656039Sbostic err(fmt, va_alist)
59756039Sbostic 	char *fmt;
59856039Sbostic         va_dcl
59956039Sbostic #endif
60056039Sbostic {
60156039Sbostic 	va_list ap;
60256039Sbostic #if __STDC__
60356039Sbostic 	va_start(ap, fmt);
60456039Sbostic #else
60556039Sbostic 	va_start(ap);
60656039Sbostic #endif
60756039Sbostic 	(void)fprintf(stderr, "dbtest: ");
60856039Sbostic 	(void)vfprintf(stderr, fmt, ap);
60956039Sbostic 	va_end(ap);
61056039Sbostic 	(void)fprintf(stderr, "\n");
61156039Sbostic 	exit(1);
61256039Sbostic 	/* NOTREACHED */
61356039Sbostic }
614