~ubuntu-branches/ubuntu/oneiric/likewise-open/oneiric

« back to all changes in this revision

Viewing changes to krb5/src/plugins/kdb/db2/libdb2/test/dbtest.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Salley
  • Date: 2010-11-22 12:06:00 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20101122120600-8lba1fpceot71wlb
Tags: 6.0.0.53010-1
Likewise Open 6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * Copyright (c) 1992, 1993, 1994
 
3
 *      The Regents of the University of California.  All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 * 1. Redistributions of source code must retain the above copyright
 
9
 *    notice, this list of conditions and the following disclaimer.
 
10
 * 2. Redistributions in binary form must reproduce the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer in the
 
12
 *    documentation and/or other materials provided with the distribution.
 
13
 * 3. All advertising materials mentioning features or use of this software
 
14
 *    must display the following acknowledgement:
 
15
 *      This product includes software developed by the University of
 
16
 *      California, Berkeley and its contributors.
 
17
 * 4. Neither the name of the University nor the names of its contributors
 
18
 *    may be used to endorse or promote products derived from this software
 
19
 *    without specific prior written permission.
 
20
 *
 
21
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
31
 * SUCH DAMAGE.
 
32
 */
 
33
 
 
34
#if !defined(lint) && defined(LIBC_SCCS)
 
35
static char copyright[] =
 
36
"@(#) Copyright (c) 1992, 1993, 1994\n\
 
37
        The Regents of the University of California.  All rights reserved.\n";
 
38
#endif /* not lint */
 
39
 
 
40
#if !defined(lint) && defined(LIBC_SCCS)
 
41
static char sccsid[] = "@(#)dbtest.c    8.17 (Berkeley) 9/1/94";
 
42
#endif /* not lint */
 
43
 
 
44
#include <sys/param.h>
 
45
#include <sys/stat.h>
 
46
 
 
47
#include <ctype.h>
 
48
#include <errno.h>
 
49
#include <fcntl.h>
 
50
#include <limits.h>
 
51
#include <stdio.h>
 
52
#include <stdlib.h>
 
53
#include <string.h>
 
54
#include <unistd.h>
 
55
 
 
56
#include "db-int.h"
 
57
#ifdef STATISTICS
 
58
#include "btree.h"
 
59
#endif
 
60
 
 
61
enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
 
62
 
 
63
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
 
64
#define ATTR(x) __attribute__(x)
 
65
#else
 
66
#define ATTR(x)
 
67
#endif
 
68
 
 
69
void     compare __P((DBT *, DBT *));
 
70
DBTYPE   dbtype __P((char *));
 
71
void     dump __P((DB *, int));
 
72
void     err __P((const char *, ...)) ATTR ((__format__(__printf__,1,2))) ATTR ((__noreturn__));
 
73
void     get __P((DB *, DBT *));
 
74
void     getdata __P((DB *, DBT *, DBT *));
 
75
void     put __P((DB *, DBT *, DBT *));
 
76
void     rem __P((DB *, DBT *));
 
77
char    *sflags __P((int));
 
78
void     synk __P((DB *));
 
79
void    *rfile __P((char *, size_t *));
 
80
void     seq __P((DB *, DBT *));
 
81
u_int    setflags __P((char *));
 
82
void    *setinfo __P((DBTYPE, char *));
 
83
void     usage __P((void));
 
84
void    *xmalloc __P((char *, size_t));
 
85
 
 
86
DBTYPE type;                            /* Database type. */
 
87
void *infop;                            /* Iflags. */
 
88
u_long lineno;                          /* Current line in test script. */
 
89
u_int flags;                            /* Current DB flags. */
 
90
int ofd = STDOUT_FILENO;                /* Standard output fd. */
 
91
 
 
92
DB *XXdbp;                              /* Global for gdb. */
 
93
int XXlineno;                           /* Fast breakpoint for gdb. */
 
94
 
 
95
int
 
96
main(argc, argv)
 
97
        int argc;
 
98
        char *argv[];
 
