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