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*59492Sbostic static char sccsid[] = "@(#)dbtest.c 5.12 (Berkeley) 04/29/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 30*59492Sbostic #include <db.h> 31*59492Sbostic 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; 6956039Sbostic char *infoarg, *p, buf[8 * 1024]; 7056039Sbostic 7156039Sbostic infoarg = NULL; 7256039Sbostic while ((ch = getopt(argc, argv, "i:o:")) != EOF) 7356039Sbostic switch(ch) { 7456039Sbostic case 'i': 7556039Sbostic infoarg = optarg; 7656039Sbostic break; 7756039Sbostic case 'o': 7856039Sbostic if ((ofd = open(optarg, 7956039Sbostic O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 8056039Sbostic err("%s: %s", optarg, strerror(errno)); 8156039Sbostic break; 8256039Sbostic case '?': 8356039Sbostic default: 8456039Sbostic usage(); 8556039Sbostic } 8656039Sbostic argc -= optind; 8756039Sbostic argv += optind; 8856039Sbostic 8956039Sbostic if (argc != 2) 9056039Sbostic usage(); 9156039Sbostic 9256039Sbostic /* Set the type. */ 9356039Sbostic type = dbtype(*argv++); 9456039Sbostic 9556039Sbostic /* Open the descriptor file. */ 9656039Sbostic if (freopen(*argv, "r", stdin) == NULL) 9756039Sbostic err("%s: %s", *argv, strerror(errno)); 9856039Sbostic 9956039Sbostic /* Set up the db structure as necessary. */ 10056039Sbostic if (infoarg == NULL) 10156039Sbostic infop = NULL; 10256039Sbostic else 10357463Sbostic for (p = strtok(infoarg, ",\t "); p != NULL; 10457463Sbostic p = strtok(0, ",\t ")) 10556039Sbostic if (*p != '\0') 10656039Sbostic infop = setinfo(type, p); 10756039Sbostic 10856039Sbostic #define BACKINGFILE "/tmp/__dbtest" 10956039Sbostic /* Open the DB. */ 11056039Sbostic (void)unlink(BACKINGFILE); 11156039Sbostic if ((dbp = dbopen(BACKINGFILE, 11256039Sbostic O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL) 11356039Sbostic err("dbopen: %s", strerror(errno)); 11457456Sbostic XXdbp = dbp; 11556039Sbostic 11656039Sbostic state = COMMAND; 11756039Sbostic for (lineno = 1; 11856039Sbostic (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) { 11956039Sbostic len = strlen(buf); 12056039Sbostic switch(*p) { 12156557Sbostic case 'c': /* compare */ 12256557Sbostic if (state != COMMAND) 12356557Sbostic err("line %lu: not expecting command", lineno); 12456557Sbostic state = KEY; 12556557Sbostic command = COMPARE; 12656557Sbostic break; 12756059Sbostic case 'e': /* echo */ 12856059Sbostic if (state != COMMAND) 12956059Sbostic err("line %lu: not expecting command", lineno); 13056557Sbostic /* Don't display the newline, if CR at EOL. */ 13156557Sbostic if (p[len - 2] == '\r') 13256557Sbostic --len; 13356557Sbostic if (write(ofd, p + 1, len - 1) != len - 1) 13456557Sbostic err("write: %s", strerror(errno)); 13556059Sbostic break; 13656039Sbostic case 'g': /* get */ 13756039Sbostic if (state != COMMAND) 13856039Sbostic err("line %lu: not expecting command", lineno); 13956039Sbostic state = KEY; 14056039Sbostic command = GET; 14156039Sbostic break; 14256039Sbostic case 'p': /* put */ 14356039Sbostic if (state != COMMAND) 14456039Sbostic err("line %lu: not expecting command", lineno); 14556039Sbostic state = KEY; 14656039Sbostic command = PUT; 14756039Sbostic break; 14856039Sbostic case 'r': /* remove */ 14956039Sbostic if (state != COMMAND) 15056039Sbostic err("line %lu: not expecting command", lineno); 15156039Sbostic state = KEY; 15256039Sbostic command = REMOVE; 15356039Sbostic break; 15456039Sbostic case 's': /* seq */ 15556039Sbostic if (state != COMMAND) 15656039Sbostic err("line %lu: not expecting command", lineno); 15756059Sbostic if (flags == R_CURSOR) { 15856059Sbostic state = KEY; 15956059Sbostic command = SEQ; 16056059Sbostic } else 16156059Sbostic seq(dbp, &key); 16256039Sbostic break; 16356039Sbostic case 'f': 16456059Sbostic flags = setflags(p + 1); 16556039Sbostic break; 16656039Sbostic case 'D': /* data file */ 16756039Sbostic if (state != DATA) 16856039Sbostic err("line %lu: not expecting data", lineno); 16956039Sbostic data.data = rfile(p + 1, &data.size); 17056992Sbostic goto ldata; 17156039Sbostic case 'd': /* data */ 17256039Sbostic if (state != DATA) 17356039Sbostic err("line %lu: not expecting data", lineno); 17456557Sbostic data.data = xmalloc(p + 1, len - 1); 17556557Sbostic data.size = len - 1; 17656992Sbostic ldata: switch(command) { 17756557Sbostic case COMPARE: 17856557Sbostic compare(&keydata, &data); 17956557Sbostic break; 18056557Sbostic case PUT: 18156557Sbostic put(dbp, &key, &data); 18256557Sbostic break; 18356557Sbostic default: 18456039Sbostic err("line %lu: command doesn't take data", 18556039Sbostic lineno); 18656557Sbostic } 18756039Sbostic free(key.data); 18856039Sbostic free(data.data); 18956039Sbostic state = COMMAND; 19056039Sbostic break; 19156039Sbostic case 'K': /* key file */ 19256039Sbostic if (state != KEY) 19356039Sbostic err("line %lu: not expecting a key", lineno); 19456039Sbostic if (type == DB_RECNO) 19556039Sbostic err("line %lu: 'K' not available for recno", 19656039Sbostic lineno); 19756039Sbostic key.data = rfile(p + 1, &key.size); 19856992Sbostic goto lkey; 19956039Sbostic case 'k': /* key */ 20056039Sbostic if (state != KEY) 20156039Sbostic err("line %lu: not expecting a key", lineno); 20256039Sbostic if (type == DB_RECNO) { 20356039Sbostic static recno_t recno; 20456039Sbostic recno = strtol(p + 1, NULL, 0); 20556039Sbostic key.data = &recno; 20656039Sbostic key.size = sizeof(recno); 20756039Sbostic } else { 20856039Sbostic key.data = xmalloc(p + 1, len - 1); 20956039Sbostic key.size = len - 1; 21056039Sbostic } 21156992Sbostic lkey: switch(command) { 21256557Sbostic case COMPARE: 21356557Sbostic getdata(dbp, &key, &keydata); 21456557Sbostic state = DATA; 21556557Sbostic break; 21656039Sbostic case GET: 21756039Sbostic get(dbp, &key); 21856039Sbostic if (type != DB_RECNO) 21956039Sbostic free(key.data); 22056039Sbostic state = COMMAND; 22156039Sbostic break; 22256039Sbostic case PUT: 22356039Sbostic state = DATA; 22456039Sbostic break; 22556039Sbostic case REMOVE: 22656039Sbostic rem(dbp, &key); 22756039Sbostic if (type != DB_RECNO) 22856039Sbostic free(key.data); 22956039Sbostic state = COMMAND; 23056039Sbostic break; 23156059Sbostic case SEQ: 23256059Sbostic seq(dbp, &key); 23356059Sbostic if (type != DB_RECNO) 23456059Sbostic free(key.data); 23556059Sbostic state = COMMAND; 23656059Sbostic break; 23756039Sbostic default: 23856039Sbostic err("line %lu: command doesn't take a key", 23956039Sbostic lineno); 24056039Sbostic } 24156039Sbostic break; 24256992Sbostic case 'o': 24356992Sbostic dump(dbp, p[1] == 'r'); 24456992Sbostic break; 24556039Sbostic default: 24656039Sbostic err("line %lu: %s: unknown command character", 24756059Sbostic p, lineno); 24856039Sbostic } 24956039Sbostic } 25056039Sbostic (void)close(ofd); 25156039Sbostic exit(0); 25256039Sbostic } 25356039Sbostic 25456059Sbostic #define NOOVERWRITE "put failed, would overwrite key\n" 25556059Sbostic #define NOSUCHKEY "get failed, no such key\n" 25656059Sbostic 25756039Sbostic void 25856557Sbostic compare(db1, db2) 25956557Sbostic DBT *db1, *db2; 26056557Sbostic { 26156557Sbostic register size_t len; 26256557Sbostic register u_char *p1, *p2; 26356557Sbostic 26456557Sbostic if (db1->size != db2->size) 26556557Sbostic printf("compare failed: key->data len %lu != data len %lu\n", 26656557Sbostic db1->size, db2->size); 26756557Sbostic 26856557Sbostic len = MIN(db1->size, db2->size); 26956557Sbostic for (p1 = db1->data, p2 = db2->data; len--;) 27056557Sbostic if (*p1++ != *p2++) { 27156557Sbostic printf("compare failed at offset %d\n", 27256557Sbostic p1 - (u_char *)db1->data); 27356557Sbostic break; 27456557Sbostic } 27556557Sbostic } 27656557Sbostic 27756557Sbostic void 27856039Sbostic get(dbp, kp) 27956039Sbostic DB *dbp; 28056039Sbostic DBT *kp; 28156039Sbostic { 28256039Sbostic DBT data; 28356039Sbostic 28456059Sbostic switch(dbp->get(dbp, kp, &data, flags)) { 28556059Sbostic case 0: 28656059Sbostic (void)write(ofd, data.data, data.size); 28756059Sbostic break; 28856059Sbostic case -1: 28956039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 29056059Sbostic /* NOTREACHED */ 29156059Sbostic case 1: 29256059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 29357456Sbostic (void)fprintf(stderr, "%d: %.*s: %s\n", 29457456Sbostic lineno, kp->size, kp->data, NOSUCHKEY); 29556059Sbostic break; 29656059Sbostic } 29756039Sbostic } 29856039Sbostic 29956039Sbostic void 30056557Sbostic getdata(dbp, kp, dp) 30156557Sbostic DB *dbp; 30256557Sbostic DBT *kp, *dp; 30356557Sbostic { 30456557Sbostic switch(dbp->get(dbp, kp, dp, flags)) { 30556557Sbostic case 0: 30656557Sbostic return; 30756557Sbostic case -1: 30856557Sbostic err("line %lu: getdata: %s", lineno, strerror(errno)); 30956557Sbostic /* NOTREACHED */ 31056557Sbostic case 1: 31156557Sbostic err("line %lu: get failed, no such key", lineno); 31256557Sbostic /* NOTREACHED */ 31356557Sbostic } 31456557Sbostic } 31556557Sbostic 31656557Sbostic void 31756039Sbostic put(dbp, kp, dp) 31856039Sbostic DB *dbp; 31956039Sbostic DBT *kp, *dp; 32056039Sbostic { 32156059Sbostic switch(dbp->put(dbp, kp, dp, flags)) { 32256059Sbostic case 0: 32356059Sbostic break; 32456059Sbostic case -1: 32556039Sbostic err("line %lu: put: %s", lineno, strerror(errno)); 32656059Sbostic /* NOTREACHED */ 32756059Sbostic case 1: 32856059Sbostic (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1); 32956059Sbostic break; 33056059Sbostic } 33156039Sbostic } 33256039Sbostic 33356039Sbostic void 33456039Sbostic rem(dbp, kp) 33556039Sbostic DB *dbp; 33656039Sbostic DBT *kp; 33756039Sbostic { 33856059Sbostic switch(dbp->del(dbp, kp, flags)) { 33956059Sbostic case 0: 34056059Sbostic break; 34156059Sbostic case -1: 34256039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 34356059Sbostic /* NOTREACHED */ 34456059Sbostic case 1: 34556059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 34656059Sbostic break; 34756059Sbostic } 34856039Sbostic } 34956039Sbostic 35056039Sbostic void 35156059Sbostic seq(dbp, kp) 35256039Sbostic DB *dbp; 35356059Sbostic DBT *kp; 35456039Sbostic { 35556059Sbostic DBT data; 35656039Sbostic 35756059Sbostic switch(dbp->seq(dbp, kp, &data, flags)) { 35856059Sbostic case 0: 35956059Sbostic (void)write(ofd, data.data, data.size); 36056059Sbostic break; 36156059Sbostic case -1: 36256039Sbostic err("line %lu: seq: %s", lineno, strerror(errno)); 36356059Sbostic /* NOTREACHED */ 36456059Sbostic case 1: 36556059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 36656059Sbostic break; 36756059Sbostic } 36856039Sbostic } 36956992Sbostic 37056992Sbostic void 37156992Sbostic dump(dbp, rev) 37256992Sbostic DB *dbp; 37356992Sbostic int rev; 37456992Sbostic { 37556992Sbostic DBT key, data; 37656992Sbostic int flags, nflags; 37756992Sbostic 37856992Sbostic if (rev) { 37956992Sbostic flags = R_LAST; 38056992Sbostic nflags = R_PREV; 38156992Sbostic } else { 38256992Sbostic flags = R_FIRST; 38356992Sbostic nflags = R_NEXT; 38456992Sbostic } 38556992Sbostic for (;; flags = nflags) 38656992Sbostic switch(dbp->seq(dbp, &key, &data, flags)) { 38756992Sbostic case 0: 38856992Sbostic (void)write(ofd, data.data, data.size); 38956992Sbostic break; 39056992Sbostic case 1: 39156992Sbostic goto done; 39256992Sbostic case -1: 39356992Sbostic err("line %lu: (dump) seq: %s", 39456992Sbostic lineno, strerror(errno)); 39556992Sbostic /* NOTREACHED */ 39656992Sbostic } 39756992Sbostic done: return; 39856992Sbostic } 39956039Sbostic 40056039Sbostic u_int 40156039Sbostic setflags(s) 40256039Sbostic char *s; 40356039Sbostic { 40456039Sbostic char *p; 40556039Sbostic 40656039Sbostic for (; isspace(*s); ++s); 40756039Sbostic if (*s == '\n') 40856039Sbostic return (0); 40956039Sbostic if ((p = index(s, '\n')) != NULL) 41056039Sbostic *p = '\0'; 41156039Sbostic if (!strcmp(s, "R_CURSOR")) 41256039Sbostic return (R_CURSOR); 41356749Sbostic if (!strcmp(s, "R_CURSORLOG")) 41456749Sbostic return (R_CURSORLOG); 41556749Sbostic if (!strcmp(s, "R_FIRST")) 41656749Sbostic return (R_FIRST); 41756039Sbostic if (!strcmp(s, "R_IAFTER")) 41856039Sbostic return (R_IAFTER); 41956039Sbostic if (!strcmp(s, "R_IBEFORE")) 42056039Sbostic return (R_IBEFORE); 42156039Sbostic if (!strcmp(s, "R_LAST")) 42256039Sbostic return (R_LAST); 42356039Sbostic if (!strcmp(s, "R_NEXT")) 42456039Sbostic return (R_NEXT); 42556749Sbostic if (!strcmp(s, "R_NOOVERWRITE")) 42656749Sbostic return (R_NOOVERWRITE); 42756039Sbostic if (!strcmp(s, "R_PREV")) 42856039Sbostic return (R_PREV); 42956749Sbostic if (!strcmp(s, "R_SETCURSOR")) 43056749Sbostic return (R_SETCURSOR); 43156039Sbostic err("line %lu: %s: unknown flag", lineno, s); 43256039Sbostic /* NOTREACHED */ 43356039Sbostic } 43456039Sbostic 43556039Sbostic DBTYPE 43656039Sbostic dbtype(s) 43756039Sbostic char *s; 43856039Sbostic { 43956039Sbostic if (!strcmp(s, "btree")) 44056039Sbostic return (DB_BTREE); 44156039Sbostic if (!strcmp(s, "hash")) 44256039Sbostic return (DB_HASH); 44356039Sbostic if (!strcmp(s, "recno")) 44456039Sbostic return (DB_RECNO); 44556039Sbostic err("%s: unknown type (use btree, hash or recno)", s); 44656039Sbostic /* NOTREACHED */ 44756039Sbostic } 44856039Sbostic 44956039Sbostic void * 45056039Sbostic setinfo(type, s) 45156039Sbostic DBTYPE type; 45256039Sbostic char *s; 45356039Sbostic { 45456039Sbostic static BTREEINFO ib; 45556039Sbostic static HASHINFO ih; 45656039Sbostic static RECNOINFO rh; 45756039Sbostic char *eq; 45856039Sbostic 45956039Sbostic if ((eq = index(s, '=')) == NULL) 46056039Sbostic err("%s: illegal structure set statement", s); 46156039Sbostic *eq++ = '\0'; 46256039Sbostic if (!isdigit(*eq)) 46356039Sbostic err("%s: structure set statement must be a number", s); 46456039Sbostic 46556039Sbostic switch(type) { 46656039Sbostic case DB_BTREE: 46756039Sbostic if (!strcmp("flags", s)) { 46856039Sbostic ib.flags = strtoul(eq, NULL, 0); 46956039Sbostic return (&ib); 47056039Sbostic } 47156039Sbostic if (!strcmp("cachesize", s)) { 47256039Sbostic ib.cachesize = strtoul(eq, NULL, 0); 47356039Sbostic return (&ib); 47456039Sbostic } 47556039Sbostic if (!strcmp("maxkeypage", s)) { 47656039Sbostic ib.maxkeypage = strtoul(eq, NULL, 0); 47756039Sbostic return (&ib); 47856039Sbostic } 47956039Sbostic if (!strcmp("minkeypage", s)) { 48056039Sbostic ib.minkeypage = strtoul(eq, NULL, 0); 48156039Sbostic return (&ib); 48256039Sbostic } 48356039Sbostic if (!strcmp("lorder", s)) { 48456039Sbostic ib.lorder = strtoul(eq, NULL, 0); 48556039Sbostic return (&ib); 48656039Sbostic } 48757456Sbostic if (!strcmp("psize", s)) { 48857456Sbostic ib.psize = strtoul(eq, NULL, 0); 48957456Sbostic return (&ib); 49057456Sbostic } 49156039Sbostic break; 49256039Sbostic case DB_HASH: 49356039Sbostic if (!strcmp("bsize", s)) { 49456039Sbostic ih.bsize = strtoul(eq, NULL, 0); 49556039Sbostic return (&ib); 49656039Sbostic } 49756039Sbostic if (!strcmp("ffactor", s)) { 49856039Sbostic ih.ffactor = strtoul(eq, NULL, 0); 49956039Sbostic return (&ib); 50056039Sbostic } 50156039Sbostic if (!strcmp("nelem", s)) { 50256039Sbostic ih.nelem = strtoul(eq, NULL, 0); 50356039Sbostic return (&ib); 50456039Sbostic } 50556039Sbostic if (!strcmp("cachesize", s)) { 50656039Sbostic ih.cachesize = strtoul(eq, NULL, 0); 50756039Sbostic return (&ib); 50856039Sbostic } 50956039Sbostic if (!strcmp("lorder", s)) { 51056039Sbostic ih.lorder = strtoul(eq, NULL, 0); 51156039Sbostic return (&ib); 51256039Sbostic } 51356039Sbostic break; 51456039Sbostic case DB_RECNO: 51556039Sbostic if (!strcmp("flags", s)) { 51656039Sbostic rh.flags = strtoul(eq, NULL, 0); 51756039Sbostic return (&ib); 51856039Sbostic } 51956039Sbostic if (!strcmp("cachesize", s)) { 52056039Sbostic rh.cachesize = strtoul(eq, NULL, 0); 52156039Sbostic return (&ib); 52256039Sbostic } 52356039Sbostic if (!strcmp("lorder", s)) { 52456039Sbostic rh.lorder = strtoul(eq, NULL, 0); 52556039Sbostic return (&ib); 52656039Sbostic } 52756039Sbostic if (!strcmp("reclen", s)) { 52856039Sbostic rh.reclen = strtoul(eq, NULL, 0); 52956039Sbostic return (&ib); 53056039Sbostic } 53156039Sbostic if (!strcmp("bval", s)) { 53256039Sbostic rh.bval = strtoul(eq, NULL, 0); 53356039Sbostic return (&ib); 53456039Sbostic } 53556039Sbostic break; 53656039Sbostic } 53756039Sbostic err("%s: unknown structure value", s); 53856039Sbostic /* NOTREACHED */ 53956039Sbostic } 54056039Sbostic 54156039Sbostic void * 54256039Sbostic rfile(name, lenp) 54356039Sbostic char *name; 54456039Sbostic size_t *lenp; 54556039Sbostic { 54656039Sbostic struct stat sb; 54756039Sbostic void *p; 54856039Sbostic int fd; 54956039Sbostic char *np; 55056039Sbostic 55156039Sbostic for (; isspace(*name); ++name); 55256039Sbostic if ((np = index(name, '\n')) != NULL) 55356039Sbostic *np = '\0'; 55456039Sbostic if ((fd = open(name, O_RDONLY, 0)) < 0 || 55556039Sbostic fstat(fd, &sb)) 55656039Sbostic err("%s: %s\n", name, strerror(errno)); 55758312Sbostic if (sb.st_size > (off_t)SIZE_T_MAX) 55856039Sbostic err("%s: %s\n", name, strerror(E2BIG)); 55956039Sbostic if ((p = malloc((u_int)sb.st_size)) == NULL) 56056039Sbostic err("%s", strerror(errno)); 56156039Sbostic (void)read(fd, p, (int)sb.st_size); 56256039Sbostic *lenp = sb.st_size; 56356489Sbostic (void)close(fd); 56456039Sbostic return (p); 56556039Sbostic } 56656039Sbostic 56756039Sbostic void * 56856039Sbostic xmalloc(text, len) 56956039Sbostic char *text; 57056039Sbostic size_t len; 57156039Sbostic { 57256039Sbostic void *p; 57356039Sbostic 57456039Sbostic if ((p = malloc(len)) == NULL) 57556039Sbostic err("%s", strerror(errno)); 57658015Sbostic memmove(p, text, len); 57756039Sbostic return (p); 57856039Sbostic } 57956039Sbostic 58056039Sbostic void 58156039Sbostic usage() 58256039Sbostic { 58356039Sbostic (void)fprintf(stderr, 58456039Sbostic "usage: dbtest [-i info] [-o file] type script\n"); 58556039Sbostic exit(1); 58656039Sbostic } 58756039Sbostic 58856039Sbostic #if __STDC__ 58956039Sbostic #include <stdarg.h> 59056039Sbostic #else 59156039Sbostic #include <varargs.h> 59256039Sbostic #endif 59356039Sbostic 59456039Sbostic void 59556039Sbostic #if __STDC__ 59656039Sbostic err(const char *fmt, ...) 59756039Sbostic #else 59856039Sbostic err(fmt, va_alist) 59956039Sbostic char *fmt; 60056039Sbostic va_dcl 60156039Sbostic #endif 60256039Sbostic { 60356039Sbostic va_list ap; 60456039Sbostic #if __STDC__ 60556039Sbostic va_start(ap, fmt); 60656039Sbostic #else 60756039Sbostic va_start(ap); 60856039Sbostic #endif 60956039Sbostic (void)fprintf(stderr, "dbtest: "); 61056039Sbostic (void)vfprintf(stderr, fmt, ap); 61156039Sbostic va_end(ap); 61256039Sbostic (void)fprintf(stderr, "\n"); 61356039Sbostic exit(1); 61456039Sbostic /* NOTREACHED */ 61556039Sbostic } 616