| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: db.c,v 1.20 2008/09/05 07:55:33 lukem Exp $ */ | | 1 | /* $NetBSD: db.c,v 1.21 2008/10/07 01:32:51 lukem Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2002-2008 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2002-2008 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Luke Mewburn of Wasabi Systems. | | 8 | * by Luke Mewburn of Wasabi Systems. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -22,32 +22,32 @@ | | | @@ -22,32 +22,32 @@ |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | #ifndef lint | | 33 | #ifndef lint |
34 | #ifdef __RCSID | | 34 | #ifdef __RCSID |
35 | __RCSID("$NetBSD: db.c,v 1.20 2008/09/05 07:55:33 lukem Exp $"); | | 35 | __RCSID("$NetBSD: db.c,v 1.21 2008/10/07 01:32:51 lukem Exp $"); |
36 | #endif /* __RCSID */ | | 36 | #endif /* __RCSID */ |
37 | #endif /* not lint */ | | 37 | #endif /* not lint */ |
38 | | | 38 | |
39 | #include <db.h> | | | |
40 | #include <ctype.h> | | 39 | #include <ctype.h> |
| | | 40 | #include <db.h> |
41 | #include <err.h> | | 41 | #include <err.h> |
42 | #include <fcntl.h> | | 42 | #include <fcntl.h> |
43 | #include <limits.h> | | 43 | #include <limits.h> |
44 | #include <stdio.h> | | 44 | #include <stdio.h> |
45 | #include <stdlib.h> | | 45 | #include <stdlib.h> |
46 | #include <string.h> | | 46 | #include <string.h> |
47 | #include <unistd.h> | | 47 | #include <unistd.h> |
48 | #include <vis.h> | | 48 | #include <vis.h> |
49 | | | 49 | |
50 | | | 50 | |
51 | typedef enum { | | 51 | typedef enum { |
52 | F_WRITE = 1<<0, | | 52 | F_WRITE = 1<<0, |
53 | F_DELETE = 1<<1, | | 53 | F_DELETE = 1<<1, |
| @@ -431,41 +431,46 @@ db_makekey(DBT *key, char *keystr, int d | | | @@ -431,41 +431,46 @@ db_makekey(DBT *key, char *keystr, int d |
431 | key->data = ks; | | 431 | key->data = ks; |
432 | key->size = klen + (flags & F_NO_NUL ? 0 : 1); | | 432 | key->size = klen + (flags & F_NO_NUL ? 0 : 1); |
433 | if (downcase && (flags & F_IGNORECASE)) { | | 433 | if (downcase && (flags & F_IGNORECASE)) { |
434 | for (p = ks; *p; p++) | | 434 | for (p = ks; *p; p++) |
435 | if (isupper((int)*p)) | | 435 | if (isupper((int)*p)) |
436 | *p = tolower((int)*p); | | 436 | *p = tolower((int)*p); |
437 | } | | 437 | } |
438 | } | | 438 | } |
439 | | | 439 | |
440 | int | | 440 | int |
441 | db_del(char *keystr) | | 441 | db_del(char *keystr) |
442 | { | | 442 | { |
443 | DBT key; | | 443 | DBT key; |
444 | int r = 0; | | 444 | int r; |
445 | | | 445 | |
446 | db_makekey(&key, keystr, 1, (flags & F_DECODE_KEY ? 1 : 0)); | | 446 | db_makekey(&key, keystr, 1, (flags & F_DECODE_KEY ? 1 : 0)); |
447 | switch (db->del(db, &key, 0)) { | | 447 | r = db->del(db, &key, 0); |
| | | 448 | switch (r) { |
448 | case -1: | | 449 | case -1: |
449 | warn("Error deleting key `%s'", keystr); | | 450 | if (! (flags & F_QUIET)) |
| | | 451 | warn("Error deleting key `%s'", keystr); |
450 | r = 1; | | 452 | r = 1; |
451 | break; | | 453 | break; |
452 | case 0: | | 454 | case 0: |
453 | if (! (flags & F_QUIET)) | | 455 | if (! (flags & F_QUIET)) |
454 | printf("Deleted key `%s'\n", keystr); | | 456 | printf("Deleted key `%s'\n", keystr); |
455 | break; | | 457 | break; |
456 | case 1: | | 458 | case 1: |
457 | warnx("Unknown key `%s'", keystr); | | 459 | if (! (flags & F_QUIET)) |
| | | 460 | warnx("Unknown key `%s'", keystr); |
458 | break; | | 461 | break; |
| | | 462 | default: |
| | | 463 | abort(); |
459 | } | | 464 | } |
460 | if (flags & F_DECODE_KEY) | | 465 | if (flags & F_DECODE_KEY) |
461 | free(key.data); | | 466 | free(key.data); |
462 | return (r); | | 467 | return (r); |
463 | } | | 468 | } |
464 | | | 469 | |
465 | int | | 470 | int |
466 | db_get(char *keystr) | | 471 | db_get(char *keystr) |
467 | { | | 472 | { |
468 | DBT key, val; | | 473 | DBT key, val; |
469 | char *wantkey; | | 474 | char *wantkey; |
470 | int r, found; | | 475 | int r, found; |
471 | u_int seqflags; | | 476 | u_int seqflags; |
| @@ -495,55 +500,59 @@ db_get(char *keystr) | | | @@ -495,55 +500,59 @@ db_get(char *keystr) |
495 | r = 1; | | 500 | r = 1; |
496 | break; | | 501 | break; |
497 | case 0: | | 502 | case 0: |
498 | break; | | 503 | break; |
499 | case 1: | | 504 | case 1: |
500 | if (found) { | | 505 | if (found) { |
501 | r = 0; | | 506 | r = 0; |
502 | break; | | 507 | break; |
503 | } | | 508 | } |
504 | if (! (flags & F_QUIET)) { | | 509 | if (! (flags & F_QUIET)) { |
505 | warnx("Unknown key `%s'", keystr); | | 510 | warnx("Unknown key `%s'", keystr); |
506 | } | | 511 | } |
507 | break; | | 512 | break; |
| | | 513 | default: |
| | | 514 | abort(); |
508 | } | | 515 | } |
509 | if (flags & F_DECODE_KEY) | | 516 | if (flags & F_DECODE_KEY) |
510 | free(key.data); | | 517 | free(key.data); |
511 | free(wantkey); | | 518 | free(wantkey); |
512 | return (r); | | 519 | return (r); |
513 | } | | 520 | } |
514 | | | 521 | |
515 | int | | 522 | int |
516 | db_put(char *keystr, char *valstr) | | 523 | db_put(char *keystr, char *valstr) |
517 | { | | 524 | { |
518 | DBT key, val; | | 525 | DBT key, val; |
519 | int r = 0; | | 526 | int r = 0; |
520 | | | 527 | |
521 | db_makekey(&key, keystr, 1, (flags & F_DECODE_KEY ? 1 : 0)); | | 528 | db_makekey(&key, keystr, 1, (flags & F_DECODE_KEY ? 1 : 0)); |
522 | db_makekey(&val, valstr, 0, (flags & F_DECODE_VAL ? 1 : 0)); | | 529 | db_makekey(&val, valstr, 0, (flags & F_DECODE_VAL ? 1 : 0)); |
523 | switch (db->put(db, &key, &val, | | 530 | r = db->put(db, &key, &val, (flags & F_REPLACE) ? 0 : R_NOOVERWRITE); |
524 | (flags & F_REPLACE) ? 0 : R_NOOVERWRITE)) { | | 531 | switch (r) { |
525 | case -1: | | 532 | case -1: |
526 | warn("Error writing key `%s'", keystr); | | 533 | warn("Error writing key `%s'", keystr); |
527 | r = 1; | | 534 | r = 1; |
528 | break; | | 535 | break; |
529 | case 0: | | 536 | case 0: |
530 | if (! (flags & F_QUIET)) | | 537 | if (! (flags & F_QUIET)) |
531 | printf("Added key `%s'\n", keystr); | | 538 | printf("Added key `%s'\n", keystr); |
532 | break; | | 539 | break; |
533 | case 1: | | 540 | case 1: |
534 | if (! (flags & F_QUIET)) | | 541 | if (! (flags & F_QUIET)) |
535 | warnx("Key `%s' already exists", keystr); | | 542 | warnx("Key `%s' already exists", keystr); |
536 | break; | | 543 | break; |
| | | 544 | default: |
| | | 545 | abort(); |
537 | } | | 546 | } |
538 | if (flags & F_DECODE_KEY) | | 547 | if (flags & F_DECODE_KEY) |
539 | free(key.data); | | 548 | free(key.data); |
540 | if (flags & F_DECODE_VAL) | | 549 | if (flags & F_DECODE_VAL) |
541 | free(val.data); | | 550 | free(val.data); |
542 | return (r); | | 551 | return (r); |
543 | } | | 552 | } |
544 | | | 553 | |
545 | int | | 554 | int |
546 | parseline(FILE *fp, const char *sep, char **kp, char **vp) | | 555 | parseline(FILE *fp, const char *sep, char **kp, char **vp) |
547 | { | | 556 | { |
548 | size_t len; | | 557 | size_t len; |
549 | char *key, *val; | | 558 | char *key, *val; |