xref: /csrg-svn/lib/libc/db/test/dbtest.c (revision 56749)
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*56749Sbostic static char sccsid[] = "@(#)dbtest.c	5.5 (Berkeley) 11/13/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 *));
3556039Sbostic void	 err __P((const char *, ...));
3656039Sbostic void	 get __P((DB *, DBT *));
3756557Sbostic void	 getdata __P((DB *, DBT *, DBT *));
3856039Sbostic void	 put __P((DB *, DBT *, DBT *));
3956039Sbostic void	 rem __P((DB *, DBT *));
4056039Sbostic void	*rfile __P((char *, size_t *));
4156059Sbostic void	 seq __P((DB *, DBT *));
4256039Sbostic u_int	 setflags __P((char *));
4356039Sbostic void	*setinfo __P((DBTYPE, char *));
4456039Sbostic void	 usage __P((void));
4556039Sbostic void	*xmalloc __P((char *, size_t));
4656039Sbostic 
4756039Sbostic DBTYPE type;
4856039Sbostic void *infop;
4956039Sbostic u_long lineno;
5056039Sbostic u_int flags;
5156039Sbostic int ofd = STDOUT_FILENO;
5256039Sbostic 
5356039Sbostic int
5456039Sbostic main(argc, argv)
5556039Sbostic 	int argc;
5656039Sbostic 	char *argv[];
5756039Sbostic {
5856039Sbostic 	enum S command, state;
5956039Sbostic 	DB *dbp;
6056557Sbostic 	DBT data, key, keydata;
6156039Sbostic 	size_t len;
6256039Sbostic 	int ch;
6356039Sbostic 	char *infoarg, *p, buf[8 * 1024];
6456039Sbostic 
6556039Sbostic 	infoarg = NULL;
6656039Sbostic 	while ((ch = getopt(argc, argv, "i:o:")) != EOF)
6756039Sbostic 		switch(ch) {
6856039Sbostic 		case 'i':
6956039Sbostic 			infoarg = optarg;
7056039Sbostic 			break;
7156039Sbostic 		case 'o':
7256039Sbostic 			if ((ofd = open(optarg,
7356039Sbostic 			    O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
7456039Sbostic 				err("%s: %s", optarg, strerror(errno));
7556039Sbostic 			break;
7656039Sbostic 		case '?':
7756039Sbostic 		default:
7856039Sbostic 			usage();
7956039Sbostic 		}
8056039Sbostic 	argc -= optind;
8156039Sbostic 	argv += optind;
8256039Sbostic 
8356039Sbostic 	if (argc != 2)
8456039Sbostic 		usage();
8556039Sbostic 
8656039Sbostic 	/* Set the type. */
8756039Sbostic 	type = dbtype(*argv++);
8856039Sbostic 
8956039Sbostic 	/* Open the descriptor file. */
9056039Sbostic 	if (freopen(*argv, "r", stdin) == NULL)
9156039Sbostic 		err("%s: %s", *argv, strerror(errno));
9256039Sbostic 
9356039Sbostic 	/* Set up the db structure as necessary. */
9456039Sbostic 	if (infoarg == NULL)
9556039Sbostic 		infop = NULL;
9656039Sbostic 	else
9756039Sbostic 		while ((p = strsep(&infoarg, ",\t ")) != NULL)
9856039Sbostic 			if (*p != '\0')
9956039Sbostic 				infop = setinfo(type, p);
10056039Sbostic 
10156039Sbostic #define	BACKINGFILE	"/tmp/__dbtest"
10256039Sbostic 	/* Open the DB. */
10356039Sbostic 	(void)unlink(BACKINGFILE);
10456039Sbostic 	if ((dbp = dbopen(BACKINGFILE,
10556039Sbostic 	    O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
10656039Sbostic 		err("dbopen: %s", strerror(errno));
10756039Sbostic 
10856039Sbostic 	state = COMMAND;
10956039Sbostic 	for (lineno = 1;
11056039Sbostic 	    (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
11156039Sbostic 		len = strlen(buf);
11256039Sbostic 		switch(*p) {
11356557Sbostic 		case 'c':			/* compare */
11456557Sbostic 			if (state != COMMAND)
11556557Sbostic 				err("line %lu: not expecting command", lineno);
11656557Sbostic 			state = KEY;
11756557Sbostic 			command = COMPARE;
11856557Sbostic 			break;
11956059Sbostic 		case 'e':			/* echo */
12056059Sbostic 			if (state != COMMAND)
12156059Sbostic 				err("line %lu: not expecting command", lineno);
12256557Sbostic 			/* Don't display the newline, if CR at EOL. */
12356557Sbostic 			if (p[len - 2] == '\r')
12456557Sbostic 				--len;
12556557Sbostic 			if (write(ofd, p + 1, len - 1) != len - 1)
12656557Sbostic 				err("write: %s", strerror(errno));
12756059Sbostic 			break;
12856039Sbostic 		case 'g':			/* get */
12956039Sbostic 			if (state != COMMAND)
13056039Sbostic 				err("line %lu: not expecting command", lineno);
13156039Sbostic 			state = KEY;
13256039Sbostic 			command = GET;
13356039Sbostic 			break;
13456039Sbostic 		case 'p':			/* put */
13556039Sbostic 			if (state != COMMAND)
13656039Sbostic 				err("line %lu: not expecting command", lineno);
13756039Sbostic 			state = KEY;
13856039Sbostic 			command = PUT;
13956039Sbostic 			break;
14056039Sbostic 		case 'r':			/* remove */
14156039Sbostic 			if (state != COMMAND)
14256039Sbostic 				err("line %lu: not expecting command", lineno);
14356039Sbostic 			state = KEY;
14456039Sbostic 			command = REMOVE;
14556039Sbostic 			break;
14656039Sbostic 		case 's':			/* seq */
14756039Sbostic 			if (state != COMMAND)
14856039Sbostic 				err("line %lu: not expecting command", lineno);
14956059Sbostic 			if (flags == R_CURSOR) {
15056059Sbostic 				state = KEY;
15156059Sbostic 				command = SEQ;
15256059Sbostic 			} else
15356059Sbostic 				seq(dbp, &key);
15456039Sbostic 			break;
15556039Sbostic 		case 'f':
15656059Sbostic 			flags = setflags(p + 1);
15756039Sbostic 			break;
15856039Sbostic 		case 'D':			/* data file */
15956039Sbostic 			if (state != DATA)
16056039Sbostic 				err("line %lu: not expecting data", lineno);
16156039Sbostic 			data.data = rfile(p + 1, &data.size);
16256557Sbostic 			goto data;
16356039Sbostic 		case 'd':			/* data */
16456039Sbostic 			if (state != DATA)
16556039Sbostic 				err("line %lu: not expecting data", lineno);
16656557Sbostic 			data.data = xmalloc(p + 1, len - 1);
16756557Sbostic 			data.size = len - 1;
16856557Sbostic data:			switch(command) {
16956557Sbostic 			case COMPARE:
17056557Sbostic 				compare(&keydata, &data);
17156557Sbostic 				break;
17256557Sbostic 			case PUT:
17356557Sbostic 				put(dbp, &key, &data);
17456557Sbostic 				break;
17556557Sbostic 			default:
17656039Sbostic 				err("line %lu: command doesn't take data",
17756039Sbostic 				    lineno);
17856557Sbostic 			}
17956039Sbostic 			free(key.data);
18056039Sbostic 			free(data.data);
18156039Sbostic 			state = COMMAND;
18256039Sbostic 			break;
18356039Sbostic 		case 'K':			/* key file */
18456039Sbostic 			if (state != KEY)
18556039Sbostic 				err("line %lu: not expecting a key", lineno);
18656039Sbostic 			if (type == DB_RECNO)
18756039Sbostic 				err("line %lu: 'K' not available for recno",
18856039Sbostic 				    lineno);
18956039Sbostic 			key.data = rfile(p + 1, &key.size);
19056059Sbostic 			goto key;
19156039Sbostic 		case 'k':			/* key */
19256039Sbostic 			if (state != KEY)
19356039Sbostic 				err("line %lu: not expecting a key", lineno);
19456039Sbostic 			if (type == DB_RECNO) {
19556039Sbostic 				static recno_t recno;
19656039Sbostic 				recno = strtol(p + 1, NULL, 0);
19756039Sbostic 				key.data = &recno;
19856039Sbostic 				key.size = sizeof(recno);
19956039Sbostic 			} else {
20056039Sbostic 				key.data = xmalloc(p + 1, len - 1);
20156039Sbostic 				key.size = len - 1;
20256039Sbostic 			}
20356059Sbostic key:			switch(command) {
20456557Sbostic 			case COMPARE:
20556557Sbostic 				getdata(dbp, &key, &keydata);
20656557Sbostic 				state = DATA;
20756557Sbostic 				break;
20856039Sbostic 			case GET:
20956039Sbostic 				get(dbp, &key);
21056039Sbostic 				if (type != DB_RECNO)
21156039Sbostic 					free(key.data);
21256039Sbostic 				state = COMMAND;
21356039Sbostic 				break;
21456039Sbostic 			case PUT:
21556039Sbostic 				state = DATA;
21656039Sbostic 				break;
21756039Sbostic 			case REMOVE:
21856039Sbostic 				rem(dbp, &key);
21956039Sbostic 				if (type != DB_RECNO)
22056039Sbostic 					free(key.data);
22156039Sbostic 				state = COMMAND;
22256039Sbostic 				break;
22356059Sbostic 			case SEQ:
22456059Sbostic 				seq(dbp, &key);
22556059Sbostic 				if (type != DB_RECNO)
22656059Sbostic 					free(key.data);
22756059Sbostic 				state = COMMAND;
22856059Sbostic 				break;
22956039Sbostic 			default:
23056039Sbostic 				err("line %lu: command doesn't take a key",
23156039Sbostic 				    lineno);
23256039Sbostic 			}
23356039Sbostic 			break;
23456039Sbostic 		default:
23556039Sbostic 			err("line %lu: %s: unknown command character",
23656059Sbostic 			    p, lineno);
23756039Sbostic 		}
23856039Sbostic 	}
23956039Sbostic 	(void)close(ofd);
24056039Sbostic 	exit(0);
24156039Sbostic }
24256039Sbostic 
24356059Sbostic #define	NOOVERWRITE	"put failed, would overwrite key\n"
24456059Sbostic #define	NOSUCHKEY	"get failed, no such key\n"
24556059Sbostic 
24656039Sbostic void
24756557Sbostic compare(db1, db2)
24856557Sbostic 	DBT *db1, *db2;
24956557Sbostic {
25056557Sbostic 	register size_t len;
25156557Sbostic 	register u_char *p1, *p2;
25256557Sbostic 
25356557Sbostic 	if (db1->size != db2->size)
25456557Sbostic 		printf("compare failed: key->data len %lu != data len %lu\n",
25556557Sbostic 		    db1->size, db2->size);
25656557Sbostic 
25756557Sbostic 	len = MIN(db1->size, db2->size);
25856557Sbostic 	for (p1 = db1->data, p2 = db2->data; len--;)
25956557Sbostic 		if (*p1++ != *p2++) {
26056557Sbostic 			printf("compare failed at offset %d\n",
26156557Sbostic 			    p1 - (u_char *)db1->data);
26256557Sbostic 			break;
26356557Sbostic 		}
26456557Sbostic }
26556557Sbostic 
26656557Sbostic void
26756039Sbostic get(dbp, kp)
26856039Sbostic 	DB *dbp;
26956039Sbostic 	DBT *kp;
27056039Sbostic {
27156039Sbostic 	DBT data;
27256039Sbostic 
27356059Sbostic 	switch(dbp->get(dbp, kp, &data, flags)) {
27456059Sbostic 	case 0:
27556059Sbostic 		(void)write(ofd, data.data, data.size);
27656059Sbostic 		break;
27756059Sbostic 	case -1:
27856039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
27956059Sbostic 		/* NOTREACHED */
28056059Sbostic 	case 1:
28156059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
28256059Sbostic 		break;
28356059Sbostic 	}
28456039Sbostic }
28556039Sbostic 
28656039Sbostic void
28756557Sbostic getdata(dbp, kp, dp)
28856557Sbostic 	DB *dbp;
28956557Sbostic 	DBT *kp, *dp;
29056557Sbostic {
29156557Sbostic 	switch(dbp->get(dbp, kp, dp, flags)) {
29256557Sbostic 	case 0:
29356557Sbostic 		return;
29456557Sbostic 	case -1:
29556557Sbostic 		err("line %lu: getdata: %s", lineno, strerror(errno));
29656557Sbostic 		/* NOTREACHED */
29756557Sbostic 	case 1:
29856557Sbostic 		err("line %lu: get failed, no such key", lineno);
29956557Sbostic 		/* NOTREACHED */
30056557Sbostic 	}
30156557Sbostic }
30256557Sbostic 
30356557Sbostic void
30456039Sbostic put(dbp, kp, dp)
30556039Sbostic 	DB *dbp;
30656039Sbostic 	DBT *kp, *dp;
30756039Sbostic {
30856059Sbostic 	switch(dbp->put(dbp, kp, dp, flags)) {
30956059Sbostic 	case 0:
31056059Sbostic 		break;
31156059Sbostic 	case -1:
31256039Sbostic 		err("line %lu: put: %s", lineno, strerror(errno));
31356059Sbostic 		/* NOTREACHED */
31456059Sbostic 	case 1:
31556059Sbostic 		(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
31656059Sbostic 		break;
31756059Sbostic 	}
31856039Sbostic }
31956039Sbostic 
32056039Sbostic void
32156039Sbostic rem(dbp, kp)
32256039Sbostic 	DB *dbp;
32356039Sbostic 	DBT *kp;
32456039Sbostic {
32556059Sbostic 	switch(dbp->del(dbp, kp, flags)) {
32656059Sbostic 	case 0:
32756059Sbostic 		break;
32856059Sbostic 	case -1:
32956039Sbostic 		err("line %lu: get: %s", lineno, strerror(errno));
33056059Sbostic 		/* NOTREACHED */
33156059Sbostic 	case 1:
33256059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
33356059Sbostic 		break;
33456059Sbostic 	}
33556039Sbostic }
33656039Sbostic 
33756039Sbostic void
33856059Sbostic seq(dbp, kp)
33956039Sbostic 	DB *dbp;
34056059Sbostic 	DBT *kp;
34156039Sbostic {
34256059Sbostic 	DBT data;
34356039Sbostic 
34456059Sbostic 	switch(dbp->seq(dbp, kp, &data, flags)) {
34556059Sbostic 	case 0:
34656059Sbostic 		(void)write(ofd, data.data, data.size);
34756059Sbostic 		break;
34856059Sbostic 	case -1:
34956039Sbostic 		err("line %lu: seq: %s", lineno, strerror(errno));
35056059Sbostic 		/* NOTREACHED */
35156059Sbostic 	case 1:
35256059Sbostic 		(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
35356059Sbostic 		break;
35456059Sbostic 	}
35556039Sbostic }
35656039Sbostic 
35756039Sbostic u_int
35856039Sbostic setflags(s)
35956039Sbostic 	char *s;
36056039Sbostic {
36156039Sbostic 	char *p;
36256039Sbostic 
36356039Sbostic 	for (; isspace(*s); ++s);
36456039Sbostic 	if (*s == '\n')
36556039Sbostic 		return (0);
36656039Sbostic 	if ((p = index(s, '\n')) != NULL)
36756039Sbostic 		*p = '\0';
36856039Sbostic 	if (!strcmp(s, "R_CURSOR"))
36956039Sbostic 		return (R_CURSOR);
370*56749Sbostic 	if (!strcmp(s, "R_CURSORLOG"))
371*56749Sbostic 		return (R_CURSORLOG);
372*56749Sbostic 	if (!strcmp(s, "R_FIRST"))
373*56749Sbostic 		return (R_FIRST);
37456039Sbostic 	if (!strcmp(s, "R_IAFTER"))
37556039Sbostic 		return (R_IAFTER);
37656039Sbostic 	if (!strcmp(s, "R_IBEFORE"))
37756039Sbostic 		return (R_IBEFORE);
37856039Sbostic 	if (!strcmp(s, "R_LAST"))
37956039Sbostic 		return (R_LAST);
38056039Sbostic 	if (!strcmp(s, "R_NEXT"))
38156039Sbostic 		return (R_NEXT);
382*56749Sbostic 	if (!strcmp(s, "R_NOOVERWRITE"))
383*56749Sbostic 		return (R_NOOVERWRITE);
38456039Sbostic 	if (!strcmp(s, "R_PREV"))
38556039Sbostic 		return (R_PREV);
386*56749Sbostic 	if (!strcmp(s, "R_SETCURSOR"))
387*56749Sbostic 		return (R_SETCURSOR);
38856039Sbostic 	err("line %lu: %s: unknown flag", lineno, s);
38956039Sbostic 	/* NOTREACHED */
39056039Sbostic }
39156039Sbostic 
39256039Sbostic DBTYPE
39356039Sbostic dbtype(s)
39456039Sbostic 	char *s;
39556039Sbostic {
39656039Sbostic 	if (!strcmp(s, "btree"))
39756039Sbostic 		return (DB_BTREE);
39856039Sbostic 	if (!strcmp(s, "hash"))
39956039Sbostic 		return (DB_HASH);
40056039Sbostic 	if (!strcmp(s, "recno"))
40156039Sbostic 		return (DB_RECNO);
40256039Sbostic 	err("%s: unknown type (use btree, hash or recno)", s);
40356039Sbostic 	/* NOTREACHED */
40456039Sbostic }
40556039Sbostic 
40656039Sbostic void *
40756039Sbostic setinfo(type, s)
40856039Sbostic 	DBTYPE type;
40956039Sbostic 	char *s;
41056039Sbostic {
41156039Sbostic 	static BTREEINFO ib;
41256039Sbostic 	static HASHINFO ih;
41356039Sbostic 	static RECNOINFO rh;
41456039Sbostic 	char *eq;
41556039Sbostic 
41656039Sbostic 	if ((eq = index(s, '=')) == NULL)
41756039Sbostic 		err("%s: illegal structure set statement", s);
41856039Sbostic 	*eq++ = '\0';
41956039Sbostic 	if (!isdigit(*eq))
42056039Sbostic 		err("%s: structure set statement must be a number", s);
42156039Sbostic 
42256039Sbostic 	switch(type) {
42356039Sbostic 	case DB_BTREE:
42456039Sbostic 		if (!strcmp("flags", s)) {
42556039Sbostic 			ib.flags = strtoul(eq, NULL, 0);
42656039Sbostic 			return (&ib);
42756039Sbostic 		}
42856039Sbostic 		if (!strcmp("cachesize", s)) {
42956039Sbostic 			ib.cachesize = strtoul(eq, NULL, 0);
43056039Sbostic 			return (&ib);
43156039Sbostic 		}
43256039Sbostic 		if (!strcmp("maxkeypage", s)) {
43356039Sbostic 			ib.maxkeypage = strtoul(eq, NULL, 0);
43456039Sbostic 			return (&ib);
43556039Sbostic 		}
43656039Sbostic 		if (!strcmp("minkeypage", s)) {
43756039Sbostic 			ib.minkeypage = strtoul(eq, NULL, 0);
43856039Sbostic 			return (&ib);
43956039Sbostic 		}
44056039Sbostic 		if (!strcmp("lorder", s)) {
44156039Sbostic 			ib.lorder = strtoul(eq, NULL, 0);
44256039Sbostic 			return (&ib);
44356039Sbostic 		}
44456039Sbostic 		break;
44556039Sbostic 	case DB_HASH:
44656039Sbostic 		if (!strcmp("bsize", s)) {
44756039Sbostic 			ih.bsize = strtoul(eq, NULL, 0);
44856039Sbostic 			return (&ib);
44956039Sbostic 		}
45056039Sbostic 		if (!strcmp("ffactor", s)) {
45156039Sbostic 			ih.ffactor = strtoul(eq, NULL, 0);
45256039Sbostic 			return (&ib);
45356039Sbostic 		}
45456039Sbostic 		if (!strcmp("nelem", s)) {
45556039Sbostic 			ih.nelem = strtoul(eq, NULL, 0);
45656039Sbostic 			return (&ib);
45756039Sbostic 		}
45856039Sbostic 		if (!strcmp("cachesize", s)) {
45956039Sbostic 			ih.cachesize = strtoul(eq, NULL, 0);
46056039Sbostic 			return (&ib);
46156039Sbostic 		}
46256039Sbostic 		if (!strcmp("lorder", s)) {
46356039Sbostic 			ih.lorder = strtoul(eq, NULL, 0);
46456039Sbostic 			return (&ib);
46556039Sbostic 		}
46656039Sbostic 		break;
46756039Sbostic 	case DB_RECNO:
46856039Sbostic 		if (!strcmp("flags", s)) {
46956039Sbostic 			rh.flags = strtoul(eq, NULL, 0);
47056039Sbostic 			return (&ib);
47156039Sbostic 		}
47256039Sbostic 		if (!strcmp("cachesize", s)) {
47356039Sbostic 			rh.cachesize = strtoul(eq, NULL, 0);
47456039Sbostic 			return (&ib);
47556039Sbostic 		}
47656039Sbostic 		if (!strcmp("lorder", s)) {
47756039Sbostic 			rh.lorder = strtoul(eq, NULL, 0);
47856039Sbostic 			return (&ib);
47956039Sbostic 		}
48056039Sbostic 		if (!strcmp("reclen", s)) {
48156039Sbostic 			rh.reclen = strtoul(eq, NULL, 0);
48256039Sbostic 			return (&ib);
48356039Sbostic 		}
48456039Sbostic 		if (!strcmp("bval", s)) {
48556039Sbostic 			rh.bval = strtoul(eq, NULL, 0);
48656039Sbostic 			return (&ib);
48756039Sbostic 		}
48856039Sbostic 		break;
48956039Sbostic 	}
49056039Sbostic 	err("%s: unknown structure value", s);
49156039Sbostic 	/* NOTREACHED */
49256039Sbostic }
49356039Sbostic 
49456039Sbostic void *
49556039Sbostic rfile(name, lenp)
49656039Sbostic 	char *name;
49756039Sbostic 	size_t *lenp;
49856039Sbostic {
49956039Sbostic 	struct stat sb;
50056039Sbostic 	void *p;
50156039Sbostic 	int fd;
50256039Sbostic 	char *np;
50356039Sbostic 
50456039Sbostic 	for (; isspace(*name); ++name);
50556039Sbostic 	if ((np = index(name, '\n')) != NULL)
50656039Sbostic 		*np = '\0';
50756039Sbostic 	if ((fd = open(name, O_RDONLY, 0)) < 0 ||
50856039Sbostic 	    fstat(fd, &sb))
50956039Sbostic 		err("%s: %s\n", name, strerror(errno));
51056039Sbostic 	if (sb.st_size > SIZE_T_MAX)
51156039Sbostic 		err("%s: %s\n", name, strerror(E2BIG));
51256039Sbostic 	if ((p = malloc((u_int)sb.st_size)) == NULL)
51356039Sbostic 		err("%s", strerror(errno));
51456039Sbostic 	(void)read(fd, p, (int)sb.st_size);
51556039Sbostic 	*lenp = sb.st_size;
51656489Sbostic 	(void)close(fd);
51756039Sbostic 	return (p);
51856039Sbostic }
51956039Sbostic 
52056039Sbostic void *
52156039Sbostic xmalloc(text, len)
52256039Sbostic 	char *text;
52356039Sbostic 	size_t len;
52456039Sbostic {
52556039Sbostic 	void *p;
52656039Sbostic 
52756039Sbostic 	if ((p = malloc(len)) == NULL)
52856039Sbostic 		err("%s", strerror(errno));
52956039Sbostic 	bcopy(text, p, len);
53056039Sbostic 	return (p);
53156039Sbostic }
53256039Sbostic 
53356039Sbostic void
53456039Sbostic usage()
53556039Sbostic {
53656039Sbostic 	(void)fprintf(stderr,
53756039Sbostic 	    "usage: dbtest [-i info] [-o file] type script\n");
53856039Sbostic 	exit(1);
53956039Sbostic }
54056039Sbostic 
54156039Sbostic #if __STDC__
54256039Sbostic #include <stdarg.h>
54356039Sbostic #else
54456039Sbostic #include <varargs.h>
54556039Sbostic #endif
54656039Sbostic 
54756039Sbostic void
54856039Sbostic #if __STDC__
54956039Sbostic err(const char *fmt, ...)
55056039Sbostic #else
55156039Sbostic err(fmt, va_alist)
55256039Sbostic 	char *fmt;
55356039Sbostic         va_dcl
55456039Sbostic #endif
55556039Sbostic {
55656039Sbostic 	va_list ap;
55756039Sbostic #if __STDC__
55856039Sbostic 	va_start(ap, fmt);
55956039Sbostic #else
56056039Sbostic 	va_start(ap);
56156039Sbostic #endif
56256039Sbostic 	(void)fprintf(stderr, "dbtest: ");
56356039Sbostic 	(void)vfprintf(stderr, fmt, ap);
56456039Sbostic 	va_end(ap);
56556039Sbostic 	(void)fprintf(stderr, "\n");
56656039Sbostic 	exit(1);
56756039Sbostic 	/* NOTREACHED */
56856039Sbostic }
569