summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/db/dbtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/db/dbtest.c')
-rw-r--r--src/regress/lib/libc/db/dbtest.c359
1 files changed, 217 insertions, 142 deletions
diff --git a/src/regress/lib/libc/db/dbtest.c b/src/regress/lib/libc/db/dbtest.c
index 1fcf09af97..8eda40d6b3 100644
--- a/src/regress/lib/libc/db/dbtest.c
+++ b/src/regress/lib/libc/db/dbtest.c
@@ -1,7 +1,8 @@
1/* $NetBSD: dbtest.c,v 1.7 1995/04/20 22:39:22 cgd Exp $ */ 1/* $OpenBSD: dbtest.c,v 1.11 2003/07/31 21:48:02 deraadt Exp $ */
2/* $NetBSD: dbtest.c,v 1.8 1996/05/03 21:57:48 cgd Exp $ */
2 3
3/*- 4/*-
4 * Copyright (c) 1992, 1993 5 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved. 6 * The Regents of the University of California. All rights reserved.
6 * 7 *
7 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
@@ -12,11 +13,7 @@
12 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 16 * 3. Neither the name of the University nor the names of its contributors
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 17 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 18 * without specific prior written permission.
22 * 19 *
@@ -35,15 +32,15 @@
35 32
36#ifndef lint 33#ifndef lint
37static char copyright[] = 34static char copyright[] =
38"@(#) Copyright (c) 1992, 1993\n\ 35"@(#) Copyright (c) 1992, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n"; 36 The Regents of the University of California. All rights reserved.\n";
40#endif /* not lint */ 37#endif /* not lint */
41 38
42#ifndef lint 39#ifndef lint
43#if 0 40#if 0
44static char sccsid[] = "@(#)dbtest.c 8.8 (Berkeley) 2/21/94"; 41static char sccsid[] = "@(#)dbtest.c 8.17 (Berkeley) 9/1/94";
45#else 42#else
46static char rcsid[] = "$NetBSD: dbtest.c,v 1.7 1995/04/20 22:39:22 cgd Exp $"; 43static char rcsid[] = "$OpenBSD: dbtest.c,v 1.11 2003/07/31 21:48:02 deraadt Exp $";
47#endif 44#endif
48#endif /* not lint */ 45#endif /* not lint */
49 46
@@ -57,39 +54,41 @@ static char rcsid[] = "$NetBSD: dbtest.c,v 1.7 1995/04/20 22:39:22 cgd Exp $";
57#include <stdio.h> 54#include <stdio.h>
58#include <stdlib.h> 55#include <stdlib.h>
59#include <string.h> 56#include <string.h>
57#include <stdarg.h>
60#include <unistd.h> 58#include <unistd.h>
61 59
62#include <db.h> 60#include <db.h>
63 61
64enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA }; 62enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
65 63
66void compare __P((DBT *, DBT *)); 64void compare(DBT *, DBT *);
67DBTYPE dbtype __P((char *)); 65DBTYPE dbtype(char *);
68void dump __P((DB *, int)); 66void dump(DB *, int);
69void err __P((const char *, ...)); 67void dberr(const char *, ...);
70void get __P((DB *, DBT *)); 68void get(DB *, DBT *);
71void getdata __P((DB *, DBT *, DBT *)); 69void getdata(DB *, DBT *, DBT *);
72void put __P((DB *, DBT *, DBT *)); 70void put(DB *, DBT *, DBT *);
73void rem __P((DB *, DBT *)); 71void rem(DB *, DBT *);
74void *rfile __P((char *, size_t *)); 72char *sflags(int);
75void seq __P((DB *, DBT *)); 73void synk(DB *);
76u_int setflags __P((char *)); 74void *rfile(char *, size_t *);
77void *setinfo __P((DBTYPE, char *)); 75void seq(DB *, DBT *);
78void usage __P((void)); 76u_int setflags(char *);
79void *xmalloc __P((char *, size_t)); 77void *setinfo(DBTYPE, char *);
80 78void usage(void);
81DBTYPE type; 79void *xmalloc(char *, size_t);
82void *infop; 80
83u_long lineno; 81DBTYPE type; /* Database type. */
84u_int flags; 82void *infop; /* Iflags. */
85int ofd = STDOUT_FILENO; 83u_long lineno; /* Current line in test script. */
84u_int flags; /* Current DB flags. */
85int ofd = STDOUT_FILENO; /* Standard output fd. */
86 86
87DB *XXdbp; /* Global for gdb. */ 87DB *XXdbp; /* Global for gdb. */
88int XXlineno; /* Fast breakpoint for gdb. */
88 89
89int 90int
90main(argc, argv) 91main(int argc, char *argv[])
91 int argc;
92 char *argv[];
93{ 92{
94 extern int optind; 93 extern int optind;
95 extern char *optarg; 94 extern char *optarg;
@@ -97,14 +96,15 @@ main(argc, argv)
97 DB *dbp; 96 DB *dbp;
98 DBT data, key, keydata; 97 DBT data, key, keydata;
99 size_t len; 98 size_t len;
100 int ch, oflags; 99 int ch, oflags, sflag;
101 char *fname, *infoarg, *p, buf[8 * 1024]; 100 char *fname, *infoarg, *p, *t, buf[8 * 1024];
102 101
103 infoarg = NULL; 102 infoarg = NULL;
104 fname = NULL; 103 fname = NULL;
105 oflags = O_CREAT | O_RDWR; 104 oflags = O_CREAT | O_RDWR;
106 while ((ch = getopt(argc, argv, "f:i:lo:")) != EOF) 105 sflag = 0;
107 switch(ch) { 106 while ((ch = getopt(argc, argv, "f:i:lo:s")) != -1)
107 switch (ch) {
108 case 'f': 108 case 'f':
109 fname = optarg; 109 fname = optarg;
110 break; 110 break;
@@ -117,7 +117,10 @@ main(argc, argv)
117 case 'o': 117 case 'o':
118 if ((ofd = open(optarg, 118 if ((ofd = open(optarg,
119 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 119 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
120 err("%s: %s", optarg, strerror(errno)); 120 dberr("%s: %s", optarg, strerror(errno));
121 break;
122 case 's':
123 sflag = 1;
121 break; 124 break;
122 case '?': 125 case '?':
123 default: 126 default:
@@ -133,8 +136,8 @@ main(argc, argv)
133 type = dbtype(*argv++); 136 type = dbtype(*argv++);
134 137
135 /* Open the descriptor file. */ 138 /* Open the descriptor file. */
136 if (freopen(*argv, "r", stdin) == NULL) 139 if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
137 err("%s: %s", *argv, strerror(errno)); 140 dberr("%s: %s", *argv, strerror(errno));
138 141
139 /* Set up the db structure as necessary. */ 142 /* Set up the db structure as necessary. */
140 if (infoarg == NULL) 143 if (infoarg == NULL)
@@ -145,61 +148,93 @@ main(argc, argv)
145 if (*p != '\0') 148 if (*p != '\0')
146 infop = setinfo(type, p); 149 infop = setinfo(type, p);
147 150
148 /* Open the DB. */ 151 /*
152 * Open the DB. Delete any preexisting copy, you almost never
153 * want it around, and it often screws up tests.
154 */
149 if (fname == NULL) { 155 if (fname == NULL) {
150 p = getenv("TMPDIR"); 156 p = getenv("TMPDIR");
151 if (p == NULL) 157 if (p == NULL)
152 p = "/var/tmp"; 158 p = "/var/tmp";
153 (void)sprintf(buf, "%s/__dbtest", p); 159 (void)snprintf(buf, sizeof buf, "%s/__dbtest", p);
154 fname = buf; 160 fname = buf;
155 (void)unlink(buf); 161 (void)unlink(buf);
156 } 162 } else if (!sflag)
163 (void)unlink(fname);
164
157 if ((dbp = dbopen(fname, 165 if ((dbp = dbopen(fname,
158 oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL) 166 oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
159 err("dbopen: %s", strerror(errno)); 167 dberr("dbopen: %s", strerror(errno));
160 XXdbp = dbp; 168 XXdbp = dbp;
161 169
162 state = COMMAND; 170 state = COMMAND;
163 for (lineno = 1; 171 for (lineno = 1;
164 (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) { 172 (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
165 len = strlen(buf); 173 /* Delete the newline, displaying the key/data is easier. */
166 switch(*p) { 174 if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
175 *t = '\0';
176 if ((len = strlen(buf)) == 0 || isspace(*p) || *p == '#')
177 continue;
178
179 /* Convenient gdb break point. */
180 if (XXlineno == lineno)
181 XXlineno = 1;
182 switch (*p) {
167 case 'c': /* compare */ 183 case 'c': /* compare */
168 if (state != COMMAND) 184 if (state != COMMAND)
169 err("line %lu: not expecting command", lineno); 185 dberr("line %lu: not expecting command",
186 lineno);
170 state = KEY; 187 state = KEY;
171 command = COMPARE; 188 command = COMPARE;
172 break; 189 break;
173 case 'e': /* echo */ 190 case 'e': /* echo */
174 if (state != COMMAND) 191 if (state != COMMAND)
175 err("line %lu: not expecting command", lineno); 192 dberr("line %lu: not expecting command",
193 lineno);
176 /* Don't display the newline, if CR at EOL. */ 194 /* Don't display the newline, if CR at EOL. */
177 if (p[len - 2] == '\r') 195 if (p[len - 2] == '\r')
178 --len; 196 --len;
179 if (write(ofd, p + 1, len - 1) != len - 1) 197 if (write(ofd, p + 1, len - 1) != len - 1 ||
180 err("write: %s", strerror(errno)); 198 write(ofd, "\n", 1) != 1)
199 dberr("write: %s", strerror(errno));
181 break; 200 break;
182 case 'g': /* get */ 201 case 'g': /* get */
183 if (state != COMMAND) 202 if (state != COMMAND)
184 err("line %lu: not expecting command", lineno); 203 dberr("line %lu: not expecting command",
204 lineno);
185 state = KEY; 205 state = KEY;
186 command = GET; 206 command = GET;
187 break; 207 break;
188 case 'p': /* put */ 208 case 'p': /* put */
189 if (state != COMMAND) 209 if (state != COMMAND)
190 err("line %lu: not expecting command", lineno); 210 dberr("line %lu: not expecting command",
211 lineno);
191 state = KEY; 212 state = KEY;
192 command = PUT; 213 command = PUT;
193 break; 214 break;
194 case 'r': /* remove */ 215 case 'r': /* remove */
195 if (state != COMMAND) 216 if (state != COMMAND)
196 err("line %lu: not expecting command", lineno); 217 dberr("line %lu: not expecting command",
197 state = KEY; 218 lineno);
198 command = REMOVE; 219 if (flags == R_CURSOR) {
220 rem(dbp, &key);
221 state = COMMAND;
222 } else {
223 state = KEY;
224 command = REMOVE;
225 }
226 break;
227 case 'S': /* sync */
228 if (state != COMMAND)
229 dberr("line %lu: not expecting command",
230 lineno);
231 synk(dbp);
232 state = COMMAND;
199 break; 233 break;
200 case 's': /* seq */ 234 case 's': /* seq */
201 if (state != COMMAND) 235 if (state != COMMAND)
202 err("line %lu: not expecting command", lineno); 236 dberr("line %lu: not expecting command",
237 lineno);
203 if (flags == R_CURSOR) { 238 if (flags == R_CURSOR) {
204 state = KEY; 239 state = KEY;
205 command = SEQ; 240 command = SEQ;
@@ -211,15 +246,15 @@ main(argc, argv)
211 break; 246 break;
212 case 'D': /* data file */ 247 case 'D': /* data file */
213 if (state != DATA) 248 if (state != DATA)
214 err("line %lu: not expecting data", lineno); 249 dberr("line %lu: not expecting data", lineno);
215 data.data = rfile(p + 1, &data.size); 250 data.data = rfile(p + 1, &data.size);
216 goto ldata; 251 goto ldata;
217 case 'd': /* data */ 252 case 'd': /* data */
218 if (state != DATA) 253 if (state != DATA)
219 err("line %lu: not expecting data", lineno); 254 dberr("line %lu: not expecting data", lineno);
220 data.data = xmalloc(p + 1, len - 1); 255 data.data = xmalloc(p + 1, len - 1);
221 data.size = len - 1; 256 data.size = len - 1;
222ldata: switch(command) { 257ldata: switch (command) {
223 case COMPARE: 258 case COMPARE:
224 compare(&keydata, &data); 259 compare(&keydata, &data);
225 break; 260 break;
@@ -227,7 +262,7 @@ ldata: switch(command) {
227 put(dbp, &key, &data); 262 put(dbp, &key, &data);
228 break; 263 break;
229 default: 264 default:
230 err("line %lu: command doesn't take data", 265 dberr("line %lu: command doesn't take data",
231 lineno); 266 lineno);
232 } 267 }
233 if (type != DB_RECNO) 268 if (type != DB_RECNO)
@@ -237,15 +272,15 @@ ldata: switch(command) {
237 break; 272 break;
238 case 'K': /* key file */ 273 case 'K': /* key file */
239 if (state != KEY) 274 if (state != KEY)
240 err("line %lu: not expecting a key", lineno); 275 dberr("line %lu: not expecting a key", lineno);
241 if (type == DB_RECNO) 276 if (type == DB_RECNO)
242 err("line %lu: 'K' not available for recno", 277 dberr("line %lu: 'K' not available for recno",
243 lineno); 278 lineno);
244 key.data = rfile(p + 1, &key.size); 279 key.data = rfile(p + 1, &key.size);
245 goto lkey; 280 goto lkey;
246 case 'k': /* key */ 281 case 'k': /* key */
247 if (state != KEY) 282 if (state != KEY)
248 err("line %lu: not expecting a key", lineno); 283 dberr("line %lu: not expecting a key", lineno);
249 if (type == DB_RECNO) { 284 if (type == DB_RECNO) {
250 static recno_t recno; 285 static recno_t recno;
251 recno = atoi(p + 1); 286 recno = atoi(p + 1);
@@ -255,7 +290,7 @@ ldata: switch(command) {
255 key.data = xmalloc(p + 1, len - 1); 290 key.data = xmalloc(p + 1, len - 1);
256 key.size = len - 1; 291 key.size = len - 1;
257 } 292 }
258lkey: switch(command) { 293lkey: switch (command) {
259 case COMPARE: 294 case COMPARE:
260 getdata(dbp, &key, &keydata); 295 getdata(dbp, &key, &keydata);
261 state = DATA; 296 state = DATA;
@@ -271,18 +306,18 @@ lkey: switch(command) {
271 break; 306 break;
272 case REMOVE: 307 case REMOVE:
273 rem(dbp, &key); 308 rem(dbp, &key);
274 if (type != DB_RECNO) 309 if ((type != DB_RECNO) && (flags != R_CURSOR))
275 free(key.data); 310 free(key.data);
276 state = COMMAND; 311 state = COMMAND;
277 break; 312 break;
278 case SEQ: 313 case SEQ:
279 seq(dbp, &key); 314 seq(dbp, &key);
280 if (type != DB_RECNO) 315 if ((type != DB_RECNO) && (flags != R_CURSOR))
281 free(key.data); 316 free(key.data);
282 state = COMMAND; 317 state = COMMAND;
283 break; 318 break;
284 default: 319 default:
285 err("line %lu: command doesn't take a key", 320 dberr("line %lu: command doesn't take a key",
286 lineno); 321 lineno);
287 } 322 }
288 break; 323 break;
@@ -290,22 +325,25 @@ lkey: switch(command) {
290 dump(dbp, p[1] == 'r'); 325 dump(dbp, p[1] == 'r');
291 break; 326 break;
292 default: 327 default:
293 err("line %lu: %s: unknown command character", 328 dberr("line %lu: %s: unknown command character",
294 p, lineno); 329 lineno, p);
295 } 330 }
296 } 331 }
297#ifdef STATISTICS 332#ifdef STATISTICS
298 if (type == DB_BTREE) 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)
299 __bt_stat(dbp); 338 __bt_stat(dbp);
300#endif 339#endif
301 if (dbp->close(dbp)) 340 if (dbp->close(dbp))
302 err("db->close: %s", strerror(errno)); 341 dberr("db->close: %s", strerror(errno));
303 (void)close(ofd); 342 (void)close(ofd);
304 exit(0); 343 exit(0);
305} 344}
306 345
307#define NOOVERWRITE "put failed, would overwrite key\n" 346#define NOOVERWRITE "put failed, would overwrite key\n"
308#define NOSUCHKEY "get failed, no such key\n"
309 347
310void 348void
311compare(db1, db2) 349compare(db1, db2)
@@ -334,17 +372,23 @@ get(dbp, kp)
334{ 372{
335 DBT data; 373 DBT data;
336 374
337 switch(dbp->get(dbp, kp, &data, flags)) { 375 switch (dbp->get(dbp, kp, &data, flags)) {
338 case 0: 376 case 0:
339 (void)write(ofd, data.data, data.size); 377 (void)write(ofd, data.data, data.size);
378 if (ofd == STDOUT_FILENO)
379 (void)write(ofd, "\n", 1);
340 break; 380 break;
341 case -1: 381 case -1:
342 err("line %lu: get: %s", lineno, strerror(errno)); 382 dberr("line %lu: get: %s", lineno, strerror(errno));
343 /* NOTREACHED */ 383 /* NOTREACHED */
344 case 1: 384 case 1:
345 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 385#define NOSUCHKEY "get failed, no such key\n"
346 (void)fprintf(stderr, "%d: %.*s: %s\n", 386 if (ofd != STDOUT_FILENO)
347 lineno, kp->size, kp->data, NOSUCHKEY); 387 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
388 else
389 (void)fprintf(stderr, "%d: %.*s: %s",
390 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
391#undef NOSUCHKEY
348 break; 392 break;
349 } 393 }
350} 394}
@@ -354,14 +398,14 @@ getdata(dbp, kp, dp)
354 DB *dbp; 398 DB *dbp;
355 DBT *kp, *dp; 399 DBT *kp, *dp;
356{ 400{
357 switch(dbp->get(dbp, kp, dp, flags)) { 401 switch (dbp->get(dbp, kp, dp, flags)) {
358 case 0: 402 case 0:
359 return; 403 return;
360 case -1: 404 case -1:
361 err("line %lu: getdata: %s", lineno, strerror(errno)); 405 dberr("line %lu: getdata: %s", lineno, strerror(errno));
362 /* NOTREACHED */ 406 /* NOTREACHED */
363 case 1: 407 case 1:
364 err("line %lu: get failed, no such key", lineno); 408 dberr("line %lu: getdata failed, no such key", lineno);
365 /* NOTREACHED */ 409 /* NOTREACHED */
366 } 410 }
367} 411}
@@ -371,11 +415,11 @@ put(dbp, kp, dp)
371 DB *dbp; 415 DB *dbp;
372 DBT *kp, *dp; 416 DBT *kp, *dp;
373{ 417{
374 switch(dbp->put(dbp, kp, dp, flags)) { 418 switch (dbp->put(dbp, kp, dp, flags)) {
375 case 0: 419 case 0:
376 break; 420 break;
377 case -1: 421 case -1:
378 err("line %lu: put: %s", lineno, strerror(errno)); 422 dberr("line %lu: put: %s", lineno, strerror(errno));
379 /* NOTREACHED */ 423 /* NOTREACHED */
380 case 1: 424 case 1:
381 (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1); 425 (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
@@ -388,34 +432,67 @@ rem(dbp, kp)
388 DB *dbp; 432 DB *dbp;
389 DBT *kp; 433 DBT *kp;
390{ 434{
391 switch(dbp->del(dbp, kp, flags)) { 435 switch (dbp->del(dbp, kp, flags)) {
392 case 0: 436 case 0:
393 break; 437 break;
394 case -1: 438 case -1:
395 err("line %lu: get: %s", lineno, strerror(errno)); 439 dberr("line %lu: rem: %s", lineno, strerror(errno));
396 /* NOTREACHED */ 440 /* NOTREACHED */
397 case 1: 441 case 1:
398 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 442#define NOSUCHKEY "rem failed, no such key\n"
443 if (ofd != STDOUT_FILENO)
444 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
445 else if (flags != R_CURSOR)
446 (void)fprintf(stderr, "%d: %.*s: %s",
447 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
448 else
449 (void)fprintf(stderr,
450 "%d: rem of cursor failed\n", lineno);
451#undef NOSUCHKEY
399 break; 452 break;
400 } 453 }
401} 454}
402 455
403void 456void
457synk(dbp)
458 DB *dbp;
459{
460 switch (dbp->sync(dbp, flags)) {
461 case 0:
462 break;
463 case -1:
464 dberr("line %lu: synk: %s", lineno, strerror(errno));
465 /* NOTREACHED */
466 }
467}
468
469void
404seq(dbp, kp) 470seq(dbp, kp)
405 DB *dbp; 471 DB *dbp;
406 DBT *kp; 472 DBT *kp;
407{ 473{
408 DBT data; 474 DBT data;
409 475
410 switch(dbp->seq(dbp, kp, &data, flags)) { 476 switch (dbp->seq(dbp, kp, &data, flags)) {
411 case 0: 477 case 0:
412 (void)write(ofd, data.data, data.size); 478 (void)write(ofd, data.data, data.size);
479 if (ofd == STDOUT_FILENO)
480 (void)write(ofd, "\n", 1);
413 break; 481 break;
414 case -1: 482 case -1:
415 err("line %lu: seq: %s", lineno, strerror(errno)); 483 dberr("line %lu: seq: %s", lineno, strerror(errno));
416 /* NOTREACHED */ 484 /* NOTREACHED */
417 case 1: 485 case 1:
418 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 486#define NOSUCHKEY "seq failed, no such key\n"
487 if (ofd != STDOUT_FILENO)
488 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
489 else if (flags == R_CURSOR)
490 (void)fprintf(stderr, "%d: %.*s: %s",
491 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
492 else
493 (void)fprintf(stderr,
494 "%d: seq (%s) failed\n", lineno, sflags(flags));
495#undef NOSUCHKEY
419 break; 496 break;
420 } 497 }
421} 498}
@@ -436,14 +513,16 @@ dump(dbp, rev)
436 nflags = R_NEXT; 513 nflags = R_NEXT;
437 } 514 }
438 for (;; flags = nflags) 515 for (;; flags = nflags)
439 switch(dbp->seq(dbp, &key, &data, flags)) { 516 switch (dbp->seq(dbp, &key, &data, flags)) {
440 case 0: 517 case 0:
441 (void)write(ofd, data.data, data.size); 518 (void)write(ofd, data.data, data.size);
519 if (ofd == STDOUT_FILENO)
520 (void)write(ofd, "\n", 1);
442 break; 521 break;
443 case 1: 522 case 1:
444 goto done; 523 goto done;
445 case -1: 524 case -1:
446 err("line %lu: (dump) seq: %s", 525 dberr("line %lu: (dump) seq: %s",
447 lineno, strerror(errno)); 526 lineno, strerror(errno));
448 /* NOTREACHED */ 527 /* NOTREACHED */
449 } 528 }
@@ -454,34 +533,45 @@ u_int
454setflags(s) 533setflags(s)
455 char *s; 534 char *s;
456{ 535{
457 char *p, *index(); 536 char *p;
458 537
459 for (; isspace(*s); ++s); 538 for (; isspace(*s); ++s);
460 if (*s == '\n') 539 if (*s == '\n' || *s == '\0')
461 return (0); 540 return (0);
462 if ((p = index(s, '\n')) != NULL) 541 if ((p = strchr(s, '\n')) != NULL)
463 *p = '\0'; 542 *p = '\0';
464 if (!strcmp(s, "R_CURSOR")) 543 if (!strcmp(s, "R_CURSOR")) return (R_CURSOR);
465 return (R_CURSOR); 544 if (!strcmp(s, "R_FIRST")) return (R_FIRST);
466 if (!strcmp(s, "R_FIRST")) 545 if (!strcmp(s, "R_IAFTER")) return (R_IAFTER);
467 return (R_FIRST); 546 if (!strcmp(s, "R_IBEFORE")) return (R_IBEFORE);
468 if (!strcmp(s, "R_IAFTER")) 547 if (!strcmp(s, "R_LAST")) return (R_LAST);
469 return (R_IAFTER); 548 if (!strcmp(s, "R_NEXT")) return (R_NEXT);
470 if (!strcmp(s, "R_IBEFORE")) 549 if (!strcmp(s, "R_NOOVERWRITE")) return (R_NOOVERWRITE);
471 return (R_IBEFORE); 550 if (!strcmp(s, "R_PREV")) return (R_PREV);
472 if (!strcmp(s, "R_LAST")) 551 if (!strcmp(s, "R_SETCURSOR")) return (R_SETCURSOR);
473 return (R_LAST); 552
474 if (!strcmp(s, "R_NEXT")) 553 dberr("line %lu: %s: unknown flag", lineno, s);
475 return (R_NEXT);
476 if (!strcmp(s, "R_NOOVERWRITE"))
477 return (R_NOOVERWRITE);
478 if (!strcmp(s, "R_PREV"))
479 return (R_PREV);
480 if (!strcmp(s, "R_SETCURSOR"))
481 return (R_SETCURSOR);
482 err("line %lu: %s: unknown flag", lineno, s);
483 /* NOTREACHED */ 554 /* NOTREACHED */
484} 555}
556
557char *
558sflags(flags)
559 int flags;
560{
561 switch (flags) {
562 case R_CURSOR: return ("R_CURSOR");
563 case R_FIRST: return ("R_FIRST");
564 case R_IAFTER: return ("R_IAFTER");
565 case R_IBEFORE: return ("R_IBEFORE");
566 case R_LAST: return ("R_LAST");
567 case R_NEXT: return ("R_NEXT");
568 case R_NOOVERWRITE: return ("R_NOOVERWRITE");
569 case R_PREV: return ("R_PREV");
570 case R_SETCURSOR: return ("R_SETCURSOR");
571 }
572
573 return ("UNKNOWN!");
574}
485 575
486DBTYPE 576DBTYPE
487dbtype(s) 577dbtype(s)
@@ -493,7 +583,7 @@ dbtype(s)
493 return (DB_HASH); 583 return (DB_HASH);
494 if (!strcmp(s, "recno")) 584 if (!strcmp(s, "recno"))
495 return (DB_RECNO); 585 return (DB_RECNO);
496 err("%s: unknown type (use btree, hash or recno)", s); 586 dberr("%s: unknown type (use btree, hash or recno)", s);
497 /* NOTREACHED */ 587 /* NOTREACHED */
498} 588}
499 589
@@ -505,15 +595,15 @@ setinfo(type, s)
505 static BTREEINFO ib; 595 static BTREEINFO ib;
506 static HASHINFO ih; 596 static HASHINFO ih;
507 static RECNOINFO rh; 597 static RECNOINFO rh;
508 char *eq, *index(); 598 char *eq;
509 599
510 if ((eq = index(s, '=')) == NULL) 600 if ((eq = strchr(s, '=')) == NULL)
511 err("%s: illegal structure set statement", s); 601 dberr("%s: illegal structure set statement", s);
512 *eq++ = '\0'; 602 *eq++ = '\0';
513 if (!isdigit(*eq)) 603 if (!isdigit(*eq))
514 err("%s: structure set statement must be a number", s); 604 dberr("%s: structure set statement must be a number", s);
515 605
516 switch(type) { 606 switch (type) {
517 case DB_BTREE: 607 case DB_BTREE:
518 if (!strcmp("flags", s)) { 608 if (!strcmp("flags", s)) {
519 ib.flags = atoi(eq); 609 ib.flags = atoi(eq);
@@ -589,7 +679,7 @@ setinfo(type, s)
589 } 679 }
590 break; 680 break;
591 } 681 }
592 err("%s: unknown structure value", s); 682 dberr("%s: unknown structure value", s);
593 /* NOTREACHED */ 683 /* NOTREACHED */
594} 684}
595 685
@@ -601,20 +691,20 @@ rfile(name, lenp)
601 struct stat sb; 691 struct stat sb;
602 void *p; 692 void *p;
603 int fd; 693 int fd;
604 char *np, *index(); 694 char *np;
605 695
606 for (; isspace(*name); ++name); 696 for (; isspace(*name); ++name);
607 if ((np = index(name, '\n')) != NULL) 697 if ((np = strchr(name, '\n')) != NULL)
608 *np = '\0'; 698 *np = '\0';
609 if ((fd = open(name, O_RDONLY, 0)) < 0 || 699 if ((fd = open(name, O_RDONLY, 0)) < 0 ||
610 fstat(fd, &sb)) 700 fstat(fd, &sb))
611 err("%s: %s\n", name, strerror(errno)); 701 dberr("%s: %s\n", name, strerror(errno));
612#ifdef NOT_PORTABLE 702#ifdef NOT_PORTABLE
613 if (sb.st_size > (off_t)SIZE_T_MAX) 703 if (sb.st_size > (off_t)SIZE_T_MAX)
614 err("%s: %s\n", name, strerror(E2BIG)); 704 dberr("%s: %s\n", name, strerror(E2BIG));
615#endif 705#endif
616 if ((p = (void *)malloc((u_int)sb.st_size)) == NULL) 706 if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
617 err("%s", strerror(errno)); 707 dberr("%s", strerror(errno));
618 (void)read(fd, p, (int)sb.st_size); 708 (void)read(fd, p, (int)sb.st_size);
619 *lenp = sb.st_size; 709 *lenp = sb.st_size;
620 (void)close(fd); 710 (void)close(fd);
@@ -629,7 +719,7 @@ xmalloc(text, len)
629 void *p; 719 void *p;
630 720
631 if ((p = (void *)malloc(len)) == NULL) 721 if ((p = (void *)malloc(len)) == NULL)
632 err("%s", strerror(errno)); 722 dberr("%s", strerror(errno));
633 memmove(p, text, len); 723 memmove(p, text, len);
634 return (p); 724 return (p);
635} 725}
@@ -642,27 +732,12 @@ usage()
642 exit(1); 732 exit(1);
643} 733}
644 734
645#if __STDC__
646#include <stdarg.h>
647#else
648#include <varargs.h>
649#endif
650
651void 735void
652#if __STDC__ 736dberr(const char *fmt, ...)
653err(const char *fmt, ...)
654#else
655err(fmt, va_alist)
656 char *fmt;
657 va_dcl
658#endif
659{ 737{
660 va_list ap; 738 va_list ap;
661#if __STDC__ 739
662 va_start(ap, fmt); 740 va_start(ap, fmt);
663#else
664 va_start(ap);
665#endif
666 (void)fprintf(stderr, "dbtest: "); 741 (void)fprintf(stderr, "dbtest: ");
667 (void)vfprintf(stderr, fmt, ap); 742 (void)vfprintf(stderr, fmt, ap);
668 va_end(ap); 743 va_end(ap);