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*60229Sbostic static char sccsid[] = "@(#)dbtest.c 5.18 (Berkeley) 05/22/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; 6959626Sbostic char *fname, *infoarg, *p, buf[8 * 1024]; 7056039Sbostic 7156039Sbostic infoarg = NULL; 7259626Sbostic fname = NULL; 7359626Sbostic while ((ch = getopt(argc, argv, "f:i:o:")) != EOF) 7456039Sbostic switch(ch) { 7559626Sbostic case 'f': 7659626Sbostic fname = optarg; 7759626Sbostic break; 7856039Sbostic case 'i': 7956039Sbostic infoarg = optarg; 8056039Sbostic break; 8156039Sbostic case 'o': 8256039Sbostic if ((ofd = open(optarg, 8356039Sbostic O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 8456039Sbostic err("%s: %s", optarg, strerror(errno)); 8556039Sbostic break; 8656039Sbostic case '?': 8756039Sbostic default: 8856039Sbostic usage(); 8956039Sbostic } 9056039Sbostic argc -= optind; 9156039Sbostic argv += optind; 9256039Sbostic 9356039Sbostic if (argc != 2) 9456039Sbostic usage(); 9556039Sbostic 9656039Sbostic /* Set the type. */ 9756039Sbostic type = dbtype(*argv++); 9856039Sbostic 9956039Sbostic /* Open the descriptor file. */ 10056039Sbostic if (freopen(*argv, "r", stdin) == NULL) 10156039Sbostic err("%s: %s", *argv, strerror(errno)); 10256039Sbostic 10356039Sbostic /* Set up the db structure as necessary. */ 10456039Sbostic if (infoarg == NULL) 10556039Sbostic infop = NULL; 10656039Sbostic else 10757463Sbostic for (p = strtok(infoarg, ",\t "); p != NULL; 10857463Sbostic p = strtok(0, ",\t ")) 10956039Sbostic if (*p != '\0') 11056039Sbostic infop = setinfo(type, p); 11156039Sbostic 11259626Sbostic /* Open the DB. */ 11359626Sbostic if (fname == NULL) { 11460129Smargo p = getenv("TMPDIR"); 11560129Smargo if (p == NULL) 11660129Smargo p = "/var/tmp"; 11760129Smargo (void)sprintf(buf, "%s/__dbtest", p); 11860129Smargo fname = buf; 11960129Smargo (void)unlink(buf); 12059626Sbostic } 12159626Sbostic if ((dbp = dbopen(fname, 12256039Sbostic O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL) 12356039Sbostic err("dbopen: %s", strerror(errno)); 12457456Sbostic XXdbp = dbp; 12556039Sbostic 12656039Sbostic state = COMMAND; 12756039Sbostic for (lineno = 1; 12856039Sbostic (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) { 12956039Sbostic len = strlen(buf); 13056039Sbostic switch(*p) { 13156557Sbostic case 'c': /* compare */ 13256557Sbostic if (state != COMMAND) 13356557Sbostic err("line %lu: not expecting command", lineno); 13456557Sbostic state = KEY; 13556557Sbostic command = COMPARE; 13656557Sbostic break; 13756059Sbostic case 'e': /* echo */ 13856059Sbostic if (state != COMMAND) 13956059Sbostic err("line %lu: not expecting command", lineno); 14056557Sbostic /* Don't display the newline, if CR at EOL. */ 14156557Sbostic if (p[len - 2] == '\r') 14256557Sbostic --len; 14356557Sbostic if (write(ofd, p + 1, len - 1) != len - 1) 14456557Sbostic err("write: %s", strerror(errno)); 14556059Sbostic break; 14656039Sbostic case 'g': /* get */ 14756039Sbostic if (state != COMMAND) 14856039Sbostic err("line %lu: not expecting command", lineno); 14956039Sbostic state = KEY; 15056039Sbostic command = GET; 15156039Sbostic break; 15256039Sbostic case 'p': /* put */ 15356039Sbostic if (state != COMMAND) 15456039Sbostic err("line %lu: not expecting command", lineno); 15556039Sbostic state = KEY; 15656039Sbostic command = PUT; 15756039Sbostic break; 15856039Sbostic case 'r': /* remove */ 15956039Sbostic if (state != COMMAND) 16056039Sbostic err("line %lu: not expecting command", lineno); 16156039Sbostic state = KEY; 16256039Sbostic command = REMOVE; 16356039Sbostic break; 16456039Sbostic case 's': /* seq */ 16556039Sbostic if (state != COMMAND) 16656039Sbostic err("line %lu: not expecting command", lineno); 16756059Sbostic if (flags == R_CURSOR) { 16856059Sbostic state = KEY; 16956059Sbostic command = SEQ; 17056059Sbostic } else 17156059Sbostic seq(dbp, &key); 17256039Sbostic break; 17356039Sbostic case 'f': 17456059Sbostic flags = setflags(p + 1); 17556039Sbostic break; 17656039Sbostic case 'D': /* data file */ 17756039Sbostic if (state != DATA) 17856039Sbostic err("line %lu: not expecting data", lineno); 17956039Sbostic data.data = rfile(p + 1, &data.size); 18056992Sbostic goto ldata; 18156039Sbostic case 'd': /* data */ 18256039Sbostic if (state != DATA) 18356039Sbostic err("line %lu: not expecting data", lineno); 18456557Sbostic data.data = xmalloc(p + 1, len - 1); 18556557Sbostic data.size = len - 1; 18656992Sbostic ldata: switch(command) { 18756557Sbostic case COMPARE: 18856557Sbostic compare(&keydata, &data); 18956557Sbostic break; 19056557Sbostic case PUT: 19156557Sbostic put(dbp, &key, &data); 19256557Sbostic break; 19356557Sbostic default: 19456039Sbostic err("line %lu: command doesn't take data", 19556039Sbostic lineno); 19656557Sbostic } 19759493Sbostic if (type != DB_RECNO) 19859493Sbostic free(key.data); 19956039Sbostic free(data.data); 20056039Sbostic state = COMMAND; 20156039Sbostic break; 20256039Sbostic case 'K': /* key file */ 20356039Sbostic if (state != KEY) 20456039Sbostic err("line %lu: not expecting a key", lineno); 20556039Sbostic if (type == DB_RECNO) 20656039Sbostic err("line %lu: 'K' not available for recno", 20756039Sbostic lineno); 20856039Sbostic key.data = rfile(p + 1, &key.size); 20956992Sbostic goto lkey; 21056039Sbostic case 'k': /* key */ 21156039Sbostic if (state != KEY) 21256039Sbostic err("line %lu: not expecting a key", lineno); 21356039Sbostic if (type == DB_RECNO) { 21456039Sbostic static recno_t recno; 21556039Sbostic recno = strtol(p + 1, NULL, 0); 21656039Sbostic key.data = &recno; 21756039Sbostic key.size = sizeof(recno); 21856039Sbostic } else { 21956039Sbostic key.data = xmalloc(p + 1, len - 1); 22056039Sbostic key.size = len - 1; 22156039Sbostic } 22256992Sbostic lkey: switch(command) { 22356557Sbostic case COMPARE: 22456557Sbostic getdata(dbp, &key, &keydata); 22556557Sbostic state = DATA; 22656557Sbostic break; 22756039Sbostic case GET: 22856039Sbostic get(dbp, &key); 22956039Sbostic if (type != DB_RECNO) 23056039Sbostic free(key.data); 23156039Sbostic state = COMMAND; 23256039Sbostic break; 23356039Sbostic case PUT: 23456039Sbostic state = DATA; 23556039Sbostic break; 23656039Sbostic case REMOVE: 23756039Sbostic rem(dbp, &key); 23856039Sbostic if (type != DB_RECNO) 23956039Sbostic free(key.data); 24056039Sbostic state = COMMAND; 24156039Sbostic break; 24256059Sbostic case SEQ: 24356059Sbostic seq(dbp, &key); 24456059Sbostic if (type != DB_RECNO) 24556059Sbostic free(key.data); 24656059Sbostic state = COMMAND; 24756059Sbostic break; 24856039Sbostic default: 24956039Sbostic err("line %lu: command doesn't take a key", 25056039Sbostic lineno); 25156039Sbostic } 25256039Sbostic break; 25356992Sbostic case 'o': 25456992Sbostic dump(dbp, p[1] == 'r'); 25556992Sbostic break; 25656039Sbostic default: 25756039Sbostic err("line %lu: %s: unknown command character", 25856059Sbostic p, lineno); 25956039Sbostic } 26056039Sbostic } 26159626Sbostic if (dbp->close(dbp)) 26259626Sbostic err("db->close: %s", strerror(errno)); 26356039Sbostic (void)close(ofd); 26456039Sbostic exit(0); 26556039Sbostic } 26656039Sbostic 26756059Sbostic #define NOOVERWRITE "put failed, would overwrite key\n" 26856059Sbostic #define NOSUCHKEY "get failed, no such key\n" 26956059Sbostic 27056039Sbostic void 27156557Sbostic compare(db1, db2) 27256557Sbostic DBT *db1, *db2; 27356557Sbostic { 27456557Sbostic register size_t len; 27556557Sbostic register u_char *p1, *p2; 27656557Sbostic 27756557Sbostic if (db1->size != db2->size) 27856557Sbostic printf("compare failed: key->data len %lu != data len %lu\n", 27956557Sbostic db1->size, db2->size); 28056557Sbostic 28156557Sbostic len = MIN(db1->size, db2->size); 28256557Sbostic for (p1 = db1->data, p2 = db2->data; len--;) 28356557Sbostic if (*p1++ != *p2++) { 28456557Sbostic printf("compare failed at offset %d\n", 28556557Sbostic p1 - (u_char *)db1->data); 28656557Sbostic break; 28756557Sbostic } 28856557Sbostic } 28956557Sbostic 29056557Sbostic void 29156039Sbostic get(dbp, kp) 29256039Sbostic DB *dbp; 29356039Sbostic DBT *kp; 29456039Sbostic { 29556039Sbostic DBT data; 29656039Sbostic 29756059Sbostic switch(dbp->get(dbp, kp, &data, flags)) { 29856059Sbostic case 0: 29956059Sbostic (void)write(ofd, data.data, data.size); 30056059Sbostic break; 30156059Sbostic case -1: 30256039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 30356059Sbostic /* NOTREACHED */ 30456059Sbostic case 1: 30556059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 30657456Sbostic (void)fprintf(stderr, "%d: %.*s: %s\n", 30757456Sbostic lineno, kp->size, kp->data, NOSUCHKEY); 30856059Sbostic break; 30956059Sbostic } 31056039Sbostic } 31156039Sbostic 31256039Sbostic void 31356557Sbostic getdata(dbp, kp, dp) 31456557Sbostic DB *dbp; 31556557Sbostic DBT *kp, *dp; 31656557Sbostic { 31756557Sbostic switch(dbp->get(dbp, kp, dp, flags)) { 31856557Sbostic case 0: 31956557Sbostic return; 32056557Sbostic case -1: 32156557Sbostic err("line %lu: getdata: %s", lineno, strerror(errno)); 32256557Sbostic /* NOTREACHED */ 32356557Sbostic case 1: 32456557Sbostic err("line %lu: get failed, no such key", lineno); 32556557Sbostic /* NOTREACHED */ 32656557Sbostic } 32756557Sbostic } 32856557Sbostic 32956557Sbostic void 33056039Sbostic put(dbp, kp, dp) 33156039Sbostic DB *dbp; 33256039Sbostic DBT *kp, *dp; 33356039Sbostic { 33456059Sbostic switch(dbp->put(dbp, kp, dp, flags)) { 33556059Sbostic case 0: 33656059Sbostic break; 33756059Sbostic case -1: 33856039Sbostic err("line %lu: put: %s", lineno, strerror(errno)); 33956059Sbostic /* NOTREACHED */ 34056059Sbostic case 1: 34156059Sbostic (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1); 34256059Sbostic break; 34356059Sbostic } 34456039Sbostic } 34556039Sbostic 34656039Sbostic void 34756039Sbostic rem(dbp, kp) 34856039Sbostic DB *dbp; 34956039Sbostic DBT *kp; 35056039Sbostic { 35156059Sbostic switch(dbp->del(dbp, kp, flags)) { 35256059Sbostic case 0: 35356059Sbostic break; 35456059Sbostic case -1: 35556039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 35656059Sbostic /* NOTREACHED */ 35756059Sbostic case 1: 35856059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 35956059Sbostic break; 36056059Sbostic } 36156039Sbostic } 36256039Sbostic 36356039Sbostic void 36456059Sbostic seq(dbp, kp) 36556039Sbostic DB *dbp; 36656059Sbostic DBT *kp; 36756039Sbostic { 36856059Sbostic DBT data; 36956039Sbostic 37056059Sbostic switch(dbp->seq(dbp, kp, &data, flags)) { 37156059Sbostic case 0: 37256059Sbostic (void)write(ofd, data.data, data.size); 37356059Sbostic break; 37456059Sbostic case -1: 37556039Sbostic err("line %lu: seq: %s", lineno, strerror(errno)); 37656059Sbostic /* NOTREACHED */ 37756059Sbostic case 1: 37856059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 37956059Sbostic break; 38056059Sbostic } 38156039Sbostic } 38256992Sbostic 38356992Sbostic void 38456992Sbostic dump(dbp, rev) 38556992Sbostic DB *dbp; 38656992Sbostic int rev; 38756992Sbostic { 38856992Sbostic DBT key, data; 38956992Sbostic int flags, nflags; 39056992Sbostic 39156992Sbostic if (rev) { 39256992Sbostic flags = R_LAST; 39356992Sbostic nflags = R_PREV; 39456992Sbostic } else { 39556992Sbostic flags = R_FIRST; 39656992Sbostic nflags = R_NEXT; 39756992Sbostic } 39856992Sbostic for (;; flags = nflags) 39956992Sbostic switch(dbp->seq(dbp, &key, &data, flags)) { 40056992Sbostic case 0: 40156992Sbostic (void)write(ofd, data.data, data.size); 40256992Sbostic break; 40356992Sbostic case 1: 40456992Sbostic goto done; 40556992Sbostic case -1: 40656992Sbostic err("line %lu: (dump) seq: %s", 40756992Sbostic lineno, strerror(errno)); 40856992Sbostic /* NOTREACHED */ 40956992Sbostic } 41056992Sbostic done: return; 41156992Sbostic } 41256039Sbostic 41356039Sbostic u_int 41456039Sbostic setflags(s) 41556039Sbostic char *s; 41656039Sbostic { 41756039Sbostic char *p; 41856039Sbostic 41956039Sbostic for (; isspace(*s); ++s); 42056039Sbostic if (*s == '\n') 42156039Sbostic return (0); 42256039Sbostic if ((p = index(s, '\n')) != NULL) 42356039Sbostic *p = '\0'; 42456039Sbostic if (!strcmp(s, "R_CURSOR")) 42556039Sbostic return (R_CURSOR); 42656749Sbostic if (!strcmp(s, "R_FIRST")) 42756749Sbostic return (R_FIRST); 42856039Sbostic if (!strcmp(s, "R_IAFTER")) 42956039Sbostic return (R_IAFTER); 43056039Sbostic if (!strcmp(s, "R_IBEFORE")) 43156039Sbostic return (R_IBEFORE); 43256039Sbostic if (!strcmp(s, "R_LAST")) 43356039Sbostic return (R_LAST); 43456039Sbostic if (!strcmp(s, "R_NEXT")) 43556039Sbostic return (R_NEXT); 43656749Sbostic if (!strcmp(s, "R_NOOVERWRITE")) 43756749Sbostic return (R_NOOVERWRITE); 43856039Sbostic if (!strcmp(s, "R_PREV")) 43956039Sbostic return (R_PREV); 44056749Sbostic if (!strcmp(s, "R_SETCURSOR")) 44156749Sbostic return (R_SETCURSOR); 44256039Sbostic err("line %lu: %s: unknown flag", lineno, s); 44356039Sbostic /* NOTREACHED */ 44456039Sbostic } 44556039Sbostic 44656039Sbostic DBTYPE 44756039Sbostic dbtype(s) 44856039Sbostic char *s; 44956039Sbostic { 45056039Sbostic if (!strcmp(s, "btree")) 45156039Sbostic return (DB_BTREE); 45256039Sbostic if (!strcmp(s, "hash")) 45356039Sbostic return (DB_HASH); 45456039Sbostic if (!strcmp(s, "recno")) 45556039Sbostic return (DB_RECNO); 45656039Sbostic err("%s: unknown type (use btree, hash or recno)", s); 45756039Sbostic /* NOTREACHED */ 45856039Sbostic } 45956039Sbostic 46056039Sbostic void * 46156039Sbostic setinfo(type, s) 46256039Sbostic DBTYPE type; 46356039Sbostic char *s; 46456039Sbostic { 46556039Sbostic static BTREEINFO ib; 46656039Sbostic static HASHINFO ih; 46756039Sbostic static RECNOINFO rh; 46856039Sbostic char *eq; 46956039Sbostic 47056039Sbostic if ((eq = index(s, '=')) == NULL) 47156039Sbostic err("%s: illegal structure set statement", s); 47256039Sbostic *eq++ = '\0'; 47356039Sbostic if (!isdigit(*eq)) 47456039Sbostic err("%s: structure set statement must be a number", s); 47556039Sbostic 47656039Sbostic switch(type) { 47756039Sbostic case DB_BTREE: 47856039Sbostic if (!strcmp("flags", s)) { 47956039Sbostic ib.flags = strtoul(eq, NULL, 0); 48056039Sbostic return (&ib); 48156039Sbostic } 48256039Sbostic if (!strcmp("cachesize", s)) { 48356039Sbostic ib.cachesize = strtoul(eq, NULL, 0); 48456039Sbostic return (&ib); 48556039Sbostic } 48656039Sbostic if (!strcmp("maxkeypage", s)) { 48756039Sbostic ib.maxkeypage = strtoul(eq, NULL, 0); 48856039Sbostic return (&ib); 48956039Sbostic } 49056039Sbostic if (!strcmp("minkeypage", s)) { 49156039Sbostic ib.minkeypage = strtoul(eq, NULL, 0); 49256039Sbostic return (&ib); 49356039Sbostic } 49456039Sbostic if (!strcmp("lorder", s)) { 49556039Sbostic ib.lorder = strtoul(eq, NULL, 0); 49656039Sbostic return (&ib); 49756039Sbostic } 49857456Sbostic if (!strcmp("psize", s)) { 49957456Sbostic ib.psize = strtoul(eq, NULL, 0); 50057456Sbostic return (&ib); 50157456Sbostic } 50256039Sbostic break; 50356039Sbostic case DB_HASH: 50456039Sbostic if (!strcmp("bsize", s)) { 50556039Sbostic ih.bsize = strtoul(eq, NULL, 0); 50660130Sbostic return (&ih); 50756039Sbostic } 50856039Sbostic if (!strcmp("ffactor", s)) { 50956039Sbostic ih.ffactor = strtoul(eq, NULL, 0); 51060130Sbostic return (&ih); 51156039Sbostic } 51256039Sbostic if (!strcmp("nelem", s)) { 51356039Sbostic ih.nelem = strtoul(eq, NULL, 0); 51460130Sbostic return (&ih); 51556039Sbostic } 51656039Sbostic if (!strcmp("cachesize", s)) { 51756039Sbostic ih.cachesize = strtoul(eq, NULL, 0); 51860130Sbostic return (&ih); 51956039Sbostic } 52056039Sbostic if (!strcmp("lorder", s)) { 52156039Sbostic ih.lorder = strtoul(eq, NULL, 0); 52260130Sbostic return (&ih); 52356039Sbostic } 52456039Sbostic break; 52556039Sbostic case DB_RECNO: 52656039Sbostic if (!strcmp("flags", s)) { 52756039Sbostic rh.flags = strtoul(eq, NULL, 0); 52860130Sbostic return (&rh); 52956039Sbostic } 53056039Sbostic if (!strcmp("cachesize", s)) { 53156039Sbostic rh.cachesize = strtoul(eq, NULL, 0); 53260130Sbostic return (&rh); 53356039Sbostic } 53456039Sbostic if (!strcmp("lorder", s)) { 53556039Sbostic rh.lorder = strtoul(eq, NULL, 0); 53660130Sbostic return (&rh); 53756039Sbostic } 53856039Sbostic if (!strcmp("reclen", s)) { 53956039Sbostic rh.reclen = strtoul(eq, NULL, 0); 54060130Sbostic return (&rh); 54156039Sbostic } 54256039Sbostic if (!strcmp("bval", s)) { 54356039Sbostic rh.bval = strtoul(eq, NULL, 0); 54460130Sbostic return (&rh); 54556039Sbostic } 546*60229Sbostic if (!strcmp("psize", s)) { 547*60229Sbostic rh.psize = strtoul(eq, NULL, 0); 548*60229Sbostic return (&rh); 549*60229Sbostic } 55056039Sbostic break; 55156039Sbostic } 55256039Sbostic err("%s: unknown structure value", s); 55356039Sbostic /* NOTREACHED */ 55456039Sbostic } 55556039Sbostic 55656039Sbostic void * 55756039Sbostic rfile(name, lenp) 55856039Sbostic char *name; 55956039Sbostic size_t *lenp; 56056039Sbostic { 56156039Sbostic struct stat sb; 56256039Sbostic void *p; 56356039Sbostic int fd; 56456039Sbostic char *np; 56556039Sbostic 56656039Sbostic for (; isspace(*name); ++name); 56756039Sbostic if ((np = index(name, '\n')) != NULL) 56856039Sbostic *np = '\0'; 56956039Sbostic if ((fd = open(name, O_RDONLY, 0)) < 0 || 57056039Sbostic fstat(fd, &sb)) 57156039Sbostic err("%s: %s\n", name, strerror(errno)); 57258312Sbostic if (sb.st_size > (off_t)SIZE_T_MAX) 57356039Sbostic err("%s: %s\n", name, strerror(E2BIG)); 57456039Sbostic if ((p = malloc((u_int)sb.st_size)) == NULL) 57556039Sbostic err("%s", strerror(errno)); 57656039Sbostic (void)read(fd, p, (int)sb.st_size); 57756039Sbostic *lenp = sb.st_size; 57856489Sbostic (void)close(fd); 57956039Sbostic return (p); 58056039Sbostic } 58156039Sbostic 58256039Sbostic void * 58356039Sbostic xmalloc(text, len) 58456039Sbostic char *text; 58556039Sbostic size_t len; 58656039Sbostic { 58756039Sbostic void *p; 58856039Sbostic 58956039Sbostic if ((p = malloc(len)) == NULL) 59056039Sbostic err("%s", strerror(errno)); 59158015Sbostic memmove(p, text, len); 59256039Sbostic return (p); 59356039Sbostic } 59456039Sbostic 59556039Sbostic void 59656039Sbostic usage() 59756039Sbostic { 59856039Sbostic (void)fprintf(stderr, 59959626Sbostic "usage: dbtest [-f file] [-i info] [-o file] type script\n"); 60056039Sbostic exit(1); 60156039Sbostic } 60256039Sbostic 60356039Sbostic #if __STDC__ 60456039Sbostic #include <stdarg.h> 60556039Sbostic #else 60656039Sbostic #include <varargs.h> 60756039Sbostic #endif 60856039Sbostic 60956039Sbostic void 61056039Sbostic #if __STDC__ 61156039Sbostic err(const char *fmt, ...) 61256039Sbostic #else 61356039Sbostic err(fmt, va_alist) 61456039Sbostic char *fmt; 61556039Sbostic va_dcl 61656039Sbostic #endif 61756039Sbostic { 61856039Sbostic va_list ap; 61956039Sbostic #if __STDC__ 62056039Sbostic va_start(ap, fmt); 62156039Sbostic #else 62256039Sbostic va_start(ap); 62356039Sbostic #endif 62456039Sbostic (void)fprintf(stderr, "dbtest: "); 62556039Sbostic (void)vfprintf(stderr, fmt, ap); 62656039Sbostic va_end(ap); 62756039Sbostic (void)fprintf(stderr, "\n"); 62856039Sbostic exit(1); 62956039Sbostic /* NOTREACHED */ 63056039Sbostic } 631