| 1 | #include "gdbmobj.h" |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | /* either egcs or libc breaks fwrite on some platforms -- this is confirmed */ |
| 6 | |
| 7 | static void fw(char *p, size_t a, size_t b, FILE *f) |
| 8 | { |
| 9 | size_t i, j=a*b; |
| 10 | |
| 11 | for (i=0; i<j; i++) |
| 12 | putc(p[i], f); |
| 13 | } |
| 14 | |
| 15 | struct kd { |
| 16 | struct kd *next; |
| 17 | char *key, *data; |
| 18 | } ; |
| 19 | |
| 20 | static int kdcmp (const void *a, const void *b) |
| 21 | { |
| 22 | return (strcmp( (*(const struct kd **)a)->key, |
| 23 | (*(const struct kd **)b)->key)); |
| 24 | } |
| 25 | |
| 26 | int main(int argc, char **argv) |
| 27 | { |
| 28 | if (argc < 2) exit(1); |
| 29 | |
| 30 | if (argc < 3) |
| 31 | { |
| 32 | GdbmObj dbw; |
| 33 | char *key, *data; |
| 34 | size_t keylen, datalen; |
| 35 | struct kd *kdlist, **kdarray; |
| 36 | size_t kdcnt; |
| 37 | |
| 38 | if (dbw.Open(argv[1], "R")) |
| 39 | { |
| 40 | perror("open"); |
| 41 | exit(1); |
| 42 | } |
| 43 | |
| 44 | printf("Dumping %s:\n", argv[1]); |
| 45 | kdlist=0; |
| 46 | kdcnt=0; |
| 47 | for (key=dbw.FetchFirstKeyVal(keylen, data, datalen); |
| 48 | key; key=dbw.FetchNextKeyVal(keylen, data, datalen)) |
| 49 | { |
| 50 | struct kd *k=(struct kd *)malloc(sizeof(struct kd)); |
| 51 | |
| 52 | if (!k) |
| 53 | { |
| 54 | perror("malloc"); |
| 55 | exit(1); |
| 56 | } |
| 57 | if ((k->key=(char *)malloc(keylen+1)) == 0 || |
| 58 | (k->data=(char *)malloc(datalen+1)) == 0) |
| 59 | { |
| 60 | perror("malloc"); |
| 61 | exit(1); |
| 62 | } |
| 63 | memcpy(k->key, key, keylen); |
| 64 | k->key[keylen]=0; |
| 65 | memcpy(k->data, data, datalen); |
| 66 | k->data[datalen]=0; |
| 67 | free(data); |
| 68 | ++kdcnt; |
| 69 | k->next=kdlist; |
| 70 | kdlist=k; |
| 71 | } |
| 72 | |
| 73 | if ((kdarray=(struct kd **) |
| 74 | malloc( (kdcnt+1)*sizeof(struct kd *))) == 0) |
| 75 | { |
| 76 | perror("malloc"); |
| 77 | exit(1); |
| 78 | } |
| 79 | kdcnt=0; |
| 80 | while ( kdlist ) |
| 81 | { |
| 82 | kdarray[kdcnt++]=kdlist; |
| 83 | kdlist=kdlist->next; |
| 84 | } |
| 85 | kdarray[kdcnt]=0; |
| 86 | |
| 87 | qsort( kdarray, kdcnt, sizeof(kdarray[0]), &kdcmp); |
| 88 | |
| 89 | for (kdcnt=0; kdarray[kdcnt]; kdcnt++) |
| 90 | { |
| 91 | printf("Key: "); |
| 92 | fw(kdarray[kdcnt]->key, strlen(kdarray[kdcnt]->key), |
| 93 | 1, stdout); |
| 94 | printf(", Data: "); |
| 95 | fw(kdarray[kdcnt]->data, strlen(kdarray[kdcnt]->data), |
| 96 | 1, stdout); |
| 97 | printf("\n"); |
| 98 | free(kdarray[kdcnt]->key); |
| 99 | free(kdarray[kdcnt]->data); |
| 100 | free(kdarray[kdcnt]); |
| 101 | } |
| 102 | free(kdarray); |
| 103 | dbw.Close(); |
| 104 | } else if (argc < 4 && argv[2][0] == '-') |
| 105 | { |
| 106 | GdbmObj dbw; |
| 107 | |
| 108 | if (dbw.Open(argv[1], "W")) |
| 109 | { |
| 110 | perror("open"); |
| 111 | exit(1); |
| 112 | } |
| 113 | |
| 114 | printf("Deleting %s from %s:\n", argv[2], argv[1]); |
| 115 | if (dbw.Delete(argv[2]+1, strlen(argv[2]+1))) |
| 116 | fprintf(stderr, "Not found.\n"); |
| 117 | |
| 118 | dbw.Close(); |
| 119 | } else if (argc < 4) |
| 120 | { |
| 121 | GdbmObj dbw; |
| 122 | |
| 123 | if (dbw.Open(argv[1], "R")) |
| 124 | { |
| 125 | perror("open"); |
| 126 | exit(1); |
| 127 | } |
| 128 | |
| 129 | size_t len; |
| 130 | char *val=dbw.Fetch(argv[2], strlen(argv[2]), len, 0); |
| 131 | |
| 132 | if (!val) |
| 133 | { |
| 134 | fprintf(stderr, "%s: not found.\n", argv[2]); |
| 135 | exit(1); |
| 136 | } |
| 137 | printf("Fetching %s from %s: ", argv[2], argv[1]); |
| 138 | fw(val, len, 1, stdout); |
| 139 | printf("\n"); |
| 140 | free(val); |
| 141 | dbw.Close(); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | GdbmObj dbw; |
| 146 | |
| 147 | if (dbw.Open(argv[1], "C")) |
| 148 | { |
| 149 | perror("open"); |
| 150 | exit(1); |
| 151 | } |
| 152 | |
| 153 | printf("Storing %s/%s into %s:\n", argv[2], argv[3], argv[1]); |
| 154 | if (dbw.Store(argv[2], strlen(argv[2]), |
| 155 | argv[3], strlen(argv[3]), "R")) |
| 156 | { |
| 157 | perror("write"); |
| 158 | exit(1); |
| 159 | } |
| 160 | |
| 161 | dbw.Close(); |
| 162 | } |
| 163 | exit(0); |
| 164 | return (0); |
| 165 | } |