summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/db')
-rw-r--r--src/regress/lib/libc/db/Makefile23
-rw-r--r--src/regress/lib/libc/db/README37
-rw-r--r--src/regress/lib/libc/db/dbtest.c367
-rw-r--r--src/regress/lib/libc/db/run.test36
4 files changed, 276 insertions, 187 deletions
diff --git a/src/regress/lib/libc/db/Makefile b/src/regress/lib/libc/db/Makefile
index 5bf343bf64..7d2882594d 100644
--- a/src/regress/lib/libc/db/Makefile
+++ b/src/regress/lib/libc/db/Makefile
@@ -1,17 +1,22 @@
1# $NetBSD: Makefile,v 1.10 1995/04/20 22:39:11 cgd Exp $ 1# $OpenBSD: Makefile,v 1.10 2002/09/02 20:01:43 avsm Exp $
2# @(#)Makefile 8.1 (Berkeley) 6/4/93 2# $NetBSD: Makefile,v 1.11 1995/12/12 01:54:15 cgd Exp $
3 3
4PROG= dbtest 4PROG= dbtest
5 5
6# add -DSTATISTICS to CFLAGS to get usage statistics. Note that 6# add -DSTATISTICS to CFLAGS to get usage statistics. Note that
7# for this to work, libc must be compiled with -DSTATISTICS as well 7# for this to work, libc must be compiled with -DSTATISTICS as well
8CFLAGS= -g -D__DBINTERFACE_PRIVATE -DDEBUG 8CFLAGS+= -g -D__DBINTERFACE_PRIVATE -DDEBUG
9NOMAN= noman 9CLEANFILES+= t1 t2 t3 log
10CLEANFILES+= t1 t2 t3
11 10
12install: 11DBTARGETS=1 2 3 4 5 6 7 8 9 10 11 12 13 20
13 12
14regress: 13.for DT in ${DBTARGETS}
15 sh ${.CURDIR}/run.test 14db-${DT}: ${PROG}
15 sh ${.CURDIR}/run.test ${DT}
16REGRESS_TARGETS+=db-${DT}
17.PHONY: db-${DT}
18.endfor
16 19
17.include <bsd.prog.mk> 20REGRESS_SLOW_TARGETS+=db-3 db-12 db-20
21
22.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/db/README b/src/regress/lib/libc/db/README
index 3b290b09d8..23a1b77221 100644
--- a/src/regress/lib/libc/db/README
+++ b/src/regress/lib/libc/db/README
@@ -1,5 +1,6 @@
1# $NetBSD: README,v 1.4 1995/04/20 22:39:18 cgd Exp $ 1# $OpenBSD: README,v 1.3 2001/01/29 02:05:40 niklas Exp $
2# @(#)README 8.4 (Berkeley) 6/20/94 2# $NetBSD: README,v 1.5 1996/05/03 21:54:19 cgd Exp $
3# @(#)README 8.8 (Berkeley) 7/31/94
3 4
4To run the tests, enter "make regress". 5To run the tests, enter "make regress".
5 6
@@ -9,8 +10,11 @@ the test runs, and even larger files (the database files) are created in
9variable TMPDIR to a directory where the files can be built. 10variable TMPDIR to a directory where the files can be built.
10 11
11=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 12=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
12The script file consists of lines with a initial character which is 13The script file consists of lines with an initial character which is
13the "command" for that line. Legal characters are as follows: 14the command for that line, or an initial character indicating a key
15or data entry for a previous command.
16
17Legal command characters are as follows:
14 18
15c: compare a record 19c: compare a record
16 + must be followed by [kK][dD]; the data value in the database 20 + must be followed by [kK][dD]; the data value in the database
@@ -19,17 +23,24 @@ c: compare a record
19e: echo a string 23e: echo a string
20 + writes out the rest of the line into the output file; if the 24 + writes out the rest of the line into the output file; if the
21 last character is not a carriage-return, a newline is appended. 25 last character is not a carriage-return, a newline is appended.
26f: set the flags for the next command
27 + no value zero's the flags
22g: do a get command 28g: do a get command
23 + must be followed by [kK] 29 + must be followed by [kK]
24 + writes out the retrieved data DBT. 30 + writes out the retrieved data DBT.
31o [r]: dump [reverse]
32 + dump the database out, if 'r' is set, in reverse order.
25p: do a put command 33p: do a put command
26 + must be followed by [kK][dD] 34 + must be followed by [kK][dD]
27r: do a del command 35r: do a del command
28 + must be followed by [kK] 36 + must be followed by [kK] unless R_CURSOR flag set.
37S: sync the database
29s: do a seq command 38s: do a seq command
39 + must be followed by [kK] if R_CURSOR flag set.
30 + writes out the retrieved data DBT. 40 + writes out the retrieved data DBT.
31f: set the flags for the next command 41
32 + no value zero's the flags 42Legal key/data characters are as follows:
43
33D [file]: data file 44D [file]: data file
34 + set the current data value to the contents of the file 45 + set the current data value to the contents of the file
35d [data]: 46d [data]:
@@ -38,17 +49,21 @@ K [file]: key file
38 + set the current key value to the contents of the file 49 + set the current key value to the contents of the file
39k [data]: 50k [data]:
40 + set the current key value to the contents of the line. 51 + set the current key value to the contents of the line.
41o [r]: dump [reverse] 52
42 + dump the database out, if 'r' is set, in reverse order. 53Blank lines, lines with leading white space, and lines with leading
54hash marks (#) are ignored.
43 55
44Options to dbtest are as follows: 56Options to dbtest are as follows:
45 57
58 -d: Set the DB_LOCK flag.
46 -f: Use the file argument as the database file. 59 -f: Use the file argument as the database file.
47 -i: Use the rest of the argument to set elements in the info 60 -i: Use the rest of the argument to set elements in the info
48 structure. If the type is btree, then "-i cachesize=10240" 61 structure. If the type is btree, then "-i cachesize=10240"
49 will set BTREEINFO.cachesize to 10240. 62 will set BTREEINFO.cachesize to 10240.
50 -o: The rest of the argument is the output file instead of 63 -o: The rest of the argument is the output file instead of
51 using stdout. 64 using stdout.
65 -s: Don't delete the database file before opening it, i.e.
66 use the database file from a previous run.
52 67
53Dbtest requires two arguments, the type of access "hash", "recno" or 68Dbtest requires two arguments, the type of access "hash", "recno"
54"btree", and the script name. 69or "btree", and the script name or "-" to indicate stdin.
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
37static 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
44static char sccsid[] = "@(#)dbtest.c 8.8 (Berkeley) 2/21/94";
45#else
46static 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
64enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA }; 48enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
65 49
66void compare __P((DBT *, DBT *)); 50void compare(DBT *, DBT *);
67DBTYPE dbtype __P((char *)); 51DBTYPE dbtype(char *);
68void dump __P((DB *, int)); 52void dump(DB *, int);
69void err __P((const char *, ...)); 53void dberr(const char *, ...);
70void get __P((DB *, DBT *)); 54void get(DB *, DBT *);
71void getdata __P((DB *, DBT *, DBT *)); 55void getdata(DB *, DBT *, DBT *);
72void put __P((DB *, DBT *, DBT *)); 56void put(DB *, DBT *, DBT *);
73void rem __P((DB *, DBT *)); 57void rem(DB *, DBT *);
74void *rfile __P((char *, size_t *)); 58char *sflags(int);
75void seq __P((DB *, DBT *)); 59void synk(DB *);
76u_int setflags __P((char *)); 60void *rfile(char *, size_t *);
77void *setinfo __P((DBTYPE, char *)); 61void seq(DB *, DBT *);
78void usage __P((void)); 62u_int setflags(char *);
79void *xmalloc __P((char *, size_t)); 63void *setinfo(DBTYPE, char *);
80 64void usage(void);
81DBTYPE type; 65void *xmalloc(char *, size_t);
82void *infop; 66
83u_long lineno; 67DBTYPE type; /* Database type. */
84u_int flags; 68void *infop; /* Iflags. */
85int ofd = STDOUT_FILENO; 69u_long lineno; /* Current line in test script. */
70u_int flags; /* Current DB flags. */
71int ofd = STDOUT_FILENO; /* Standard output fd. */
86 72
87DB *XXdbp; /* Global for gdb. */ 73DB *XXdbp; /* Global for gdb. */
74int XXlineno; /* Fast breakpoint for gdb. */
88 75
89int 76int
90main(argc, argv) 77main(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;
222ldata: switch(command) { 243ldata: 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 }
258lkey: switch(command) { 279lkey: 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
310void 334void
311compare(db1, db2) 335compare(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
442void
443synk(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
454setflags(s) 519setflags(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
543char *
544sflags(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
486DBTYPE 562DBTYPE
487dbtype(s) 563dbtype(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
651void 721void
652#if __STDC__ 722dberr(const char *fmt, ...)
653err(const char *fmt, ...)
654#else
655err(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);
diff --git a/src/regress/lib/libc/db/run.test b/src/regress/lib/libc/db/run.test
index 4073310a31..0d6837e360 100644
--- a/src/regress/lib/libc/db/run.test
+++ b/src/regress/lib/libc/db/run.test
@@ -1,19 +1,28 @@
1#!/bin/sh - 1#!/bin/sh -
2# $NetBSD: run.test,v 1.7 1995/04/20 22:39:27 cgd Exp $
3# 2#
4# @(#)run.test 8.8 (Berkeley) 6/16/94 3# $OpenBSD: run.test,v 1.3 2001/01/29 02:05:41 niklas Exp $
4# $NetBSD: run.test,v 1.8 1996/05/03 21:57:51 cgd Exp $
5# @(#)run.test 8.10 (Berkeley) 7/26/94
5# 6#
6 7
7# db regression tests 8# db regression tests
8main() 9main()
9{ 10{
10 11
11DICT=/usr/share/dict/web2 12 PROG=./dbtest
12PROG=./dbtest 13 TMP1=t1
13TMP1=t1 14 TMP2=t2
14TMP2=t2 15 TMP3=t3
15TMP3=t3
16 16
17 if [ -f /usr/share/dict/words ]; then
18 DICT=/usr/share/dict/words
19 elif [ -f /usr/dict/words ]; then
20 DICT=/usr/dict/words
21 else
22 echo 'run.test: no dictionary'
23 exit 1
24 fi
25
17 if [ $# -eq 0 ]; then 26 if [ $# -eq 0 ]; then
18 for t in 1 2 3 4 5 6 7 8 9 10 11 12 13 20; do 27 for t in 1 2 3 4 5 6 7 8 9 10 11 12 13 20; do
19 test$t 28 test$t
@@ -345,7 +354,7 @@ test7()
345 for (i = 1; i <= 120; ++i) 354 for (i = 1; i <= 120; ++i)
346 printf("%05d: input key %d: %s\n", i, i, $0); 355 printf("%05d: input key %d: %s\n", i, i, $0);
347 printf("%05d: input key %d: %s\n", 120, 120, $0); 356 printf("%05d: input key %d: %s\n", 120, 120, $0);
348 printf("get failed, no such key\n"); 357 printf("seq failed, no such key\n");
349 printf("%05d: input key %d: %s\n", 1, 1, $0); 358 printf("%05d: input key %d: %s\n", 1, 1, $0);
350 printf("%05d: input key %d: %s\n", 2, 2, $0); 359 printf("%05d: input key %d: %s\n", 2, 2, $0);
351 exit; 360 exit;
@@ -364,10 +373,10 @@ test7()
364 for (i = 1; i <= 120; ++i) 373 for (i = 1; i <= 120; ++i)
365 printf("s\n"); 374 printf("s\n");
366 printf("fR_CURSOR\ns\nk120\n"); 375 printf("fR_CURSOR\ns\nk120\n");
367 printf("r\nk120\n"); 376 printf("r\n");
368 printf("fR_NEXT\ns\n"); 377 printf("fR_NEXT\ns\n");
369 printf("fR_CURSOR\ns\nk1\n"); 378 printf("fR_CURSOR\ns\nk1\n");
370 printf("r\nk1\n"); 379 printf("r\n");
371 printf("fR_FIRST\ns\n"); 380 printf("fR_FIRST\ns\n");
372 }' > $TMP2 381 }' > $TMP2
373 $PROG -o $TMP3 recno $TMP2 382 $PROG -o $TMP3 recno $TMP2
@@ -397,8 +406,6 @@ test8()
397 printf("e\t%d of 10 \n", i); 406 printf("e\t%d of 10 \n", i);
398 printf("r\nkkey1\nr\nkkey2\n"); 407 printf("r\nkkey1\nr\nkkey2\n");
399 } 408 }
400 printf("e\n");
401 printf("eend of test8 run\n");
402 }' > $TMP1 409 }' > $TMP1
403 $PROG btree $TMP1 410 $PROG btree $TMP1
404# $PROG hash $TMP1 411# $PROG hash $TMP1
@@ -459,7 +466,7 @@ test10()
459 printf("p\nk%d\nd%s\n", ++i, $0); 466 printf("p\nk%d\nd%s\n", ++i, $0);
460 } 467 }
461 END { 468 END {
462 printf("fR_CURSOR\nr\nk1\n"); 469 printf("fR_CURSOR\nr\n");
463 printf("eR_CURSOR SHOULD HAVE FAILED\n"); 470 printf("eR_CURSOR SHOULD HAVE FAILED\n");
464 }' > $TMP2 471 }' > $TMP2
465 $PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1 472 $PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1
@@ -573,7 +580,8 @@ test13()
573 echo g 580 echo g
574 echo k$i 581 echo k$i
575 done > $TMP2 582 done > $TMP2
576 $PROG -ilorder=$order -f byte.file -o $TMP3 $type $TMP2 583 $PROG -s \
584 -ilorder=$order -f byte.file -o $TMP3 $type $TMP2
577 if (cmp -s $TMP1 $TMP3) ; then : 585 if (cmp -s $TMP1 $TMP3) ; then :
578 else 586 else
579 echo "test13: $type/$order get failed" 587 echo "test13: $type/$order get failed"