13 /* a tdb tool for manipulating a tdb database */
15 static TDB_CONTEXT
*tdb
;
18 static void print_asc(unsigned char *buf
,int len
)
22 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
25 static void print_data(unsigned char *buf
,int len
)
31 printf("%02X ",(int)buf
[i
]);
33 if (i
%8 == 0) printf(" ");
35 print_asc(&buf
[i
-16],8); printf(" ");
36 print_asc(&buf
[i
-8],8); printf("\n");
37 if (i
<len
) printf("[%03X] ",i
);
46 while (n
--) printf(" ");
50 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
52 if (n
>0) print_asc(&buf
[i
-n
],n
);
57 static void help(void)
61 create dbname : create a database
62 open dbname : open an existing database
63 erase : erase the database
64 dump : dump the database as strings
65 insert key data : insert a record
66 store key data : store a record (replace)
67 show key : show a record by key
68 delete key : delete a record by key
72 static void terror(char *why
)
77 static void create_tdb(void)
79 char *tok
= strtok(NULL
, " ");
84 if (tdb
) tdb_close(tdb
);
85 tdb
= tdb_open(tok
, 0, TDB_CLEAR_IF_FIRST
,
86 O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
89 static void open_tdb(void)
91 char *tok
= strtok(NULL
, " ");
96 if (tdb
) tdb_close(tdb
);
97 tdb
= tdb_open(tok
, 0, 0, O_RDWR
, 0600);
100 static void insert_tdb(void)
102 char *k
= strtok(NULL
, " ");
103 char *d
= strtok(NULL
, " ");
112 key
.dsize
= strlen(k
);
114 dbuf
.dsize
= strlen(d
);
116 if (tdb_store(tdb
, key
, dbuf
, TDB_INSERT
) == -1) {
117 terror("insert failed");
121 static void store_tdb(void)
123 char *k
= strtok(NULL
, " ");
124 char *d
= strtok(NULL
, " ");
133 key
.dsize
= strlen(k
);
135 dbuf
.dsize
= strlen(d
);
137 if (tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
) == -1) {
138 terror("store failed");
142 static void show_tdb(void)
144 char *k
= strtok(NULL
, " ");
153 key
.dsize
= strlen(k
);
155 dbuf
= tdb_fetch(tdb
, key
);
156 if (!dbuf
.dptr
) terror("fetch failed");
157 printf("%s : %*.*s\n", k
, (int)dbuf
.dsize
, (int)dbuf
.dsize
, dbuf
.dptr
);
160 static void delete_tdb(void)
162 char *k
= strtok(NULL
, " ");
171 key
.dsize
= strlen(k
);
173 if (tdb_delete(tdb
, key
) != 0) {
174 terror("delete failed");
178 static int print_rec(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
180 printf("\nkey %d bytes\n", key
.dsize
);
181 print_data(key
.dptr
, key
.dsize
);
182 printf("data %d bytes\n", dbuf
.dsize
);
183 print_data(dbuf
.dptr
, dbuf
.dsize
);
187 static int total_bytes
;
189 static int traverse_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
191 total_bytes
+= dbuf
.dsize
;
195 static void info_tdb(void)
199 count
= tdb_traverse(tdb
, traverse_fn
, NULL
);
200 printf("%d records totalling %d bytes\n", count
, total_bytes
);
203 static char *getline(char *prompt
)
205 static char line
[1024];
207 fputs(prompt
, stdout
);
209 p
= fgets(line
, sizeof(line
)-1, stdin
);
210 if (p
) p
= strchr(p
, '\n');
215 static int do_delete_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
218 return tdb_delete(tdb
, key
);
221 int main(int argc
, char *argv
[])
226 while ((line
= getline("tdb> "))) {
230 if (line
[0] == '!') {
235 tok
= strtok(line
," ");
236 if (strcmp(tok
,"create") == 0) {
239 } else if (strcmp(tok
,"open") == 0) {
244 /* all the rest require a open database */
246 terror("database not open");
251 if (strcmp(tok
,"insert") == 0) {
253 } else if (strcmp(tok
,"store") == 0) {
255 } else if (strcmp(tok
,"show") == 0) {
257 } else if (strcmp(tok
,"erase") == 0) {
258 tdb_traverse(tdb
, do_delete_fn
, NULL
);
259 } else if (strcmp(tok
,"delete") == 0) {
261 } else if (strcmp(tok
,"dump") == 0) {
262 tdb_traverse(tdb
, print_rec
, NULL
);
263 } else if (strcmp(tok
,"info") == 0) {
270 if (tdb
) tdb_close(tdb
);