99
{
 
100
        extern int optind;
 
101
        extern char *optarg;
 
102
        enum S command, state;
 
103
        DB *dbp;
 
104
        DBT data, key, keydata;
 
105
        size_t len;
 
106
        int ch, oflags, sflag;
 
107
        char *fname, *infoarg, *p, *t, buf[8 * 1024];
 
108
 
 
109
        infoarg = NULL;
 
110
        fname = NULL;
 
111
        oflags = O_CREAT | O_RDWR | O_BINARY;
 
112
        sflag = 0;
 
113
        while ((ch = getopt(argc, argv, "f:i:lo:s")) != -1)
 
114
                switch (ch) {
 
115
                case 'f':
 
116
                        fname = optarg;
 
117
                        break;
 
118
                case 'i':
 
119
                        infoarg = optarg;
 
120
                        break;
 
121
                case 'l':
 
122
                        oflags |= DB_LOCK;
 
123
                        break;
 
124
                case 'o':
 
125
                        if ((ofd = open(optarg,
 
126
                            O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
 
127
                                err("%s: %s", optarg, strerror(errno));
 
128
                        break;
 
129
                case 's':
 
130
                        sflag = 1;
 
131
                        break;
 
132
                case '?':
 
133
                default:
 
134
                        usage();
 
135
                }
 
136
        argc -= optind;
 
137
        argv += optind;
 
138
 
 
139
        if (argc != 2)
 
140
                usage();
 
141
 
 
142
        /* Set the type. */
 
143
        type = dbtype(*argv++);
 
144
 
 
145
        /* Open the descriptor file. */
 
146
        if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
 
147
            err("%s: %s", *argv, strerror(errno));
 
148
 
 
149
        /* Set up the db structure as necessary. */
 
150
        if (infoarg == NULL)
 
151
                infop = NULL;
 
152
        else
 
153
                for (p = strtok(infoarg, ",\t "); p != NULL;
 
154
                    p = strtok(0, ",\t "))
 
155
                        if (*p != '\0')
 
156
                                infop = setinfo(type, p);
 
157
 
 
158
        /*
 
159
         * Open the DB.  Delete any preexisting copy, you almost never
 
160
         * want it around, and it often screws up tests.
 
161
         */
 
162
        if (fname == NULL) {
 
163
                p = getenv("TMPDIR");
 
164
                if (p == NULL)
 
165
                        p = "/var/tmp";
 
166
                (void)snprintf(buf, sizeof(buf), "%s/__dbtest", p);
 
167
                fname = buf;
 
168
                (void)unlink(buf);
 
169
        } else  if (!sflag)
 
170
                (void)unlink(fname);
 
171
 
 
172
        if ((dbp = dbopen(fname,
 
173
            oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
 
174
                err("dbopen: %s", strerror(errno));
 
175
        XXdbp = dbp;
 
176
 
 
177
        state = COMMAND;
 
178
        for (lineno = 1;
 
179
            (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
 
180
                /* Delete the newline, displaying the key/data is easier. */
 
181
                if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
 
182
                        *t = '\0';
 
183
                if ((len = strlen(buf)) == 0 || isspace((int) *p) || *p == '#')
 
184
                        continue;
 
185
 
 
186
                /* Convenient gdb break point. */
 
187
                if (XXlineno == lineno)
 
188
                        XXlineno = 1;
 
189
                switch (*p) {
 
190
                case 'c':                       /* compare */
 
191
                        if (state != COMMAND)
 
192
                                err("line %lu: not expecting command", lineno);
 
193
                        state = KEY;
 
194
                        command = COMPARE;
 
195
                        break;
 
196
                case 'e':                       /* echo */
 
197
                        if (state != COMMAND)
 
198
                                err("line %lu: not expecting command", lineno);
 
199
                        /* Don't display the newline, if CR at EOL. */
 
200
                        if (p[len - 2] == '\r')
 
201
                                --len;
 
202
                        if (write(ofd, p + 1, len - 1) != len - 1 ||
 
203
                            write(ofd, "\n", 1) != 1)
 
204
                                err("write: %s", strerror(errno));
 
205
                        break;
 
206
                case 'g':                       /* get */
 
207
                        if (state != COMMAND)
 
208
                                err("line %lu: not expecting command", lineno);
 
209
                        state = KEY;
 
210
                        command = GET;
 
211
                        break;
 
212
                case 'p':                       /* put */
 
213
                        if (state != COMMAND)
 
214
                                err("line %lu: not expecting command", lineno);
 
215
                        state = KEY;
 
216
                        command = PUT;
 
217
                        break;
 
218
                case 'r':                       /* remove */
 
219
                        if (state != COMMAND)
 
220
                                err("line %lu: not expecting command", lineno);
 
221
                        if (flags == R_CURSOR) {
 
222
                                rem(dbp, &key);
 
223
                                state = COMMAND;
 
224
                        } else {
 
225
                                state = KEY;
 
226
                                command = REMOVE;
 
227
                        }
 
228
                        break;
 
229
                case 'S':                       /* sync */
 
230
                        if (state != COMMAND)
 
231
                                err("line %lu: not expecting command", lineno);
 
232
                        synk(dbp);
 
233
                        state = COMMAND;
 
234
                        break;
 
235
                case 's':                       /* seq */
 
236
                        if (state != COMMAND)
 
237
                                err("line %lu: not expecting command", lineno);
 
238
                        if (flags == R_CURSOR) {
 
239
                                state = KEY;
 
240
                                command = SEQ;
 
241
                        } else
 
242
                                seq(dbp, &key);
 
243
                        break;
 
244
                case 'f':
 
245
                        flags = setflags(p + 1);
 
246
                        break;
 
247
                case 'D':                       /* data file */
 
248
                        if (state != DATA)
 
249
                                err("line %lu: not expecting data", lineno);
 
250
                        data.data = rfile(p + 1, &data.size);
 
251
                        goto ldata;
 
252
                case 'd':                       /* data */
 
253
                        if (state != DATA)
 
254
                                err("line %lu: not expecting data", lineno);
 
255
                        data.data = xmalloc(p + 1, len - 1);
 
256
                        data.size = len - 1;
 
257
ldata:                  switch (command) {
 
258
                        case COMPARE:
 
259
                                compare(&keydata, &data);
 
260
                                break;
 
261
                        case PUT:
 
262
                                put(dbp, &key, &data);
 
263
                                break;
 
264
                        default:
 
265
                                err("line %lu: command doesn't take data",
 
266
                                    lineno);
 
267
                        }
 
268
                        if (type != DB_RECNO)
 
269
                                free(key.data);
 
270
                        free(data.data);
 
271
                        state = COMMAND;
 
272
                        break;
 
273
                case 'K':                       /* key file */
 
274
                        if (state != KEY)
 
275
                                err("line %lu: not expecting a key", lineno);
 
276
                        if (type == DB_RECNO)
 
277
                                err("line %lu: 'K' not available for recno",
 
278
                                    lineno);
 
279
                        key.data = rfile(p + 1, &key.size);
 
280
                        goto lkey;
 
281
                case 'k':                       /* key */
 
282
                        if (state != KEY)
 
283
                                err("line %lu: not expecting a key", lineno);
 
284
                        if (type == DB_RECNO) {
 
285
                                static recno_t recno;
 
286
                                recno = atoi(p + 1);
 
287
                                key.data = &recno;
 
288
                                key.size = sizeof(recno);
 
289
                        } else {
 
290
                                key.data = xmalloc(p + 1, len - 1);
 
291
                                key.size = len - 1;
 
292
                        }
 
293
lkey:                   switch (command) {
 
294
                        case COMPARE:
 
295
                                getdata(dbp, &key, &keydata);
 
296
                                state = DATA;
 
297
                                break;
 
298
                        case GET:
 
299
                                get(dbp, &key);
 
300
                                if (type != DB_RECNO)
 
301
                                        free(key.data);
 
302
                                state = COMMAND;
 
303
                                break;
 
304
                        case PUT:
 
305
                                state = DATA;
 
306
                                break;
 
307
                        case REMOVE:
 
308
                                rem(dbp, &key);
 
309
                                if ((type != DB_RECNO) && (flags != R_CURSOR))
 
310
                                        free(key.data);
 
311
                                state = COMMAND;
 
312
                                break;
 
313
                        case SEQ:
 
314
                                seq(dbp, &key);
 
315
                                if ((type != DB_RECNO) && (flags != R_CURSOR))
 
316
                                        free(key.data);
 
317
                                state = COMMAND;
 
318
                                break;
 
319
                        default:
 
320
                                err("line %lu: command doesn't take a key",
 
321
                                    lineno);
 
322
                        }
 
323
                        break;
 
324
                case 'o':
 
325
                        dump(dbp, p[1] == 'r');
 
326
                        break;
 
327
                default:
 
328
                        err("line %lu: %s: unknown command character",
 
329
                            lineno, p);
 
330
                }
 
331
        }
 
332
#ifdef STATISTICS
 
333
        /*
 
334
         * -l must be used (DB_LOCK must be set) for this to be
 
335
         * used, otherwise a page will be locked and it will fail.
 
336
         */
 
337
        if (type == DB_BTREE && oflags & DB_LOCK)
 
338
                __bt_stat(dbp);
 
339
#endif
 
340
        if (dbp->close(dbp))
 
341
                err("db->close: %s", strerror(errno));
 
342
        (void)close(ofd);
 
343
        exit(0);
 
344
}
 
345
 
 
346
#define NOOVERWRITE     "put failed, would overwrite key\n"
 
347
 
 
348
void
 
349
compare(db1, db2)
 
350
        DBT *db1, *db2;
 
351
{
 
352
        register size_t len;
 
353
        register u_char *p1, *p2;
 
354
 
 
355
        if (db1->size != db2->size) {
 
356
                printf("compare failed: key->data len %lu != data len %lu\n",
 
357
                    (u_long) db1->size, (u_long) db2->size);
 
358
                exit (1);
 
359
        }
 
360
 
 
361
        len = MIN(db1->size, db2->size);
 
362
        for (p1 = db1->data, p2 = db2->data; len--;)
 
363
                if (*p1++ != *p2++) {
 
364
                        err("compare failed at offset %d\n",
 
365
                            p1 - (u_char *)db1->data);
 
366
                        break;
 
367
                }
 
368
}
 
369
 
 
370
void
 
371
get(dbp, kp)
 
372
        DB *dbp;
 
373
        DBT *kp;
 
374
{
 
375
        DBT data;
 
376
 
 
377
        switch (dbp->get(dbp, kp, &data, flags)) {
 
378
        case 0:
 
379
                (void)write(ofd, data.data, data.size);
 
380
                if (ofd == STDOUT_FILENO)
 
381
                        (void)write(ofd, "\n", 1);
 
382
                break;
 
383
        case -1:
 
384
                err("line %lu: get: %s", lineno, strerror(errno));
 
385
                /* NOTREACHED */
 
386
        case 1:
 
387
#define NOSUCHKEY       "get failed, no such key\n"
 
388
                if (ofd != STDOUT_FILENO) {
 
389
                        (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
 
390
                        exit(1);
 
391
                } else
 
392
                        (void)fprintf(stderr, "%lu: %.*s: %s",
 
393
                            lineno, (int) MIN(kp->size, 20), (char *) kp->data, 
 
394
                                      NOSUCHKEY);
 
395
#undef  NOSUCHKEY
 
396
                break;
 
397
        }
 
398
}
 
399
 
 
400
void
 
401
getdata(dbp, kp, dp)
 
402
        DB *dbp;
 
403
        DBT *kp, *dp;
 
404
{
 
405
        switch (dbp->get(dbp, kp, dp, flags)) {
 
406
        case 0:
 
407
                return;
 
408
        case -1:
 
409
                err("line %lu: getdata: %s", lineno, strerror(errno));
 
410
                /* NOTREACHED */
 
411
        case 1:
 
412
                err("line %lu: getdata failed, no such key", lineno);
 
413
                /* NOTREACHED */
 
414
        }
 
415
}
 
416
 
 
417
void
 
418
put(dbp, kp, dp)
 
419
        DB *dbp;
 
420
        DBT *kp, *dp;
 
421
{
 
422
        switch (dbp->put(dbp, kp, dp, flags)) {
 
423
        case 0:
 
424
                break;
 
425
        case -1:
 
426
                err("line %lu: put: %s", lineno, strerror(errno));
 
427
                /* NOTREACHED */
 
428
        case 1:
 
429
                (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
 
430
                break;
 
431
        }
 
432
}
 
433
 
 
434
void
 
435
rem(dbp, kp)
 
436
        DB *dbp;
 
437
        DBT *kp;
 
438
{
 
439
        switch (dbp->del(dbp, kp, flags)) {
 
440
        case 0:
 
441
                break;
 
442
        case -1:
 
443
                err("line %lu: rem: %s", lineno, strerror(errno));
 
444
                /* NOTREACHED */
 
445
        case 1:
 
446
#define NOSUCHKEY       "rem failed, no such key\n"
 
447
                if (ofd != STDOUT_FILENO)
 
448
                        (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
 
449
                else if (flags != R_CURSOR)
 
450
                        (void)fprintf(stderr, "%lu: %.*s: %s", 
 
451
                            lineno, (int) MIN(kp->size, 20), (char *) kp->data, 
 
452
                                      NOSUCHKEY);
 
453
                else
 
454
                        (void)fprintf(stderr,
 
455
                            "%lu: rem of cursor failed\n", lineno);
 
456
#undef  NOSUCHKEY
 
457
                break;
 
458
        }
 
459
}
 
460
 
 
461
void
 
462
synk(dbp)
 
463
        DB *dbp;
 
464
{
 
465
        switch (dbp->sync(dbp, flags)) {
 
466
        case 0:
 
467
                break;
 
468
        case -1:
 
469
                err("line %lu: synk: %s", lineno, strerror(errno));
 
470
                /* NOTREACHED */
 
471
        }
 
472
}
 
473
 
 
474
void
 
475
seq(dbp, kp)
 
476
        DB *dbp;
 
477
        DBT *kp;
 
478
{
 
479
        DBT data;
 
480
 
 
481
        switch (dbp->seq(dbp, kp, &data, flags)) {
 
482
        case 0:
 
483
                (void)write(ofd, data.data, data.size);
 
484
                if (ofd == STDOUT_FILENO)
 
485
                        (void)write(ofd, "\n", 1);
 
486
                break;
 
487
        case -1:
 
488
                err("line %lu: seq: %s", lineno, strerror(errno));
 
489
                /* NOTREACHED */
 
490
        case 1:
 
491
#define NOSUCHKEY       "seq failed, no such key\n"
 
492
                if (ofd != STDOUT_FILENO)
 
493
                        (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
 
494
                else if (flags == R_CURSOR)
 
495
                        (void)fprintf(stderr, "%lu: %.*s: %s", 
 
496
                            lineno, (int) MIN(kp->size, 20), (char *) kp->data, 
 
497
                                      NOSUCHKEY);
 
498
                else
 
499
                        (void)fprintf(stderr,
 
500
                            "%lu: seq (%s) failed\n", lineno, sflags(flags));
 
501
#undef  NOSUCHKEY
 
502
                break;
 
503
        }
 
504
}
 
505
 
 
506
void
 
507
dump(dbp, rev)
 
508
        DB *dbp;
 
509
        int rev;
 
510
{
 
511
        DBT key, data;
 
512
        int lflags, nflags;
 
513
 
 
514
        if (rev) {
 
515
                lflags = R_LAST;
 
516
                nflags = R_PREV;
 
517
        } else {
 
518
                lflags = R_FIRST;
 
519
                nflags = R_NEXT;
 
520
        }
 
521
        for (;; lflags = nflags)
 
522
                switch (dbp->seq(dbp, &key, &data, lflags)) {
 
523
                case 0:
 
524
                        (void)write(ofd, data.data, data.size);
 
525
                        if (ofd == STDOUT_FILENO)
 
526
                                (void)write(ofd, "\n", 1);
 
527
                        break;
 
528
                case 1:
 
529
                        goto done;
 
530
                case -1:
 
531
                        err("line %lu: (dump) seq: %s",
 
532
                            lineno, strerror(errno));
 
533
                        /* NOTREACHED */
 
534
                }
 
535
done:   return;
 
536
}
 
537
        
 
538
u_int
 
539
setflags(s)
 
540
        char *s;
 
541
{
 
542
        char *p;
 
543
 
 
544
        for (; isspace((int) *s); ++s);
 
545
        if (*s == '\n' || *s == '\0')
 
546
                return (0);
 
547
        if ((p = strchr(s, '\n')) != NULL)
 
548
                *p = '\0';
 
549
        if (!strcmp(s, "R_CURSOR"))             return (R_CURSOR);
 
550
        if (!strcmp(s, "R_FIRST"))              return (R_FIRST);
 
551
        if (!strcmp(s, "R_IAFTER"))             return (R_IAFTER);
 
552
        if (!strcmp(s, "R_IBEFORE"))            return (R_IBEFORE);
 
553
        if (!strcmp(s, "R_LAST"))               return (R_LAST);
 
554
        if (!strcmp(s, "R_NEXT"))               return (R_NEXT);
 
555
        if (!strcmp(s, "R_NOOVERWRITE"))        return (R_NOOVERWRITE);
 
556
        if (!strcmp(s, "R_PREV"))               return (R_PREV);
 
557
        if (!strcmp(s, "R_SETCURSOR"))          return (R_SETCURSOR);
 
558
 
 
559
        err("line %lu: %s: unknown flag", lineno, s);
 
560
        /* NOTREACHED */
 
561
}
 
562
 
 
563
char *
 
564
sflags(lflags)
 
565
        int lflags;
 
566
{
 
567
        switch (lflags) {
 
568
        case R_CURSOR:          return ("R_CURSOR");
 
569
        case R_FIRST:           return ("R_FIRST");
 
570
        case R_IAFTER:          return ("R_IAFTER");
 
571
        case R_IBEFORE:         return ("R_IBEFORE");
 
572
        case R_LAST:            return ("R_LAST");
 
573
        case R_NEXT:            return ("R_NEXT");
 
574
        case R_NOOVERWRITE:     return ("R_NOOVERWRITE");
 
575
        case R_PREV:            return ("R_PREV");
 
576
        case R_SETCURSOR:       return ("R_SETCURSOR");
 
577
        }
 
578
 
 
579
        return ("UNKNOWN!");
 
580
}
 
581
        
 
582
DBTYPE
 
583
dbtype(s)
 
584
        char *s;
 
585
{
 
586
        if (!strcmp(s, "btree"))
 
587
                return (DB_BTREE);
 
588
        if (!strcmp(s, "hash"))
 
589
                return (DB_HASH);
 
590
        if (!strcmp(s, "recno"))
 
591
                return (DB_RECNO);
 
592
        err("%s: unknown type (use btree, hash or recno)", s);
 
593
        /* NOTREACHED */
 
594
}
 
595
 
 
596
void *
 
597
setinfo(db_type, s)
 
598
        DBTYPE db_type;
 
599
        char *s;
 
600
{
 
601
        static BTREEINFO ib;
 
602
        static HASHINFO ih;
 
603
        static RECNOINFO rh;
 
604
        char *eq;
 
605
 
 
606
        if ((eq = strchr(s, '=')) == NULL)
 
607
                err("%s: illegal structure set statement", s);
 
608
        *eq++ = '\0';
 
609
        if (!isdigit((int) *eq))
 
610
                err("%s: structure set statement must be a number", s);
 
611
                
 
612
        switch (db_type) {
 
613
        case DB_BTREE:
 
614
                if (!strcmp("flags", s)) {
 
615
                        ib.flags = atoi(eq);
 
616
                        return (&ib);
 
617
                }
 
618
                if (!strcmp("cachesize", s)) {
 
619
                        ib.cachesize = atoi(eq);
 
620
                        return (&ib);
 
621
                }
 
622
                if (!strcmp("maxkeypage", s)) {
 
623
                        ib.maxkeypage = atoi(eq);
 
624
                        return (&ib);
 
625
                }
 
626
                if (!strcmp("minkeypage", s)) {
 
627
                        ib.minkeypage = atoi(eq);
 
628
                        return (&ib);
 
629
                }
 
630
                if (!strcmp("lorder", s)) {
 
631
                        ib.lorder = atoi(eq);
 
632
                        return (&ib);
 
633
                }
 
634
                if (!strcmp("psize", s)) {
 
635
                        ib.psize = atoi(eq);
 
636
                        return (&ib);
 
637
                }
 
638
                break;
 
639
        case DB_HASH:
 
640
                if (!strcmp("bsize", s)) {
 
641
                        ih.bsize = atoi(eq);
 
642
                        return (&ih);
 
643
                }
 
644
                if (!strcmp("ffactor", s)) {
 
645
                        ih.ffactor = atoi(eq);
 
646
                        return (&ih);
 
647
                }
 
648
                if (!strcmp("nelem", s)) {
 
649
                        ih.nelem = atoi(eq);
 
650
                        return (&ih);
 
651
                }
 
652
                if (!strcmp("cachesize", s)) {
 
653
                        ih.cachesize = atoi(eq);
 
654
                        return (&ih);
 
655
                }
 
656
                if (!strcmp("lorder", s)) {
 
657
                        ih.lorder = atoi(eq);
 
658
                        return (&ih);
 
659
                }
 
660
                break;
 
661
        case DB_RECNO:
 
662
                if (!strcmp("flags", s)) {
 
663
                        rh.flags = atoi(eq);
 
664
                        return (&rh);
 
665
                }
 
666
                if (!strcmp("cachesize", s)) {
 
667
                        rh.cachesize = atoi(eq);
 
668
                        return (&rh);
 
669
                }
 
670
                if (!strcmp("lorder", s)) {
 
671
                        rh.lorder = atoi(eq);
 
672
                        return (&rh);
 
673
                }
 
674
                if (!strcmp("reclen", s)) {
 
675
                        rh.reclen = atoi(eq);
 
676
                        return (&rh);
 
677
                }
 
678
                if (!strcmp("bval", s)) {
 
679
                        rh.bval = atoi(eq);
 
680
                        return (&rh);
 
681
                }
 
682
                if (!strcmp("psize", s)) {
 
683
                        rh.psize = atoi(eq);
 
684
                        return (&rh);
 
685
                }
 
686
                break;
 
687
        }
 
688
        err("%s: unknown structure value", s);
 
689
        /* NOTREACHED */
 
690
}
 
691
 
 
692
void *
 
693
rfile(name, lenp)
 
694
        char *name;
 
695
        size_t *lenp;
 
696
{
 
697
        struct stat sb;
 
698
        void *p;
 
699
        int fd;
 
700
        char *np;
 
701
 
 
702
        for (; isspace((int) *name); ++name);
 
703
        if ((np = strchr(name, '\n')) != NULL)
 
704
                *np = '\0';
 
705
        if ((fd = open(name, O_RDONLY, 0)) < 0 ||
 
706
            fstat(fd, &sb))
 
707
                err("%s: %s\n", name, strerror(errno));
 
708
#ifdef NOT_PORTABLE
 
709
        if (sb.st_size > (off_t)SIZE_T_MAX)
 
710
                err("%s: %s\n", name, strerror(E2BIG));
 
711
#endif
 
712
        if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
 
713
                err("%s", strerror(errno));
 
714
        (void)read(fd, p, (int)sb.st_size);
 
715
        *lenp = sb.st_size;
 
716
        (void)close(fd);
 
717
        return (p);
 
718
}
 
719
 
 
720
void *
 
721
xmalloc(text, len)
 
722
        char *text;
 
723
        size_t len;
 
724
{
 
725
        void *p;
 
726
 
 
727
        if ((p = (void *)malloc(len)) == NULL)
 
728
                err("%s", strerror(errno));
 
729
        memmove(p, text, len);
 
730
        return (p);
 
731
}
 
732
 
 
733
void
 
734
usage()
 
735
{
 
736
        (void)fprintf(stderr,
 
737
            "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
 
738
        exit(1);
 
739
}
 
740
 
 
741
#include <stdarg.h>
 
742
 
 
743
void
 
744
err(const char *fmt, ...)
 
745
{
 
746
        va_list ap;
 
747
        va_start(ap, fmt);
 
748
        (void)fprintf(stderr, "dbtest: ");
 
749
        (void)vfprintf(stderr, fmt, ap);
 
750
        va_end(ap);
 
751
        (void)fprintf(stderr, "\n");
 
752
        exit(1);
 
753
        /* NOTREACHED */
 
754
}