156039Sbostic /*- 261220Sbostic * Copyright (c) 1992, 1993 361220Sbostic * The Regents of the University of California. All rights reserved. 456039Sbostic * 556039Sbostic * %sccs.include.redist.c% 656039Sbostic */ 756039Sbostic 856039Sbostic #ifndef lint 961220Sbostic static char copyright[] = 1061220Sbostic "@(#) Copyright (c) 1992, 1993\n\ 1161220Sbostic The Regents of the University of California. All rights reserved.\n"; 1256039Sbostic #endif /* not lint */ 1356039Sbostic 1456039Sbostic #ifndef lint 15*64453Sbostic static char sccsid[] = "@(#)dbtest.c 8.5 (Berkeley) 09/07/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> 3164288Sbostic #include "../PORT/include/compat.h" 3259492Sbostic 3356557Sbostic enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA }; 3456039Sbostic 3556557Sbostic void compare __P((DBT *, DBT *)); 3656039Sbostic DBTYPE dbtype __P((char *)); 3756992Sbostic void dump __P((DB *, int)); 3856039Sbostic void err __P((const char *, ...)); 3956039Sbostic void get __P((DB *, DBT *)); 4056557Sbostic void getdata __P((DB *, DBT *, DBT *)); 4156039Sbostic void put __P((DB *, DBT *, DBT *)); 4256039Sbostic void rem __P((DB *, DBT *)); 4356039Sbostic void *rfile __P((char *, size_t *)); 4456059Sbostic void seq __P((DB *, DBT *)); 4556039Sbostic u_int setflags __P((char *)); 4656039Sbostic void *setinfo __P((DBTYPE, char *)); 4756039Sbostic void usage __P((void)); 4856039Sbostic void *xmalloc __P((char *, size_t)); 4956039Sbostic 5056039Sbostic DBTYPE type; 5156039Sbostic void *infop; 5256039Sbostic u_long lineno; 5356039Sbostic u_int flags; 5456039Sbostic int ofd = STDOUT_FILENO; 5556039Sbostic 5657456Sbostic DB *XXdbp; /* Global for gdb. */ 5757456Sbostic 5856039Sbostic int 5956039Sbostic main(argc, argv) 6056039Sbostic int argc; 6156039Sbostic char *argv[]; 6256039Sbostic { 6357461Sbostic extern int optind; 6457461Sbostic extern char *optarg; 6556039Sbostic enum S command, state; 6656039Sbostic DB *dbp; 6756557Sbostic DBT data, key, keydata; 6856039Sbostic size_t len; 69*64453Sbostic int ch, oflags; 7059626Sbostic char *fname, *infoarg, *p, buf[8 * 1024]; 7156039Sbostic 7256039Sbostic infoarg = NULL; 7359626Sbostic fname = NULL; 74*64453Sbostic oflags = O_CREAT | O_RDWR; 75*64453Sbostic while ((ch = getopt(argc, argv, "f:i:lo:")) != EOF) 7656039Sbostic switch(ch) { 7759626Sbostic case 'f': 7859626Sbostic fname = optarg; 7959626Sbostic break; 8056039Sbostic case 'i': 8156039Sbostic infoarg = optarg; 8256039Sbostic break; 83*64453Sbostic case 'l': 84*64453Sbostic oflags |= DB_LOCK; 85*64453Sbostic break; 8656039Sbostic case 'o': 8756039Sbostic if ((ofd = open(optarg, 8856039Sbostic O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 8956039Sbostic err("%s: %s", optarg, strerror(errno)); 9056039Sbostic break; 9156039Sbostic case '?': 9256039Sbostic default: 9356039Sbostic usage(); 9456039Sbostic } 9556039Sbostic argc -= optind; 9656039Sbostic argv += optind; 9756039Sbostic 9856039Sbostic if (argc != 2) 9956039Sbostic usage(); 10056039Sbostic 10156039Sbostic /* Set the type. */ 10256039Sbostic type = dbtype(*argv++); 10356039Sbostic 10456039Sbostic /* Open the descriptor file. */ 10556039Sbostic if (freopen(*argv, "r", stdin) == NULL) 10656039Sbostic err("%s: %s", *argv, strerror(errno)); 10756039Sbostic 10856039Sbostic /* Set up the db structure as necessary. */ 10956039Sbostic if (infoarg == NULL) 11056039Sbostic infop = NULL; 11156039Sbostic else 11257463Sbostic for (p = strtok(infoarg, ",\t "); p != NULL; 11357463Sbostic p = strtok(0, ",\t ")) 11456039Sbostic if (*p != '\0') 11556039Sbostic infop = setinfo(type, p); 11656039Sbostic 11759626Sbostic /* Open the DB. */ 11859626Sbostic if (fname == NULL) { 11960129Smargo p = getenv("TMPDIR"); 12060129Smargo if (p == NULL) 12160129Smargo p = "/var/tmp"; 12260129Smargo (void)sprintf(buf, "%s/__dbtest", p); 12360129Smargo fname = buf; 12460129Smargo (void)unlink(buf); 12559626Sbostic } 12659626Sbostic if ((dbp = dbopen(fname, 127*64453Sbostic oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL) 12856039Sbostic err("dbopen: %s", strerror(errno)); 12957456Sbostic XXdbp = dbp; 13056039Sbostic 13156039Sbostic state = COMMAND; 13256039Sbostic for (lineno = 1; 13356039Sbostic (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) { 13456039Sbostic len = strlen(buf); 13556039Sbostic switch(*p) { 13656557Sbostic case 'c': /* compare */ 13756557Sbostic if (state != COMMAND) 13856557Sbostic err("line %lu: not expecting command", lineno); 13956557Sbostic state = KEY; 14056557Sbostic command = COMPARE; 14156557Sbostic break; 14256059Sbostic case 'e': /* echo */ 14356059Sbostic if (state != COMMAND) 14456059Sbostic err("line %lu: not expecting command", lineno); 14556557Sbostic /* Don't display the newline, if CR at EOL. */ 14656557Sbostic if (p[len - 2] == '\r') 14756557Sbostic --len; 14856557Sbostic if (write(ofd, p + 1, len - 1) != len - 1) 14956557Sbostic err("write: %s", strerror(errno)); 15056059Sbostic break; 15156039Sbostic case 'g': /* get */ 15256039Sbostic if (state != COMMAND) 15356039Sbostic err("line %lu: not expecting command", lineno); 15456039Sbostic state = KEY; 15556039Sbostic command = GET; 15656039Sbostic break; 15756039Sbostic case 'p': /* put */ 15856039Sbostic if (state != COMMAND) 15956039Sbostic err("line %lu: not expecting command", lineno); 16056039Sbostic state = KEY; 16156039Sbostic command = PUT; 16256039Sbostic break; 16356039Sbostic case 'r': /* remove */ 16456039Sbostic if (state != COMMAND) 16556039Sbostic err("line %lu: not expecting command", lineno); 16656039Sbostic state = KEY; 16756039Sbostic command = REMOVE; 16856039Sbostic break; 16956039Sbostic case 's': /* seq */ 17056039Sbostic if (state != COMMAND) 17156039Sbostic err("line %lu: not expecting command", lineno); 17256059Sbostic if (flags == R_CURSOR) { 17356059Sbostic state = KEY; 17456059Sbostic command = SEQ; 17556059Sbostic } else 17656059Sbostic seq(dbp, &key); 17756039Sbostic break; 17856039Sbostic case 'f': 17956059Sbostic flags = setflags(p + 1); 18056039Sbostic break; 18156039Sbostic case 'D': /* data file */ 18256039Sbostic if (state != DATA) 18356039Sbostic err("line %lu: not expecting data", lineno); 18456039Sbostic data.data = rfile(p + 1, &data.size); 18556992Sbostic goto ldata; 18656039Sbostic case 'd': /* data */ 18756039Sbostic if (state != DATA) 18856039Sbostic err("line %lu: not expecting data", lineno); 18956557Sbostic data.data = xmalloc(p + 1, len - 1); 19056557Sbostic data.size = len - 1; 19156992Sbostic ldata: switch(command) { 19256557Sbostic case COMPARE: 19356557Sbostic compare(&keydata, &data); 19456557Sbostic break; 19556557Sbostic case PUT: 19656557Sbostic put(dbp, &key, &data); 19756557Sbostic break; 19856557Sbostic default: 19956039Sbostic err("line %lu: command doesn't take data", 20056039Sbostic lineno); 20156557Sbostic } 20259493Sbostic if (type != DB_RECNO) 20359493Sbostic free(key.data); 20456039Sbostic free(data.data); 20556039Sbostic state = COMMAND; 20656039Sbostic break; 20756039Sbostic case 'K': /* key file */ 20856039Sbostic if (state != KEY) 20956039Sbostic err("line %lu: not expecting a key", lineno); 21056039Sbostic if (type == DB_RECNO) 21156039Sbostic err("line %lu: 'K' not available for recno", 21256039Sbostic lineno); 21356039Sbostic key.data = rfile(p + 1, &key.size); 21456992Sbostic goto lkey; 21556039Sbostic case 'k': /* key */ 21656039Sbostic if (state != KEY) 21756039Sbostic err("line %lu: not expecting a key", lineno); 21856039Sbostic if (type == DB_RECNO) { 21956039Sbostic static recno_t recno; 22064443Sbostic recno = atoi(p + 1); 22156039Sbostic key.data = &recno; 22256039Sbostic key.size = sizeof(recno); 22356039Sbostic } else { 22456039Sbostic key.data = xmalloc(p + 1, len - 1); 22556039Sbostic key.size = len - 1; 22656039Sbostic } 22756992Sbostic lkey: switch(command) { 22856557Sbostic case COMPARE: 22956557Sbostic getdata(dbp, &key, &keydata); 23056557Sbostic state = DATA; 23156557Sbostic break; 23256039Sbostic case GET: 23356039Sbostic get(dbp, &key); 23456039Sbostic if (type != DB_RECNO) 23556039Sbostic free(key.data); 23656039Sbostic state = COMMAND; 23756039Sbostic break; 23856039Sbostic case PUT: 23956039Sbostic state = DATA; 24056039Sbostic break; 24156039Sbostic case REMOVE: 24256039Sbostic rem(dbp, &key); 24356039Sbostic if (type != DB_RECNO) 24456039Sbostic free(key.data); 24556039Sbostic state = COMMAND; 24656039Sbostic break; 24756059Sbostic case SEQ: 24856059Sbostic seq(dbp, &key); 24956059Sbostic if (type != DB_RECNO) 25056059Sbostic free(key.data); 25156059Sbostic state = COMMAND; 25256059Sbostic break; 25356039Sbostic default: 25456039Sbostic err("line %lu: command doesn't take a key", 25556039Sbostic lineno); 25656039Sbostic } 25756039Sbostic break; 25856992Sbostic case 'o': 25956992Sbostic dump(dbp, p[1] == 'r'); 26056992Sbostic break; 26156039Sbostic default: 26256039Sbostic err("line %lu: %s: unknown command character", 26356059Sbostic p, lineno); 26456039Sbostic } 26556039Sbostic } 26659626Sbostic if (dbp->close(dbp)) 26759626Sbostic err("db->close: %s", strerror(errno)); 26856039Sbostic (void)close(ofd); 26956039Sbostic exit(0); 27056039Sbostic } 27156039Sbostic 27256059Sbostic #define NOOVERWRITE "put failed, would overwrite key\n" 27356059Sbostic #define NOSUCHKEY "get failed, no such key\n" 27456059Sbostic 27556039Sbostic void 27656557Sbostic compare(db1, db2) 27756557Sbostic DBT *db1, *db2; 27856557Sbostic { 27956557Sbostic register size_t len; 28056557Sbostic register u_char *p1, *p2; 28156557Sbostic 28256557Sbostic if (db1->size != db2->size) 28356557Sbostic printf("compare failed: key->data len %lu != data len %lu\n", 28456557Sbostic db1->size, db2->size); 28556557Sbostic 28656557Sbostic len = MIN(db1->size, db2->size); 28756557Sbostic for (p1 = db1->data, p2 = db2->data; len--;) 28856557Sbostic if (*p1++ != *p2++) { 28956557Sbostic printf("compare failed at offset %d\n", 29056557Sbostic p1 - (u_char *)db1->data); 29156557Sbostic break; 29256557Sbostic } 29356557Sbostic } 29456557Sbostic 29556557Sbostic void 29656039Sbostic get(dbp, kp) 29756039Sbostic DB *dbp; 29856039Sbostic DBT *kp; 29956039Sbostic { 30056039Sbostic DBT data; 30156039Sbostic 30256059Sbostic switch(dbp->get(dbp, kp, &data, flags)) { 30356059Sbostic case 0: 30456059Sbostic (void)write(ofd, data.data, data.size); 30556059Sbostic break; 30656059Sbostic case -1: 30756039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 30856059Sbostic /* NOTREACHED */ 30956059Sbostic case 1: 31056059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 31157456Sbostic (void)fprintf(stderr, "%d: %.*s: %s\n", 31257456Sbostic lineno, kp->size, kp->data, NOSUCHKEY); 31356059Sbostic break; 31456059Sbostic } 31556039Sbostic } 31656039Sbostic 31756039Sbostic void 31856557Sbostic getdata(dbp, kp, dp) 31956557Sbostic DB *dbp; 32056557Sbostic DBT *kp, *dp; 32156557Sbostic { 32256557Sbostic switch(dbp->get(dbp, kp, dp, flags)) { 32356557Sbostic case 0: 32456557Sbostic return; 32556557Sbostic case -1: 32656557Sbostic err("line %lu: getdata: %s", lineno, strerror(errno)); 32756557Sbostic /* NOTREACHED */ 32856557Sbostic case 1: 32956557Sbostic err("line %lu: get failed, no such key", lineno); 33056557Sbostic /* NOTREACHED */ 33156557Sbostic } 33256557Sbostic } 33356557Sbostic 33456557Sbostic void 33556039Sbostic put(dbp, kp, dp) 33656039Sbostic DB *dbp; 33756039Sbostic DBT *kp, *dp; 33856039Sbostic { 33956059Sbostic switch(dbp->put(dbp, kp, dp, flags)) { 34056059Sbostic case 0: 34156059Sbostic break; 34256059Sbostic case -1: 34356039Sbostic err("line %lu: put: %s", lineno, strerror(errno)); 34456059Sbostic /* NOTREACHED */ 34556059Sbostic case 1: 34656059Sbostic (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1); 34756059Sbostic break; 34856059Sbostic } 34956039Sbostic } 35056039Sbostic 35156039Sbostic void 35256039Sbostic rem(dbp, kp) 35356039Sbostic DB *dbp; 35456039Sbostic DBT *kp; 35556039Sbostic { 35656059Sbostic switch(dbp->del(dbp, kp, flags)) { 35756059Sbostic case 0: 35856059Sbostic break; 35956059Sbostic case -1: 36056039Sbostic err("line %lu: get: %s", lineno, strerror(errno)); 36156059Sbostic /* NOTREACHED */ 36256059Sbostic case 1: 36356059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 36456059Sbostic break; 36556059Sbostic } 36656039Sbostic } 36756039Sbostic 36856039Sbostic void 36956059Sbostic seq(dbp, kp) 37056039Sbostic DB *dbp; 37156059Sbostic DBT *kp; 37256039Sbostic { 37356059Sbostic DBT data; 37456039Sbostic 37556059Sbostic switch(dbp->seq(dbp, kp, &data, flags)) { 37656059Sbostic case 0: 37756059Sbostic (void)write(ofd, data.data, data.size); 37856059Sbostic break; 37956059Sbostic case -1: 38056039Sbostic err("line %lu: seq: %s", lineno, strerror(errno)); 38156059Sbostic /* NOTREACHED */ 38256059Sbostic case 1: 38356059Sbostic (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 38456059Sbostic break; 38556059Sbostic } 38656039Sbostic } 38756992Sbostic 38856992Sbostic void 38956992Sbostic dump(dbp, rev) 39056992Sbostic DB *dbp; 39156992Sbostic int rev; 39256992Sbostic { 39356992Sbostic DBT key, data; 39456992Sbostic int flags, nflags; 39556992Sbostic 39656992Sbostic if (rev) { 39756992Sbostic flags = R_LAST; 39856992Sbostic nflags = R_PREV; 39956992Sbostic } else { 40056992Sbostic flags = R_FIRST; 40156992Sbostic nflags = R_NEXT; 40256992Sbostic } 40356992Sbostic for (;; flags = nflags) 40456992Sbostic switch(dbp->seq(dbp, &key, &data, flags)) { 40556992Sbostic case 0: 40656992Sbostic (void)write(ofd, data.data, data.size); 40756992Sbostic break; 40856992Sbostic case 1: 40956992Sbostic goto done; 41056992Sbostic case -1: 41156992Sbostic err("line %lu: (dump) seq: %s", 41256992Sbostic lineno, strerror(errno)); 41356992Sbostic /* NOTREACHED */ 41456992Sbostic } 41556992Sbostic done: return; 41656992Sbostic } 41756039Sbostic 41856039Sbostic u_int 41956039Sbostic setflags(s) 42056039Sbostic char *s; 42156039Sbostic { 42256039Sbostic char *p; 42356039Sbostic 42456039Sbostic for (; isspace(*s); ++s); 42556039Sbostic if (*s == '\n') 42656039Sbostic return (0); 42756039Sbostic if ((p = index(s, '\n')) != NULL) 42856039Sbostic *p = '\0'; 42956039Sbostic if (!strcmp(s, "R_CURSOR")) 43056039Sbostic return (R_CURSOR); 43156749Sbostic if (!strcmp(s, "R_FIRST")) 43256749Sbostic return (R_FIRST); 43356039Sbostic if (!strcmp(s, "R_IAFTER")) 43456039Sbostic return (R_IAFTER); 43556039Sbostic if (!strcmp(s, "R_IBEFORE")) 43656039Sbostic return (R_IBEFORE); 43756039Sbostic if (!strcmp(s, "R_LAST")) 43856039Sbostic return (R_LAST); 43956039Sbostic if (!strcmp(s, "R_NEXT")) 44056039Sbostic return (R_NEXT); 44156749Sbostic if (!strcmp(s, "R_NOOVERWRITE")) 44256749Sbostic return (R_NOOVERWRITE); 44356039Sbostic if (!strcmp(s, "R_PREV")) 44456039Sbostic return (R_PREV); 44556749Sbostic if (!strcmp(s, "R_SETCURSOR")) 44656749Sbostic return (R_SETCURSOR); 44756039Sbostic err("line %lu: %s: unknown flag", lineno, s); 44856039Sbostic /* NOTREACHED */ 44956039Sbostic } 45056039Sbostic 45156039Sbostic DBTYPE 45256039Sbostic dbtype(s) 45356039Sbostic char *s; 45456039Sbostic { 45556039Sbostic if (!strcmp(s, "btree")) 45656039Sbostic return (DB_BTREE); 45756039Sbostic if (!strcmp(s, "hash")) 45856039Sbostic return (DB_HASH); 45956039Sbostic if (!strcmp(s, "recno")) 46056039Sbostic return (DB_RECNO); 46156039Sbostic err("%s: unknown type (use btree, hash or recno)", s); 46256039Sbostic /* NOTREACHED */ 46356039Sbostic } 46456039Sbostic 46556039Sbostic void * 46656039Sbostic setinfo(type, s) 46756039Sbostic DBTYPE type; 46856039Sbostic char *s; 46956039Sbostic { 47056039Sbostic static BTREEINFO ib; 47156039Sbostic static HASHINFO ih; 47256039Sbostic static RECNOINFO rh; 47356039Sbostic char *eq; 47456039Sbostic 47556039Sbostic if ((eq = index(s, '=')) == NULL) 47656039Sbostic err("%s: illegal structure set statement", s); 47756039Sbostic *eq++ = '\0'; 47856039Sbostic if (!isdigit(*eq)) 47956039Sbostic err("%s: structure set statement must be a number", s); 48056039Sbostic 48156039Sbostic switch(type) { 48256039Sbostic case DB_BTREE: 48356039Sbostic if (!strcmp("flags", s)) { 48464288Sbostic ib.flags = atoi(eq); 48556039Sbostic return (&ib); 48656039Sbostic } 48756039Sbostic if (!strcmp("cachesize", s)) { 48864288Sbostic ib.cachesize = atoi(eq); 48956039Sbostic return (&ib); 49056039Sbostic } 49156039Sbostic if (!strcmp("maxkeypage", s)) { 49264288Sbostic ib.maxkeypage = atoi(eq); 49356039Sbostic return (&ib); 49456039Sbostic } 49556039Sbostic if (!strcmp("minkeypage", s)) { 49664288Sbostic ib.minkeypage = atoi(eq); 49756039Sbostic return (&ib); 49856039Sbostic } 49956039Sbostic if (!strcmp("lorder", s)) { 50064288Sbostic ib.lorder = atoi(eq); 50156039Sbostic return (&ib); 50256039Sbostic } 50357456Sbostic if (!strcmp("psize", s)) { 50464288Sbostic ib.psize = atoi(eq); 50557456Sbostic return (&ib); 50657456Sbostic } 50756039Sbostic break; 50856039Sbostic case DB_HASH: 50956039Sbostic if (!strcmp("bsize", s)) { 51064288Sbostic ih.bsize = atoi(eq); 51160130Sbostic return (&ih); 51256039Sbostic } 51356039Sbostic if (!strcmp("ffactor", s)) { 51464288Sbostic ih.ffactor = atoi(eq); 51560130Sbostic return (&ih); 51656039Sbostic } 51756039Sbostic if (!strcmp("nelem", s)) { 51864288Sbostic ih.nelem = atoi(eq); 51960130Sbostic return (&ih); 52056039Sbostic } 52156039Sbostic if (!strcmp("cachesize", s)) { 52264288Sbostic ih.cachesize = atoi(eq); 52360130Sbostic return (&ih); 52456039Sbostic } 52556039Sbostic if (!strcmp("lorder", s)) { 52664288Sbostic ih.lorder = atoi(eq); 52760130Sbostic return (&ih); 52856039Sbostic } 52956039Sbostic break; 53056039Sbostic case DB_RECNO: 53156039Sbostic if (!strcmp("flags", s)) { 53264288Sbostic rh.flags = atoi(eq); 53360130Sbostic return (&rh); 53456039Sbostic } 53556039Sbostic if (!strcmp("cachesize", s)) { 53664288Sbostic rh.cachesize = atoi(eq); 53760130Sbostic return (&rh); 53856039Sbostic } 53956039Sbostic if (!strcmp("lorder", s)) { 54064288Sbostic rh.lorder = atoi(eq); 54160130Sbostic return (&rh); 54256039Sbostic } 54356039Sbostic if (!strcmp("reclen", s)) { 54464288Sbostic rh.reclen = atoi(eq); 54560130Sbostic return (&rh); 54656039Sbostic } 54756039Sbostic if (!strcmp("bval", s)) { 54864288Sbostic rh.bval = atoi(eq); 54960130Sbostic return (&rh); 55056039Sbostic } 55160229Sbostic if (!strcmp("psize", s)) { 55264288Sbostic rh.psize = atoi(eq); 55360229Sbostic return (&rh); 55460229Sbostic } 55556039Sbostic break; 55656039Sbostic } 55756039Sbostic err("%s: unknown structure value", s); 55856039Sbostic /* NOTREACHED */ 55956039Sbostic } 56056039Sbostic 56156039Sbostic void * 56256039Sbostic rfile(name, lenp) 56356039Sbostic char *name; 56456039Sbostic size_t *lenp; 56556039Sbostic { 56656039Sbostic struct stat sb; 56756039Sbostic void *p; 56856039Sbostic int fd; 56956039Sbostic char *np; 57056039Sbostic 57156039Sbostic for (; isspace(*name); ++name); 57256039Sbostic if ((np = index(name, '\n')) != NULL) 57356039Sbostic *np = '\0'; 57456039Sbostic if ((fd = open(name, O_RDONLY, 0)) < 0 || 57556039Sbostic fstat(fd, &sb)) 57656039Sbostic err("%s: %s\n", name, strerror(errno)); 57764367Sbostic #ifdef NOT_PORTABLE 57858312Sbostic if (sb.st_size > (off_t)SIZE_T_MAX) 57956039Sbostic err("%s: %s\n", name, strerror(E2BIG)); 58064367Sbostic #endif 58156039Sbostic if ((p = malloc((u_int)sb.st_size)) == NULL) 58256039Sbostic err("%s", strerror(errno)); 58356039Sbostic (void)read(fd, p, (int)sb.st_size); 58456039Sbostic *lenp = sb.st_size; 58556489Sbostic (void)close(fd); 58656039Sbostic return (p); 58756039Sbostic } 58856039Sbostic 58956039Sbostic void * 59056039Sbostic xmalloc(text, len) 59156039Sbostic char *text; 59256039Sbostic size_t len; 59356039Sbostic { 59456039Sbostic void *p; 59556039Sbostic 59656039Sbostic if ((p = malloc(len)) == NULL) 59756039Sbostic err("%s", strerror(errno)); 59858015Sbostic memmove(p, text, len); 59956039Sbostic return (p); 60056039Sbostic } 60156039Sbostic 60256039Sbostic void 60356039Sbostic usage() 60456039Sbostic { 60556039Sbostic (void)fprintf(stderr, 606*64453Sbostic "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n"); 60756039Sbostic exit(1); 60856039Sbostic } 60956039Sbostic 61056039Sbostic #if __STDC__ 61156039Sbostic #include <stdarg.h> 61256039Sbostic #else 61356039Sbostic #include <varargs.h> 61456039Sbostic #endif 61556039Sbostic 61656039Sbostic void 61756039Sbostic #if __STDC__ 61856039Sbostic err(const char *fmt, ...) 61956039Sbostic #else 62056039Sbostic err(fmt, va_alist) 62156039Sbostic char *fmt; 62256039Sbostic va_dcl 62356039Sbostic #endif 62456039Sbostic { 62556039Sbostic va_list ap; 62656039Sbostic #if __STDC__ 62756039Sbostic va_start(ap, fmt); 62856039Sbostic #else 62956039Sbostic va_start(ap); 63056039Sbostic #endif 63156039Sbostic (void)fprintf(stderr, "dbtest: "); 63256039Sbostic (void)vfprintf(stderr, fmt, ap); 63356039Sbostic va_end(ap); 63456039Sbostic (void)fprintf(stderr, "\n"); 63556039Sbostic exit(1); 63656039Sbostic /* NOTREACHED */ 63756039Sbostic } 638