xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 56992)
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*56992Sbostic static char sccsid[] = "@(#)dbtest.c	5.6 (Berkeley) 12/04/92";
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 *));
35*56992Sbostic 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 
5456039Sbostic int
5556039Sbostic main(argc, argv)
5656039Sbostic 	int argc;
5756039Sbostic 	char *argv[];
5856039Sbostic {
5956039Sbostic 	enum S command, state;
6056039Sbostic 	DB *dbp;
6156557Sbostic 	DBT data, key, keydata;
6256039Sbostic 	size_t len;
6356039Sbostic 	int ch;
6456039Sbostic 	char *infoarg, *p, buf[8 * 1024];
6556039Sbostic 
6656039Sbostic 	infoarg = NULL;
6756039Sbostic 	while ((ch = getopt(argc, argv, "i:o:")) != EOF)
6856039Sbostic 		switch(ch) {
6956039Sbostic 		case 'i':
7056039Sbostic 			infoarg = optarg;
7156039Sbostic 			break;
7256039Sbostic 		case 'o':
7356039Sbostic 			if ((ofd = open(optarg,
7456039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
7556039Sbostic 				err("%s: %s", optarg, strerror(errno));
7656039Sbostic 			break;
7756039Sbostic 		case '?':
7856039Sbostic 		default:
7956039Sbostic 			usage();
8056039Sbostic 		}
8156039Sbostic 	argc -= optind;
8256039Sbostic 	argv += optind;
8356039Sbostic 
8456039Sbostic 	if (argc != 2)
8556039Sbostic 		usage();
8656039Sbostic 
8756039Sbostic 	/* Set the type. */
8856039Sbostic 	type = dbtype(*argv++);
8956039Sbostic 
9056039Sbostic 	/* Open the descriptor file. */
9156039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
9256039Sbostic 		err("%s: %s", *argv, strerror(errno));
9356039Sbostic 
9456039Sbostic 	/* Set up the db structure as necessary. */
9556039Sbostic 	if (infoarg == NULL)
9656039Sbostic 		infop = NULL;
9756039Sbostic 	else
9856039Sbostic 		while ((p = strsep(&infoarg, ",\t ")) != NULL)
9956039Sbostic 			if (*p != '\0')
10056039Sbostic 				infop = setinfo(type, p);
10156039Sbostic 
10256039Sbostic #define	BACKINGFILE	"/tmp/__dbtest"
10356039Sbostic 	/* Open the DB. */
10456039Sbostic 	(void)unlink(BACKINGFILE);
10556039Sbostic 	if ((dbp = dbopen(BACKINGFILE,
10656039Sbostic 	    O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
10756039Sbostic 		err("dbopen: %s", strerror(errno));
10856039Sbostic 
10956039Sbostic 	state = COMMAND;
11056039Sbostic 	for (lineno = 1;
11156039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
11256039Sbostic 		len = strlen(buf);
11356039Sbostic 		switch(*p) {
11456557Sbostic 		case 'c':			/* compare */
11556557Sbostic 			if (state != COMMAND)
11656557Sbostic 				err("line %lu: not expecting command", lineno);
11756557Sbostic 			state = KEY;
11856557Sbostic 			command = COMPARE;
11956557Sbostic 			break;
12056059Sbostic 		case 'e':			/* echo */
12156059Sbostic 			if (state != COMMAND)
12256059Sbostic 				err("line %lu: not expecting command", lineno);
12356557Sbostic 			/* Don't display the newline, if CR at EOL. */
12456557Sbostic 			if (p[len - 2] == '\r')
12556557Sbostic 				--len;
12656557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
12756557Sbostic 				err("write: %s", strerror(errno));
12856059Sbostic 			break;
12956039Sbostic 		case 'g':			/* get */
13056039Sbostic 			if (state != COMMAND)
13156039Sbostic 				err("line %lu: not expecting command", lineno);
13256039Sbostic 			state = KEY;
13356039Sbostic 			command = GET;
13456039Sbostic 			break;
13556039Sbostic 		case 'p':			/* put */
13656039Sbostic 			if (state != COMMAND)
13756039Sbostic 				err("line %lu: not expecting command", lineno);
13856039Sbostic 			state = KEY;
13956039Sbostic 			command = PUT;
14056039Sbostic 			break;
14156039Sbostic 		case 'r':			/* remove */
14256039Sbostic 			if (state != COMMAND)
14356039Sbostic 				err("line %lu: not expecting command", lineno);
14456039Sbostic 			state = KEY;
14556039Sbostic 			command = REMOVE;
14656039Sbostic 			break;
14756039Sbostic 		case 's':			/* seq */
14856039Sbostic 			if (state != COMMAND)
14956039Sbostic 				err("line %lu: not expecting command", lineno);
15056059Sbostic 			if (flags == R_CURSOR) {
15156059Sbostic 				state = KEY;
15256059Sbostic 				command = SEQ;
15356059Sbostic 			} else
15456059Sbostic 				seq(dbp, &key);
15556039Sbostic 			break;
15656039Sbostic 		case 'f':
15756059Sbostic 			flags = setflags(p + 1);
15856039Sbostic 			break;
15956039Sbostic 		case 'D':			/* data file */
16056039Sbostic 			if (state != DATA)
16156039Sbostic 				err("line %lu: not expecting data", lineno);
16256039Sbostic 			data.data = rfile(p + 1, &data.size);
163*56992Sbostic 			goto ldata;
16456039Sbostic 		case 'd':			/* data */
16556039Sbostic 			if (state != DATA)
16656039Sbostic 				err("line %lu: not expecting data", lineno);
16756557Sbostic 			data.data = xmalloc(p + 1, len - 1);
16856557Sbostic 			data.size = len - 1;
169*56992Sbostic ldata:			switch(command) {
17056557Sbostic 			case COMPARE:
17156557Sbostic 				compare(&keydata, &data);
17256557Sbostic 				break;
17356557Sbostic 			case PUT:
17456557Sbostic 				put(dbp, &key, &data);
17556557Sbostic 				break;
17656557Sbostic 			default:
17756039Sbostic 				err("line %lu: command doesn't take data",
17856039Sbostic 				    lineno);
17956557Sbostic 			}
18056039Sbostic 			free(key.data);
18156039Sbostic 			free(data.data);
18256039Sbostic 			state = COMMAND;
18356039Sbostic 			break;
18456039Sbostic 		case 'K':			/* key file */
18556039Sbostic 			if (state != KEY)
18656039Sbostic 				err("line %lu: not expecting a key", lineno);
18756039Sbostic 			if (type == DB_RECNO)
18856039Sbostic 				err("line %lu: 'K' not available for recno",
18956039Sbostic 				    lineno);
19056039Sbostic 			key.data = rfile(p + 1, &key.size);
191*56992Sbostic 			goto lkey;
19256039Sbostic 		case 'k':			/* key */
19356039Sbostic 			if (state != KEY)
19456039Sbostic 				err("line %lu: not expecting a key", lineno);
19556039Sbostic 			if (type == DB_RECNO) {
19656039Sbostic 				static recno_t recno;
19756039Sbostic 				recno = strtol(p + 1, NULL, 0);
19856039Sbostic 				key.data = &recno;
19956039Sbostic 				key.size = sizeof(recno);
20056039Sbostic 			} else {
20156039Sbostic 				key.data = xmalloc(p + 1, len - 1);
20256039Sbostic 				key.size = len - 1;
20356039Sbostic 			}
204*56992Sbostic lkey:			switch(command) {
20556557Sbostic 			case COMPARE:
20656557Sbostic 				getdata(dbp, &key, &keydata);
20756557Sbostic 				state = DATA;
20856557Sbostic 				break;
20956039Sbostic 			case GET:
21056039Sbostic 				get(dbp, &key);
21156039Sbostic 				if (type != DB_RECNO)
21256039Sbostic 					free(key.data);
21356039Sbostic 				state = COMMAND;
21456039Sbostic 				break;
21556039Sbostic 			case PUT:
21656039Sbostic 				state = DATA;
21756039Sbostic 				break;
21856039Sbostic 			case REMOVE:
21956039Sbostic 				rem(dbp, &key);
22056039Sbostic 				if (type != DB_RECNO)
22156039Sbostic 					free(key.data);
22256039Sbostic 				state = COMMAND;
22356039Sbostic 				break;
22456059Sbostic 			case SEQ:
22556059Sbostic 				seq(dbp, &key);
22656059Sbostic 				if (type != DB_RECNO)
22756059Sbostic 					free(key.data);
22856059Sbostic 				state = COMMAND;
22956059Sbostic 				break;
23056039Sbostic 			default:
23156039Sbostic 				err("line %lu: command doesn't take a key",
23256039Sbostic 				    lineno);
23356039Sbostic 			}
23456039Sbostic 			break;
235*56992Sbostic 		case 'o':
236*56992Sbostic 			dump(dbp, p[1] == 'r');
237*56992Sbostic 			break;
23856039Sbostic 		default:
23956039Sbostic 			err("line %lu: %s: unknown command character",
24056059Sbostic 			    p, lineno);
24156039Sbostic 		}
24256039Sbostic 	}
24356039Sbostic 	(void)close(ofd);
24456039Sbostic 	exit(0);
24556039Sbostic }
24656039Sbostic 
24756059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
24856059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
24956059Sbostic 
25056039Sbostic void
25156557Sbostic compare(db1, db2)
25256557Sbostic 	DBT *db1, *db2;
25356557Sbostic {
25456557Sbostic 	register size_t len;
25556557Sbostic 	register u_char *p1, *p2;
25656557Sbostic 
25756557Sbostic 	if (db1->size != db2->size)
25856557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
25956557Sbostic 		    db1->size, db2->size);
26056557Sbostic 
26156557Sbostic 	len = MIN(db1->size, db2->size);
26256557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
26356557Sbostic 		if (*p1++ != *p2++) {
26456557Sbostic 			printf("compare failed at offset %d\n",
26556557Sbostic 			    p1 - (u_char *)db1->data);
26656557Sbostic 			break;
26756557Sbostic 		}
26856557Sbostic }
26956557Sbostic 
27056557Sbostic void
27156039Sbostic get(dbp, kp)
27256039Sbostic 	DB *dbp;
27356039Sbostic 	DBT *kp;
27456039Sbostic {
27556039Sbostic 	DBT data;
27656039Sbostic 
27756059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
27856059Sbostic 	case 0:
27956059Sbostic 		(void)write(ofd, data.data, data.size);
28056059Sbostic 		break;
28156059Sbostic 	case -1:
28256039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
28356059Sbostic 		/* NOTREACHED */
28456059Sbostic 	case 1:
28556059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
28656059Sbostic 		break;
28756059Sbostic 	}
28856039Sbostic }
28956039Sbostic 
29056039Sbostic void
29156557Sbostic getdata(dbp, kp, dp)
29256557Sbostic 	DB *dbp;
29356557Sbostic 	DBT *kp, *dp;
29456557Sbostic {
29556557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
29656557Sbostic 	case 0:
29756557Sbostic 		return;
29856557Sbostic 	case -1:
29956557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
30056557Sbostic 		/* NOTREACHED */
30156557Sbostic 	case 1:
30256557Sbostic 		err("line %lu: get failed, no such key", lineno);
30356557Sbostic 		/* NOTREACHED */
30456557Sbostic 	}
30556557Sbostic }
30656557Sbostic 
30756557Sbostic void
30856039Sbostic put(dbp, kp, dp)
30956039Sbostic 	DB *dbp;
31056039Sbostic 	DBT *kp, *dp;
31156039Sbostic {
31256059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
31356059Sbostic 	case 0:
31456059Sbostic 		break;
31556059Sbostic 	case -1:
31656039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
31756059Sbostic 		/* NOTREACHED */
31856059Sbostic 	case 1:
31956059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
32056059Sbostic 		break;
32156059Sbostic 	}
32256039Sbostic }
32356039Sbostic 
32456039Sbostic void
32556039Sbostic rem(dbp, kp)
32656039Sbostic 	DB *dbp;
32756039Sbostic 	DBT *kp;
32856039Sbostic {
32956059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
33056059Sbostic 	case 0:
33156059Sbostic 		break;
33256059Sbostic 	case -1:
33356039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
33456059Sbostic 		/* NOTREACHED */
33556059Sbostic 	case 1:
33656059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
33756059Sbostic 		break;
33856059Sbostic 	}
33956039Sbostic }
34056039Sbostic 
34156039Sbostic void
34256059Sbostic seq(dbp, kp)
34356039Sbostic 	DB *dbp;
34456059Sbostic 	DBT *kp;
34556039Sbostic {
34656059Sbostic 	DBT data;
34756039Sbostic 
34856059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
34956059Sbostic 	case 0:
35056059Sbostic 		(void)write(ofd, data.data, data.size);
35156059Sbostic 		break;
35256059Sbostic 	case -1:
35356039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
35456059Sbostic 		/* NOTREACHED */
35556059Sbostic 	case 1:
35656059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
35756059Sbostic 		break;
35856059Sbostic 	}
35956039Sbostic }
360*56992Sbostic 
361*56992Sbostic void
362*56992Sbostic dump(dbp, rev)
363*56992Sbostic 	DB *dbp;
364*56992Sbostic 	int rev;
365*56992Sbostic {
366*56992Sbostic 	DBT key, data;
367*56992Sbostic 	int flags, nflags;
368*56992Sbostic 
369*56992Sbostic 	if (rev) {
370*56992Sbostic 		flags = R_LAST;
371*56992Sbostic 		nflags = R_PREV;
372*56992Sbostic 	} else {
373*56992Sbostic 		flags = R_FIRST;
374*56992Sbostic 		nflags = R_NEXT;
375*56992Sbostic 	}
376*56992Sbostic 	for (;; flags = nflags)
377*56992Sbostic 		switch(dbp->seq(dbp, &key, &data, flags)) {
378*56992Sbostic 		case 0:
379*56992Sbostic 			(void)write(ofd, data.data, data.size);
380*56992Sbostic 			break;
381*56992Sbostic 		case 1:
382*56992Sbostic 			goto done;
383*56992Sbostic 		case -1:
384*56992Sbostic 			err("line %lu: (dump) seq: %s",
385*56992Sbostic 			    lineno, strerror(errno));
386*56992Sbostic 			/* NOTREACHED */
387*56992Sbostic 		}
388*56992Sbostic done:	return;
389*56992Sbostic }
39056039Sbostic 
39156039Sbostic u_int
39256039Sbostic setflags(s)
39356039Sbostic 	char *s;
39456039Sbostic {
39556039Sbostic 	char *p;
39656039Sbostic 
39756039Sbostic 	for (; isspace(*s); ++s);
39856039Sbostic 	if (*s == '\n')
39956039Sbostic 		return (0);
40056039Sbostic 	if ((p = index(s, '\n')) != NULL)
40156039Sbostic 		*p = '\0';
40256039Sbostic 	if (!strcmp(s, "R_CURSOR"))
40356039Sbostic 		return (R_CURSOR);
40456749Sbostic 	if (!strcmp(s, "R_CURSORLOG"))
40556749Sbostic 		return (R_CURSORLOG);
40656749Sbostic 	if (!strcmp(s, "R_FIRST"))
40756749Sbostic 		return (R_FIRST);
40856039Sbostic 	if (!strcmp(s, "R_IAFTER"))
40956039Sbostic 		return (R_IAFTER);
41056039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
41156039Sbostic 		return (R_IBEFORE);
41256039Sbostic 	if (!strcmp(s, "R_LAST"))
41356039Sbostic 		return (R_LAST);
41456039Sbostic 	if (!strcmp(s, "R_NEXT"))
41556039Sbostic 		return (R_NEXT);
41656749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
41756749Sbostic 		return (R_NOOVERWRITE);
41856039Sbostic 	if (!strcmp(s, "R_PREV"))
41956039Sbostic 		return (R_PREV);
42056749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
42156749Sbostic 		return (R_SETCURSOR);
42256039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
42356039Sbostic 	/* NOTREACHED */
42456039Sbostic }
42556039Sbostic 
42656039Sbostic DBTYPE
42756039Sbostic dbtype(s)
42856039Sbostic 	char *s;
42956039Sbostic {
43056039Sbostic 	if (!strcmp(s, "btree"))
43156039Sbostic 		return (DB_BTREE);
43256039Sbostic 	if (!strcmp(s, "hash"))
43356039Sbostic 		return (DB_HASH);
43456039Sbostic 	if (!strcmp(s, "recno"))
43556039Sbostic 		return (DB_RECNO);
43656039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
43756039Sbostic 	/* NOTREACHED */
43856039Sbostic }
43956039Sbostic 
44056039Sbostic void *
44156039Sbostic setinfo(type, s)
44256039Sbostic 	DBTYPE type;
44356039Sbostic 	char *s;
44456039Sbostic {
44556039Sbostic 	static BTREEINFO ib;
44656039Sbostic 	static HASHINFO ih;
44756039Sbostic 	static RECNOINFO rh;
44856039Sbostic 	char *eq;
44956039Sbostic 
45056039Sbostic 	if ((eq = index(s, '=')) == NULL)
45156039Sbostic 		err("%s: illegal structure set statement", s);
45256039Sbostic 	*eq++ = '\0';
45356039Sbostic 	if (!isdigit(*eq))
45456039Sbostic 		err("%s: structure set statement must be a number", s);
45556039Sbostic 
45656039Sbostic 	switch(type) {
45756039Sbostic 	case DB_BTREE:
45856039Sbostic 		if (!strcmp("flags", s)) {
45956039Sbostic 			ib.flags = strtoul(eq, NULL, 0);
46056039Sbostic 			return (&ib);
46156039Sbostic 		}
46256039Sbostic 		if (!strcmp("cachesize", s)) {
46356039Sbostic 			ib.cachesize = strtoul(eq, NULL, 0);
46456039Sbostic 			return (&ib);
46556039Sbostic 		}
46656039Sbostic 		if (!strcmp("maxkeypage", s)) {
46756039Sbostic 			ib.maxkeypage = strtoul(eq, NULL, 0);
46856039Sbostic 			return (&ib);
46956039Sbostic 		}
47056039Sbostic 		if (!strcmp("minkeypage", s)) {
47156039Sbostic 			ib.minkeypage = strtoul(eq, NULL, 0);
47256039Sbostic 			return (&ib);
47356039Sbostic 		}
47456039Sbostic 		if (!strcmp("lorder", s)) {
47556039Sbostic 			ib.lorder = strtoul(eq, NULL, 0);
47656039Sbostic 			return (&ib);
47756039Sbostic 		}
47856039Sbostic 		break;
47956039Sbostic 	case DB_HASH:
48056039Sbostic 		if (!strcmp("bsize", s)) {
48156039Sbostic 			ih.bsize = strtoul(eq, NULL, 0);
48256039Sbostic 			return (&ib);
48356039Sbostic 		}
48456039Sbostic 		if (!strcmp("ffactor", s)) {
48556039Sbostic 			ih.ffactor = strtoul(eq, NULL, 0);
48656039Sbostic 			return (&ib);
48756039Sbostic 		}
48856039Sbostic 		if (!strcmp("nelem", s)) {
48956039Sbostic 			ih.nelem = strtoul(eq, NULL, 0);
49056039Sbostic 			return (&ib);
49156039Sbostic 		}
49256039Sbostic 		if (!strcmp("cachesize", s)) {
49356039Sbostic 			ih.cachesize = strtoul(eq, NULL, 0);
49456039Sbostic 			return (&ib);
49556039Sbostic 		}
49656039Sbostic 		if (!strcmp("lorder", s)) {
49756039Sbostic 			ih.lorder = strtoul(eq, NULL, 0);
49856039Sbostic 			return (&ib);
49956039Sbostic 		}
50056039Sbostic 		break;
50156039Sbostic 	case DB_RECNO:
50256039Sbostic 		if (!strcmp("flags", s)) {
50356039Sbostic 			rh.flags = strtoul(eq, NULL, 0);
50456039Sbostic 			return (&ib);
50556039Sbostic 		}
50656039Sbostic 		if (!strcmp("cachesize", s)) {
50756039Sbostic 			rh.cachesize = strtoul(eq, NULL, 0);
50856039Sbostic 			return (&ib);
50956039Sbostic 		}
51056039Sbostic 		if (!strcmp("lorder", s)) {
51156039Sbostic 			rh.lorder = strtoul(eq, NULL, 0);
51256039Sbostic 			return (&ib);
51356039Sbostic 		}
51456039Sbostic 		if (!strcmp("reclen", s)) {
51556039Sbostic 			rh.reclen = strtoul(eq, NULL, 0);
51656039Sbostic 			return (&ib);
51756039Sbostic 		}
51856039Sbostic 		if (!strcmp("bval", s)) {
51956039Sbostic 			rh.bval = strtoul(eq, NULL, 0);
52056039Sbostic 			return (&ib);
52156039Sbostic 		}
52256039Sbostic 		break;
52356039Sbostic 	}
52456039Sbostic 	err("%s: unknown structure value", s);
52556039Sbostic 	/* NOTREACHED */
52656039Sbostic }
52756039Sbostic 
52856039Sbostic void *
52956039Sbostic rfile(name, lenp)
53056039Sbostic 	char *name;
53156039Sbostic 	size_t *lenp;
53256039Sbostic {
53356039Sbostic 	struct stat sb;
53456039Sbostic 	void *p;
53556039Sbostic 	int fd;
53656039Sbostic 	char *np;
53756039Sbostic 
53856039Sbostic 	for (; isspace(*name); ++name);
53956039Sbostic 	if ((np = index(name, '\n')) != NULL)
54056039Sbostic 		*np = '\0';
54156039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
54256039Sbostic 	    fstat(fd, &sb))
54356039Sbostic 		err("%s: %s\n", name, strerror(errno));
54456039Sbostic 	if (sb.st_size > SIZE_T_MAX)
54556039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
54656039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
54756039Sbostic 		err("%s", strerror(errno));
54856039Sbostic 	(void)read(fd, p, (int)sb.st_size);
54956039Sbostic 	*lenp = sb.st_size;
55056489Sbostic 	(void)close(fd);
55156039Sbostic 	return (p);
55256039Sbostic }
55356039Sbostic 
55456039Sbostic void *
55556039Sbostic xmalloc(text, len)
55656039Sbostic 	char *text;
55756039Sbostic 	size_t len;
55856039Sbostic {
55956039Sbostic 	void *p;
56056039Sbostic 
56156039Sbostic 	if ((p = malloc(len)) == NULL)
56256039Sbostic 		err("%s", strerror(errno));
56356039Sbostic 	bcopy(text, p, len);
56456039Sbostic 	return (p);
56556039Sbostic }
56656039Sbostic 
56756039Sbostic void
56856039Sbostic usage()
56956039Sbostic {
57056039Sbostic 	(void)fprintf(stderr,
57156039Sbostic 	    "usage: dbtest [-i info] [-o file] type script\n");
57256039Sbostic 	exit(1);
57356039Sbostic }
57456039Sbostic 
57556039Sbostic #if __STDC__
57656039Sbostic #include <stdarg.h>
57756039Sbostic #else
57856039Sbostic #include <varargs.h>
57956039Sbostic #endif
58056039Sbostic 
58156039Sbostic void
58256039Sbostic #if __STDC__
58356039Sbostic err(const char *fmt, ...)
58456039Sbostic #else
58556039Sbostic err(fmt, va_alist)
58656039Sbostic 	char *fmt;
58756039Sbostic         va_dcl
58856039Sbostic #endif
58956039Sbostic {
59056039Sbostic 	va_list ap;
59156039Sbostic #if __STDC__
59256039Sbostic 	va_start(ap, fmt);
59356039Sbostic #else
59456039Sbostic 	va_start(ap);
59556039Sbostic #endif
59656039Sbostic 	(void)fprintf(stderr, "dbtest: ");
59756039Sbostic 	(void)vfprintf(stderr, fmt, ap);
59856039Sbostic 	va_end(ap);
59956039Sbostic 	(void)fprintf(stderr, "\n");
60056039Sbostic 	exit(1);
60156039Sbostic 	/* NOTREACHED */
60256039Sbostic }
603