13 /* a tdb tool for manipulating a tdb database */
15 static TDB_CONTEXT
*tdb
;
17 static int print_rec(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
19 static void print_asc(unsigned char *buf
,int len
)
23 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
26 static void print_data(unsigned char *buf
,int len
)
32 printf("%02X ",(int)buf
[i
]);
34 if (i
%8 == 0) printf(" ");
36 print_asc(&buf
[i
-16],8); printf(" ");
37 print_asc(&buf
[i
-8],8); printf("\n");
38 if (i
<len
) printf("[%03X] ",i
);
47 while (n
--) printf(" ");
51 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
53 if (n
>0) print_asc(&buf
[i
-n
],n
);
58 static void help(void)
62 create dbname : create a database
63 open dbname : open an existing database
64 erase : erase the database
65 dump : dump the database as strings
66 insert key data : insert a record
67 store key data : store a record (replace)
68 show key : show a record by key
69 delete key : delete a record by key
73 static void terror(char *why
)
78 static void create_tdb(void)
80 char *tok
= strtok(NULL
, " ");
85 if (tdb
) tdb_close(tdb
);
86 tdb
= tdb_open(tok
, 0, TDB_CLEAR_IF_FIRST
,
87 O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
90 static void open_tdb(void)
92 char *tok
= strtok(NULL
, " ");
97 if (tdb
) tdb_close(tdb
);
98 tdb
= tdb_open(tok
, 0, 0, O_RDWR
, 0600);
101 static void insert_tdb(void)
103 char *k
= strtok(NULL
, " ");
104 char *d
= strtok(NULL
, " ");
113 key
.dsize
= strlen(k
);
115 dbuf
.dsize
= strlen(d
);
117 if (tdb_store(tdb
, key
, dbuf
, TDB_INSERT
) == -1) {
118 terror("insert failed");
122 static void store_tdb(void)
124 char *k
= strtok(NULL
, " ");
125 char *d
= strtok(NULL
, " ");
134 key
.dsize
= strlen(k
);
136 dbuf
.dsize
= strlen(d
);
138 if (tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
) == -1) {
139 terror("store failed");
143 static void show_tdb(void)
145 char *k
= strtok(NULL
, " ");
154 key
.dsize
= strlen(k
)+1;
156 dbuf
= tdb_fetch(tdb
, key
);
157 if (!dbuf
.dptr
) terror("fetch failed");
158 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
159 print_rec(tdb
, key
, dbuf
, NULL
);
162 static void delete_tdb(void)
164 char *k
= strtok(NULL
, " ");
173 key
.dsize
= strlen(k
);
175 if (tdb_delete(tdb
, key
) != 0) {
176 terror("delete failed");
180 static int print_rec(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
182 printf("\nkey %d bytes\n", key
.dsize
);
183 print_data(key
.dptr
, key
.dsize
);
184 printf("data %d bytes\n", dbuf
.dsize
);
185 print_data(dbuf
.dptr
, dbuf
.dsize
);
189 static int total_bytes
;
191 static int traverse_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
193 total_bytes
+= dbuf
.dsize
;
197 static void info_tdb(void)
201 count
= tdb_traverse(tdb
, traverse_fn
, NULL
);
202 printf("%d records totalling %d bytes\n", count
, total_bytes
);
205 static char *getline(char *prompt
)
207 static char line
[1024];
209 fputs(prompt
, stdout
);
211 p
= fgets(line
, sizeof(line
)-1, stdin
);
212 if (p
) p
= strchr(p
, '\n');
217 static int do_delete_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
220 return tdb_delete(tdb
, key
);
223 int main(int argc
, char *argv
[])
229 static char tmp
[1024];
230 sprintf(tmp
, "open %s", argv
[1]);
235 while ((line
= getline("tdb> "))) {
239 if (line
[0] == '!') {
244 if ((tok
= strtok(line
," "))==NULL
) {
247 if (strcmp(tok
,"create") == 0) {
250 } else if (strcmp(tok
,"open") == 0) {
255 /* all the rest require a open database */
257 terror("database not open");
262 if (strcmp(tok
,"insert") == 0) {
264 } else if (strcmp(tok
,"store") == 0) {
266 } else if (strcmp(tok
,"show") == 0) {
268 } else if (strcmp(tok
,"erase") == 0) {
269 tdb_traverse(tdb
, do_delete_fn
, NULL
);
270 } else if (strcmp(tok
,"delete") == 0) {
272 } else if (strcmp(tok
,"dump") == 0) {
273 tdb_traverse(tdb
, print_rec
, NULL
);
274 } else if (strcmp(tok
,"info") == 0) {
281 if (tdb
) tdb_close(tdb
);