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*59493Sbostic static char sccsid[] = "@(#)dbtest.c 5.13 (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 3059492Sbostic #include <db.h> 3159492Sbostic 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 } 187*59493Sbostic if (type != DB_RECNO) 188*59493Sbostic free(key.data); 18956039Sbostic free(data.data); 19056039Sbostic state = COMMAND; 19156039Sbostic break; 19256039Sbostic case 'K': /* key file */ 19356039Sbostic if (state != KEY) 19456039Sbostic err("line %lu: not expecting a key", lineno); 19556039Sbostic if (type == DB_RECNO) 19656039Sbostic err("line %lu: 'K' not available for recno", 19756039Sbostic lineno); 19856039Sbostic key.data = rfile(p + 1, &key.size); 19956992Sbostic goto lkey; 20056039Sbostic case 'k': /* key */ 20156039Sbostic if (state != KEY) 20256039Sbostic err("line %lu: not expecting a key", lineno); 20356039Sbostic if (type == DB_RECNO) { 20456039Sbostic static recno_t recno; 20556039Sbostic recno = strtol(p + 1, NULL, 0); 20656039Sbostic key.data = &recno; 20756039Sbostic key.size = sizeof(recno); 20856039Sbostic } else { 20956039Sbostic key.data = xmalloc(p + 1, len - 1); 21056039Sbostic key.size = len - 1; 21156039Sbostic } 21256992Sbostic lkey: switch(command) { 21356557Sbostic case COMPARE: 21456557Sbostic getdata(dbp, &key, &keydata); 21556557Sbostic state = DATA; 21656557Sbostic break; 21756039Sbostic case GET: 21856039Sbostic get(dbp, &key); 21956039Sbostic if (type != DB_RECNO) 22056039Sbostic free(key.data); 22156039Sbostic state = COMMAND; 22256039Sbostic break; 22356039Sbostic case PUT: 22456039Sbostic state = DATA; 22556039Sbostic break; 22656039Sbostic case REMOVE: 22756039Sbostic rem(dbp, &key); 22856039Sbostic if (type != DB_RECNO) 22956039Sbostic free(key.data); 23056039Sbostic state = COMMAND; 23156039Sbostic break; 23256059Sbostic case SEQ: 23356059Sbostic seq(dbp, &key); 23456059Sbostic if (type != DB_RECNO) 23556059Sbostic free(key.data); 23656059Sbostic state = COMMAND; 23756059Sbostic break; 23856039Sbostic default: 23956039Sbostic err("line %lu: command doesn't take a key", 24056039Sbostic lineno); 24156039Sbostic } 24256039Sbostic break; 24356992Sbostic case 'o': 24456992Sbostic dump(dbp, p[1] == 'r'); 24556992Sbostic break; 24656039Sbostic default: 24756039Sbostic err("line %lu: %s: unknown command character", 24856059Sbostic p, lineno); 24956039Sbostic } 25056039Sbostic } 25156039Sbostic (void)close(ofd); 25256039Sbostic exit(0); 25356039Sbostic } 25456039Sbostic 25556059Sbostic #define NOOVERWRITE "put failed, would overwrite key\n" 25656059Sbostic #define NOSUCHKEY "get failed, no such key\n" 25756059Sbostic 25856039Sbostic void 25956557Sbostic compare(db1, db2) 26056557Sbostic DBT *db1, *db2; 26156557Sbostic { 26256557Sbostic register size_t len; 26356557Sbostic register u_char *p1, *p2; 26456557Sbostic 26556557Sbostic if (db1->size != db2->size) 26656557Sbostic printf("compare failed: key->data len %lu != data len %lu\n", 26756557Sbostic db1->size, db2->size); 26856557Sbostic 26956557Sbostic len = MIN(db1->size, db2->size); 27056557Sbostic for (p1 = db1->data, p2 = db2->data; len--;) 27156557Sbostic if (*p1++ != *p2++) { 27256557Sbostic printf("compare failed at offset %d\n", 27356557Sbostic p1 - (u_char *)db1->data); 27456557Sbostic break; 27556557Sbostic } 27656557Sbostic } 27756557Sbostic 27856557Sbostic void 27956039Sbostic get(dbp, kp) 28056039Sbostic DB *dbp; 28156039Sbostic DBT *kp; 28256039Sbostic { 28356039Sbostic DBT data; 28456039Sbostic 28556059Sbostic switch(dbp->get(dbp, kp, &data, flags)) { 28656059Sbostic case 0: 28756059Sbostic (void)write(ofd, data.data, data.size); 28856059Sbostic break; 28956059Sbostic case -1: 29056039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 29156059Sbostic /* NOTREACHED */ 29256059Sbostic case 1: 29356059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 29457456Sbostic (void)fprintf(stderr, "%d: %.*s: %s\n", 29557456Sbostic lineno, kp->size, kp->data, NOSUCHKEY); 29656059Sbostic break; 29756059Sbostic } 29856039Sbostic } 29956039Sbostic 30056039Sbostic void 30156557Sbostic getdata(dbp, kp, dp) 30256557Sbostic DB *dbp; 30356557Sbostic DBT *kp, *dp; 30456557Sbostic { 30556557Sbostic switch(dbp->get(dbp, kp, dp, flags)) { 30656557Sbostic case 0: 30756557Sbostic return; 30856557Sbostic case -1: 30956557Sbostic err("line %lu: getdata: %s", lineno, strerror(errno)); 31056557Sbostic /* NOTREACHED */ 31156557Sbostic case 1: 31256557Sbostic err("line %lu: get failed, no such key", lineno); 31356557Sbostic /* NOTREACHED */ 31456557Sbostic } 31556557Sbostic } 31656557Sbostic 31756557Sbostic void 31856039Sbostic put(dbp, kp, dp) 31956039Sbostic DB *dbp; 32056039Sbostic DBT *kp, *dp; 32156039Sbostic { 32256059Sbostic switch(dbp->put(dbp, kp, dp, flags)) { 32356059Sbostic case 0: 32456059Sbostic break; 32556059Sbostic case -1: 32656039Sbostic err("line %lu: put: %s", lineno, strerror(errno)); 32756059Sbostic /* NOTREACHED */ 32856059Sbostic case 1: 32956059Sbostic (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1); 33056059Sbostic break; 33156059Sbostic } 33256039Sbostic } 33356039Sbostic 33456039Sbostic void 33556039Sbostic rem(dbp, kp) 33656039Sbostic DB *dbp; 33756039Sbostic DBT *kp; 33856039Sbostic { 33956059Sbostic switch(dbp->del(dbp, kp, flags)) { 34056059Sbostic case 0: 34156059Sbostic break; 34256059Sbostic case -1: 34356039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 34456059Sbostic /* NOTREACHED */ 34556059Sbostic case 1: 34656059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 34756059Sbostic break; 34856059Sbostic } 34956039Sbostic } 35056039Sbostic 35156039Sbostic void 35256059Sbostic seq(dbp, kp) 35356039Sbostic DB *dbp; 35456059Sbostic DBT *kp; 35556039Sbostic { 35656059Sbostic DBT data; 35756039Sbostic 35856059Sbostic switch(dbp->seq(dbp, kp, &data, flags)) { 35956059Sbostic case 0: 36056059Sbostic (void)write(ofd, data.data, data.size); 36156059Sbostic break; 36256059Sbostic case -1: 36356039Sbostic err("line %lu: seq: %s", lineno, strerror(errno)); 36456059Sbostic /* NOTREACHED */ 36556059Sbostic case 1: 36656059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 36756059Sbostic break; 36856059Sbostic } 36956039Sbostic } 37056992Sbostic 37156992Sbostic void 37256992Sbostic dump(dbp, rev) 37356992Sbostic DB *dbp; 37456992Sbostic int rev; 37556992Sbostic { 37656992Sbostic DBT key, data; 37756992Sbostic int flags, nflags; 37856992Sbostic 37956992Sbostic if (rev) { 38056992Sbostic flags = R_LAST; 38156992Sbostic nflags = R_PREV; 38256992Sbostic } else { 38356992Sbostic flags = R_FIRST; 38456992Sbostic nflags = R_NEXT; 38556992Sbostic } 38656992Sbostic for (;; flags = nflags) 38756992Sbostic switch(dbp->seq(dbp, &key, &data, flags)) { 38856992Sbostic case 0: 38956992Sbostic (void)write(ofd, data.data, data.size); 39056992Sbostic break; 39156992Sbostic case 1: 39256992Sbostic goto done; 39356992Sbostic case -1: 39456992Sbostic err("line %lu: (dump) seq: %s", 39556992Sbostic lineno, strerror(errno)); 39656992Sbostic /* NOTREACHED */ 39756992Sbostic } 39856992Sbostic done: return; 39956992Sbostic } 40056039Sbostic 40156039Sbostic u_int 40256039Sbostic setflags(s) 40356039Sbostic char *s; 40456039Sbostic { 40556039Sbostic char *p; 40656039Sbostic 40756039Sbostic for (; isspace(*s); ++s); 40856039Sbostic if (*s == '\n') 40956039Sbostic return (0); 41056039Sbostic if ((p = index(s, '\n')) != NULL) 41156039Sbostic *p = '\0'; 41256039Sbostic if (!strcmp(s, "R_CURSOR")) 41356039Sbostic return (R_CURSOR); 41456749Sbostic if (!strcmp(s, "R_CURSORLOG")) 41556749Sbostic return (R_CURSORLOG); 41656749Sbostic if (!strcmp(s, "R_FIRST")) 41756749Sbostic return (R_FIRST); 41856039Sbostic if (!strcmp(s, "R_IAFTER")) 41956039Sbostic return (R_IAFTER); 42056039Sbostic if (!strcmp(s, "R_IBEFORE")) 42156039Sbostic return (R_IBEFORE); 42256039Sbostic if (!strcmp(s, "R_LAST")) 42356039Sbostic return (R_LAST); 42456039Sbostic if (!strcmp(s, "R_NEXT")) 42556039Sbostic return (R_NEXT); 42656749Sbostic if (!strcmp(s, "R_NOOVERWRITE")) 42756749Sbostic return (R_NOOVERWRITE); 42856039Sbostic if (!strcmp(s, "R_PREV")) 42956039Sbostic return (R_PREV); 43056749Sbostic if (!strcmp(s, "R_SETCURSOR")) 43156749Sbostic return (R_SETCURSOR); 43256039Sbostic err("line %lu: %s: unknown flag", lineno, s); 43356039Sbostic /* NOTREACHED */ 43456039Sbostic } 43556039Sbostic 43656039Sbostic DBTYPE 43756039Sbostic dbtype(s) 43856039Sbostic char *s; 43956039Sbostic { 44056039Sbostic if (!strcmp(s, "btree")) 44156039Sbostic return (DB_BTREE); 44256039Sbostic if (!strcmp(s, "hash")) 44356039Sbostic return (DB_HASH); 44456039Sbostic if (!strcmp(s, "recno")) 44556039Sbostic return (DB_RECNO); 44656039Sbostic err("%s: unknown type (use btree, hash or recno)", s); 44756039Sbostic /* NOTREACHED */ 44856039Sbostic } 44956039Sbostic 45056039Sbostic void * 45156039Sbostic setinfo(type, s) 45256039Sbostic DBTYPE type; 45356039Sbostic char *s; 45456039Sbostic { 45556039Sbostic static BTREEINFO ib; 45656039Sbostic static HASHINFO ih; 45756039Sbostic static RECNOINFO rh; 45856039Sbostic char *eq; 45956039Sbostic 46056039Sbostic if ((eq = index(s, '=')) == NULL) 46156039Sbostic err("%s: illegal structure set statement", s); 46256039Sbostic *eq++ = '\0'; 46356039Sbostic if (!isdigit(*eq)) 46456039Sbostic err("%s: structure set statement must be a number", s); 46556039Sbostic 46656039Sbostic switch(type) { 46756039Sbostic case DB_BTREE: 46856039Sbostic if (!strcmp("flags", s)) { 46956039Sbostic ib.flags = strtoul(eq, NULL, 0); 47056039Sbostic return (&ib); 47156039Sbostic } 47256039Sbostic if (!strcmp("cachesize", s)) { 47356039Sbostic ib.cachesize = strtoul(eq, NULL, 0); 47456039Sbostic return (&ib); 47556039Sbostic } 47656039Sbostic if (!strcmp("maxkeypage", s)) { 47756039Sbostic ib.maxkeypage = strtoul(eq, NULL, 0); 47856039Sbostic return (&ib); 47956039Sbostic } 48056039Sbostic if (!strcmp("minkeypage", s)) { 48156039Sbostic ib.minkeypage = strtoul(eq, NULL, 0); 48256039Sbostic return (&ib); 48356039Sbostic } 48456039Sbostic if (!strcmp("lorder", s)) { 48556039Sbostic ib.lorder = strtoul(eq, NULL, 0); 48656039Sbostic return (&ib); 48756039Sbostic } 48857456Sbostic if (!strcmp("psize", s)) { 48957456Sbostic ib.psize = strtoul(eq, NULL, 0); 49057456Sbostic return (&ib); 49157456Sbostic } 49256039Sbostic break; 49356039Sbostic case DB_HASH: 49456039Sbostic if (!strcmp("bsize", s)) { 49556039Sbostic ih.bsize = strtoul(eq, NULL, 0); 49656039Sbostic return (&ib); 49756039Sbostic } 49856039Sbostic if (!strcmp("ffactor", s)) { 49956039Sbostic ih.ffactor = strtoul(eq, NULL, 0); 50056039Sbostic return (&ib); 50156039Sbostic } 50256039Sbostic if (!strcmp("nelem", s)) { 50356039Sbostic ih.nelem = strtoul(eq, NULL, 0); 50456039Sbostic return (&ib); 50556039Sbostic } 50656039Sbostic if (!strcmp("cachesize", s)) { 50756039Sbostic ih.cachesize = strtoul(eq, NULL, 0); 50856039Sbostic return (&ib); 50956039Sbostic } 51056039Sbostic if (!strcmp("lorder", s)) { 51156039Sbostic ih.lorder = strtoul(eq, NULL, 0); 51256039Sbostic return (&ib); 51356039Sbostic } 51456039Sbostic break; 51556039Sbostic case DB_RECNO: 51656039Sbostic if (!strcmp("flags", s)) { 51756039Sbostic rh.flags = strtoul(eq, NULL, 0); 51856039Sbostic return (&ib); 51956039Sbostic } 52056039Sbostic if (!strcmp("cachesize", s)) { 52156039Sbostic rh.cachesize = strtoul(eq, NULL, 0); 52256039Sbostic return (&ib); 52356039Sbostic } 52456039Sbostic if (!strcmp("lorder", s)) { 52556039Sbostic rh.lorder = strtoul(eq, NULL, 0); 52656039Sbostic return (&ib); 52756039Sbostic } 52856039Sbostic if (!strcmp("reclen", s)) { 52956039Sbostic rh.reclen = strtoul(eq, NULL, 0); 53056039Sbostic return (&ib); 53156039Sbostic } 53256039Sbostic if (!strcmp("bval", s)) { 53356039Sbostic rh.bval = strtoul(eq, NULL, 0); 53456039Sbostic return (&ib); 53556039Sbostic } 53656039Sbostic break; 53756039Sbostic } 53856039Sbostic err("%s: unknown structure value", s); 53956039Sbostic /* NOTREACHED */ 54056039Sbostic } 54156039Sbostic 54256039Sbostic void * 54356039Sbostic rfile(name, lenp) 54456039Sbostic char *name; 54556039Sbostic size_t *lenp; 54656039Sbostic { 54756039Sbostic struct stat sb; 54856039Sbostic void *p; 54956039Sbostic int fd; 55056039Sbostic char *np; 55156039Sbostic 55256039Sbostic for (; isspace(*name); ++name); 55356039Sbostic if ((np = index(name, '\n')) != NULL) 55456039Sbostic *np = '\0'; 55556039Sbostic if ((fd = open(name, O_RDONLY, 0)) < 0 || 55656039Sbostic fstat(fd, &sb)) 55756039Sbostic err("%s: %s\n", name, strerror(errno)); 55858312Sbostic if (sb.st_size > (off_t)SIZE_T_MAX) 55956039Sbostic err("%s: %s\n", name, strerror(E2BIG)); 56056039Sbostic if ((p = malloc((u_int)sb.st_size)) == NULL) 56156039Sbostic err("%s", strerror(errno)); 56256039Sbostic (void)read(fd, p, (int)sb.st_size); 56356039Sbostic *lenp = sb.st_size; 56456489Sbostic (void)close(fd); 56556039Sbostic return (p); 56656039Sbostic } 56756039Sbostic 56856039Sbostic void * 56956039Sbostic xmalloc(text, len) 57056039Sbostic char *text; 57156039Sbostic size_t len; 57256039Sbostic { 57356039Sbostic void *p; 57456039Sbostic 57556039Sbostic if ((p = malloc(len)) == NULL) 57656039Sbostic err("%s", strerror(errno)); 57758015Sbostic memmove(p, text, len); 57856039Sbostic return (p); 57956039Sbostic } 58056039Sbostic 58156039Sbostic void 58256039Sbostic usage() 58356039Sbostic { 58456039Sbostic (void)fprintf(stderr, 58556039Sbostic "usage: dbtest [-i info] [-o file] type script\n"); 58656039Sbostic exit(1); 58756039Sbostic } 58856039Sbostic 58956039Sbostic #if __STDC__ 59056039Sbostic #include <stdarg.h> 59156039Sbostic #else 59256039Sbostic #include <varargs.h> 59356039Sbostic #endif 59456039Sbostic 59556039Sbostic void 59656039Sbostic #if __STDC__ 59756039Sbostic err(const char *fmt, ...) 59856039Sbostic #else 59956039Sbostic err(fmt, va_alist) 60056039Sbostic char *fmt; 60156039Sbostic va_dcl 60256039Sbostic #endif 60356039Sbostic { 60456039Sbostic va_list ap; 60556039Sbostic #if __STDC__ 60656039Sbostic va_start(ap, fmt); 60756039Sbostic #else 60856039Sbostic va_start(ap); 60956039Sbostic #endif 61056039Sbostic (void)fprintf(stderr, "dbtest: "); 61156039Sbostic (void)vfprintf(stderr, fmt, ap); 61256039Sbostic va_end(ap); 61356039Sbostic (void)fprintf(stderr, "\n"); 61456039Sbostic exit(1); 61556039Sbostic /* NOTREACHED */ 61656039Sbostic } 617