1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
|
/*
* dbz - use and test dbz in various ways
*
* -Log-
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <dbz.h>
#ifdef FUNNYSEEKS
#include <unistd.h>
#else
#define SEEK_SET 0
#endif
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
#ifndef lint
static char RCSid[] = "$Header$";
#endif
char *progname;
char *inname = "(no file)"; /* filename for messages etc. */
long lineno; /* line number for messages etc. */
char *base_name;
char *pagname;
char *dir_name;
char *str2dup();
FILE *base;
int op = 'b'; /* what to do, default build a new table */
char *badop = "only one of -a -x -c -m -v can be given";
int baseinput = 1; /* is the base file also the input? */
char *from = NULL; /* old table to use for dbzagain() */
int omitzero = 0; /* omit lines tagged with 0 */
long every = 0; /* report every n lines */
int syncs = 0; /* dbzsync() on each report */
int quick = 0; /* quick checking, not too thorough */
int sweep = 0; /* sweep file checking all offsets */
int useincore = 1; /* should we use incore facility? */
long xxx = 0; /* debugging variable */
int printx = 0; /* print xxx after all is done */
int unique = 1; /* before store(), check with fetch() */
int usefresh = 0; /* use dbzfresh? */
long siz = 0; /* -p size */
char map = 'C'; /* -p map */
long tag = 0; /* -p tag mask */
int exact = 0; /* do not run dbzsize(siz) */
int dbzint = 1; /* use new interface? */
char fs = '\t'; /* field separator, default tab */
int unopen = 0; /* make base unopenable during dbminit? */
char *change = NULL; /* chdir here before dbmclose */
long tagsize = 0; /* if non-zero, file size for tag sizing */
int dowt = 0; /* do writethrough? */
#define DEFBUF 1024 /* default line-buffer size */
int buflen = DEFBUF; /* line length limit */
char lbuf[DEFBUF];
char *line = lbuf;
char cbuf[DEFBUF];
char *cmp = cbuf;
void fail();
void dofile();
void runs();
void dosweep();
void verify();
void mkfiles();
void crfile();
void doline();
void process();
datum dofetch();
int dostore();
#ifdef HAVERFCIZE
extern char *rfc822ize();
#else
#define rfc822ize(n) (n)
#endif
/*
- main - parse arguments and handle options
*/
main(argc, argv)
int argc;
char *argv[];
{
int c;
int errflg = 0;
extern int optind;
extern char *optarg;
int doruns = 0;
progname = argv[0];
while ((c = getopt(argc, argv, "axcmvt:l:R0E:SqOiX:Yuf:p:eMUC:T:wd")) != EOF)
switch (c) {
case 'a': /* append to existing table */
if (op != 'b')
fail(badop, "");
op = 'a';
baseinput = 0;
break;
case 'x': /* extract from existing table */
if (op != 'b')
fail(badop, "");
op = 'x';
baseinput = 0;
break;
case 'c': /* check existing table */
if (op != 'b')
fail(badop, "");
op = 'c';
break;
case 'm': /* extract missing (complement of -x) */
if (op != 'b')
fail(badop, "");
op = 'm';
baseinput = 0;
break;
case 'v': /* verify that this is a dbz file */
if (op != 'b')
fail(badop, "");
op = 'v';
break;
case 't': /* set field separator */
if (strlen(optarg) > (size_t)1)
fail("only one field separator allowed", "");
fs = *optarg;
break;
case 'l': /* override line-length limit */
buflen = atoi(optarg) + 1;
if (buflen <= 2)
fail("bad -l value `%s'", optarg);
line = malloc(buflen);
cmp = malloc(buflen);
if (line == NULL || cmp == NULL)
fail("cannot allocate %s-byte buffers", optarg);
break;
case 'R': /* print run statistics */
doruns = 1;
break;
case '0': /* omit lines tagged (by fake -t) with 0 */
omitzero = 1;
break;
case 'E': /* report every n items */
every = atol(optarg);
break;
case 'S': /* dbzsync() on each -E report */
syncs = 1;
break;
case 'q': /* quick check or extract */
quick = 1;
break;
case 'O': /* sweep file checking all offsets */
sweep = 1;
break;
case 'i': /* don't use incore */
useincore = 0;
break;
case 'X': /* set xxx */
xxx = atoi(optarg);
break;
case 'Y': /* print xxx afterward */
printx = 1;
break;
case 'u': /* don't check uniqueness */
unique = 0;
break;
case 'f': /* init from existing table's parameters */
from = optarg;
break;
case 'p': /* parameters for dbzfresh */
if (sscanf(optarg, "%ld %1s %lx", &siz, &map, &tag) != 3) {
map = '?';
tag = 0;
if (sscanf(optarg, "%ld", &siz) != 1)
fail("bad -n value `%s'", optarg);
}
usefresh = 1;
break;
case 'e': /* -p size is exact, don't dbzsize() it */
exact = 1;
break;
case 'M': /* use old dbm interface + rfc822ize */
dbzint = 0;
break;
case 'U': /* make base unopenable during init */
unopen = 1;
break;
case 'C': /* change directories before dbmclose */
change = optarg;
break;
case 'd': /* Debugging. */
if (dbzdebug(1) < 0)
fail("dbz debugging not available", "");
break;
case 'T': /* file size for tag sizing */
tagsize = atol(optarg);
break;
case 'w': /* do writethrough */
dowt = 1;
break;
case '?':
default:
errflg++;
break;
}
if (errflg || optind >= argc || (optind+1 < argc && baseinput)) {
fprintf(stderr, "usage: %s ", progname);
fprintf(stderr, "[-{axcmv}] database [file] ...\n");
exit(2);
}
(void) dbzincore(useincore);
(void) dbzwritethrough(dowt);
base_name = argv[optind];
pagname = str2dup(base_name, ".pag");
dir_name = str2dup(base_name, ".dir");
if (op == 'v') {
verify(dir_name);
/* NOTREACHED */
}
mkfiles();
optind++;
if (baseinput) /* implies no further arguments */
process(base, base_name);
else if (optind >= argc)
process(stdin, "stdin");
else
for (; optind < argc; optind++)
dofile(argv[optind]);
if (change != NULL)
(void) chdir(change);
if (dbmclose() < 0)
fail("dbmclose failed", "");
if (doruns)
runs(pagname);
if (sweep)
dosweep(base_name, pagname);
if (printx)
printf("%ld\n", xxx);
exit(0);
}
/*
- verify - just check whether the .dir file looks right or not
*/
void /* does not return */
verify(dir)
char *dir;
{
FILE *f;
char buf[4];
size_t n;
f = fopen(dir, "r");
if (f == NULL)
exit(1);
n = fread(buf, sizeof(buf), 1, f);
(void) fclose(f);
if (n != 1 || memcmp(buf, "dbz ", (size_t)4) != 0)
exit(1);
exit(0);
/* NOTREACHED */
}
/*
- dofile - open a file and invoke process()
*/
void
dofile(name)
char *name;
{
register FILE *in;
if (STREQ(name, "-"))
process(stdin, "-");
else {
in = fopen(name, "r");
if (in == NULL)
fail("cannot open `%s'", name);
process(in, name);
(void) fclose(in);
}
}
/*
- mkfiles - create empty files and open them up
*/
void
mkfiles()
{
if (op == 'b' && !dbzint) {
crfile(dir_name);
crfile(pagname);
}
base = fopen(base_name, (op == 'a') ? "a" : "r");
if (base == NULL)
fail("cannot open `%s'", base_name);
if (unopen)
(void) chmod(base_name, 0);
if (from != NULL) {
if (dbzagain(base_name, from) < 0)
fail("dbzagain(`%s'...) failed", base_name);
} else if (op == 'b' && dbzint) {
if (!exact)
siz = dbzsize(siz);
if (tagsize != 0)
tag = dbztagmask(tagsize);
if (dbzfresh(base_name, siz, (int)fs, map, tag) < 0)
fail("dbzfresh(`%s'...) failed", base_name);
} else if (dbminit(base_name) < 0)
fail("dbminit(`%s') failed", base_name);
if (unopen)
(void) chmod(base_name, 0600); /* hard to restore original */
}
/*
- crfile - create a file
*/
void
crfile(name)
char *name;
{
register int f;
f = creat(name, 0666);
if (f < 0)
fail("cannot create `%s'", name);
(void) close(f);
}
/*
- process - process input file
*/
void
process(in, name)
FILE *in;
char *name;
{
register long place;
inname = name;
lineno = 0;
for (;;) {
place = ftell(in);
if (fgets(line, buflen, in) == NULL)
return;
lineno++;
if (every > 0 && lineno%every == 0) {
fprintf(stderr, "%ld\n", lineno);
if (dbzsync() < 0)
fail("dbzsync failed", "");
}
doline(line, place);
}
/* NOTREACHED */
}
/*
- doline - process input line
*/
void
doline(lp, inoffset)
char *lp;
long inoffset;
{
register char *p;
register char pc;
datum key, value;
long place = inoffset;
register int shouldfind;
register int llen;
char keytext[DBZMAXKEY+1];
p = NULL;
if (fs != '\0')
p = strchr(lp, fs);
if (p == NULL)
p = lp + strlen(lp);
if (p > lp && *(p-1) == '\n')
p--;
if (p - lp > DBZMAXKEY)
fail("key of `%s' too long", lp);
pc = *p;
*p = '\0';
(void) strcpy(keytext, lp);
*p = pc;
key.dptr = (dbzint) ? keytext : rfc822ize(keytext);
key.dsize = strlen(keytext)+1;
switch (op) {
case 'a':
place = ftell(base);
llen = strlen(lp);
if (fwrite(lp, 1, llen, base) != llen)
fail("write error in `%s'", base_name);
/* FALLTHROUGH */
case 'b':
if (omitzero && p != NULL && *(p+1) == '0')
return;
if (unique) {
value = dofetch(key);
if (value.dptr != NULL)
fail("`%s' already present", lp);
}
value.dptr = (char *)&place;
value.dsize = (int)sizeof(place);
if (dostore(key, value) < 0)
fail("store failed on `%s'", lp);
break;
case 'c':
value = dofetch(key);
shouldfind = (omitzero && p != NULL && *(p+1) == '0') ? 0 : 1;
if (!shouldfind && (value.dptr != NULL || value.dsize != 0))
fail("`%s' found, shouldn't be", lp);
if (shouldfind && (value.dptr == NULL ||
value.dsize != sizeof(place)))
fail("can't find `%s'", lp);
if (shouldfind && !quick) {
(void) memcpy((char *)&place, value.dptr, sizeof(place));
if (place != inoffset)
fail("offset mismatch on `%s'", lp);
if (fseek(base, place, SEEK_SET) != 0)
fail("fseek failed on `%s'", lp);
if (fgets(cmp, buflen, base) == NULL)
fail("can't read line for `%s'", lp);
if (!STREQ(lp, cmp))
fail("compare failed on `%s'", lp);
}
break;
case 'x':
value = dofetch(key);
if (value.dptr != NULL && !quick) {
(void) memcpy((char *)&place, value.dptr, sizeof(place));
if (fseek(base, place, SEEK_SET) != 0)
fail("fseek failed on `%s'", lp);
if (fgets(cmp, buflen, base) == NULL)
fail("can't read line for `%s'", lp);
fputs(cmp, stdout);
} else if (value.dptr != NULL)
fputs(lp, stdout);
break;
case 'm':
value = dofetch(key);
if (value.dptr == NULL) {
fputs(keytext, stdout);
putchar('\n');
}
break;
default:
fail("unknown operator -- can't happen", "");
break;
}
}
/*
- runs - print run statistics
*/
void
runs(file)
char *file;
{
register FILE *fd;
long it;
register long run;
fd = fopen(file, "r");
if (fd == NULL)
fail("cannot reopen `%s'", file);
run = 0;
while (fread((char *)&it, sizeof(it), 1, fd) == 1) {
if (it != 0)
run++;
else if (run > 0) {
printf("%ld\n", run);
run = 0;
}
}
(void) fclose(fd);
}
/*
- dosweep - sweep pag file checking for valid offsets
*/
void
dosweep(fn, pn)
char *fn;
char *pn;
{
register FILE *pf;
long it;
char nl;
register FILE *hf;
hf = fopen(fn, "r");
if (hf == NULL)
fail("cannot reopen `%s'", fn);
pf = fopen(pn, "r");
if (pf == NULL)
fail("cannot reopen `%s'", pn);
while (fread((char *)&it, sizeof(it), 1, pf) == 1) {
it = (it & 0x80000000) ? (it&~0xf8000000) : it;
if (it != 0 && it != 1) { /* 0 empty, 1 known okay */
it--; /* get rid of bias */
(void) fseek(hf, it-1, SEEK_SET);
nl = getc(hf);
if (nl != '\n')
fprintf(stderr, "offset 0%lo does not point to line\n",
(long)it);
}
}
(void) fclose(hf);
(void) fclose(pf);
}
/*
- fail - complain and die
*/
void
fail(s1, s2)
char *s1;
char *s2;
{
# define MAXS2 50
char s2buf[MAXS2+10];
fprintf(stderr, "%s: (file `%s', line %ld) ", progname, inname, lineno);
if (strlen(s2) <= (size_t)MAXS2)
fprintf(stderr, s1, s2);
else {
sprintf(s2buf, "%.*s...", MAXS2, s2);
fprintf(stderr, s1, s2buf);
}
fprintf(stderr, "\n");
exit(1);
}
/*
- str2dup - concatenate strings and malloc result
*/
char *
str2dup(s1, s2)
char *s1;
char *s2;
{
register char *p;
p = malloc((size_t)strlen(s1) + strlen(s2) + 1);
if (p == NULL)
fail("can't allocate space for strings", "");
(void) strcpy(p, s1);
(void) strcat(p, s2);
return(p);
}
/*
- dofetch - do a fetch or dbzfetch
*/
datum
dofetch(key)
datum key;
{
if (dbzint)
return(dbzfetch(key));
else
return(fetch(key));
}
/*
- dostore - do a store or dbzstore
*/
int
dostore(key, value)
datum key;
datum value;
{
if (dbzint)
return(dbzstore(key, value));
else
return(store(key, value));
}
|