This commit was manufactured by cvs2svn to create branch 'SAMBA_2_2'.
[Samba.git] / source / tdb / tdbtool.c
blob4400f5d8e0339f9515e109d5980f9ae5b230318e
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <fcntl.h>
7 #include <sys/mman.h>
8 #include <sys/stat.h>
9 #include <sys/time.h>
10 #include <ctype.h>
11 #include "tdb.h"
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)
21 int i;
22 for (i=0;i<len;i++)
23 printf("%c",isprint(buf[i])?buf[i]:'.');
26 static void print_data(unsigned char *buf,int len)
28 int i=0;
29 if (len<=0) return;
30 printf("[%03X] ",i);
31 for (i=0;i<len;) {
32 printf("%02X ",(int)buf[i]);
33 i++;
34 if (i%8 == 0) printf(" ");
35 if (i%16 == 0) {
36 print_asc(&buf[i-16],8); printf(" ");
37 print_asc(&buf[i-8],8); printf("\n");
38 if (i<len) printf("[%03X] ",i);
41 if (i%16) {
42 int n;
44 n = 16 - (i%16);
45 printf(" ");
46 if (n>8) printf(" ");
47 while (n--) printf(" ");
49 n = i%16;
50 if (n > 8) n = 8;
51 print_asc(&buf[i-(i%16)],n); printf(" ");
52 n = (i%16) - n;
53 if (n>0) print_asc(&buf[i-n],n);
54 printf("\n");
58 static void help(void)
60 printf("
61 tdbtool:
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
70 ");
73 static void terror(char *why)
75 printf("%s\n", why);
78 static void create_tdb(void)
80 char *tok = strtok(NULL, " ");
81 if (!tok) {
82 help();
83 return;
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, " ");
93 if (!tok) {
94 help();
95 return;
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, " ");
105 TDB_DATA key, dbuf;
107 if (!k || !d) {
108 help();
109 return;
112 key.dptr = k;
113 key.dsize = strlen(k);
114 dbuf.dptr = d;
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, " ");
126 TDB_DATA key, dbuf;
128 if (!k || !d) {
129 help();
130 return;
133 key.dptr = k;
134 key.dsize = strlen(k);
135 dbuf.dptr = d;
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, " ");
146 TDB_DATA key, dbuf;
148 if (!k) {
149 help();
150 return;
153 key.dptr = k;
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, " ");
165 TDB_DATA key;
167 if (!k) {
168 help();
169 return;
172 key.dptr = k;
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);
186 return 0;
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;
194 return 0;
197 static void info_tdb(void)
199 int count;
200 total_bytes = 0;
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];
208 char *p;
209 fputs(prompt, stdout);
210 line[0] = 0;
211 p = fgets(line, sizeof(line)-1, stdin);
212 if (p) p = strchr(p, '\n');
213 if (p) *p = 0;
214 return p?line:NULL;
217 static int do_delete_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf,
218 void *state)
220 return tdb_delete(tdb, key);
223 int main(int argc, char *argv[])
225 char *line;
226 char *tok;
228 if (argv[1]) {
229 static char tmp[1024];
230 sprintf(tmp, "open %s", argv[1]);
231 tok=strtok(tmp," ");
232 open_tdb();
235 while ((line = getline("tdb> "))) {
237 /* Shell command */
239 if (line[0] == '!') {
240 system(line + 1);
241 continue;
244 if ((tok = strtok(line," "))==NULL) {
245 continue;
247 if (strcmp(tok,"create") == 0) {
248 create_tdb();
249 continue;
250 } else if (strcmp(tok,"open") == 0) {
251 open_tdb();
252 continue;
255 /* all the rest require a open database */
256 if (!tdb) {
257 terror("database not open");
258 help();
259 continue;
262 if (strcmp(tok,"insert") == 0) {
263 insert_tdb();
264 } else if (strcmp(tok,"store") == 0) {
265 store_tdb();
266 } else if (strcmp(tok,"show") == 0) {
267 show_tdb();
268 } else if (strcmp(tok,"erase") == 0) {
269 tdb_traverse(tdb, do_delete_fn, NULL);
270 } else if (strcmp(tok,"delete") == 0) {
271 delete_tdb();
272 } else if (strcmp(tok,"dump") == 0) {
273 tdb_traverse(tdb, print_rec, NULL);
274 } else if (strcmp(tok,"info") == 0) {
275 info_tdb();
276 } else {
277 help();
281 if (tdb) tdb_close(tdb);
283 return 0;