deal with allocation size of 0 in prs_unistr when UNMARSHALLING
[Samba.git] / source / tdb / tdbtool.c
blob0e2104f3b04310aedd3546bc57adcb32e338a84d
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;
18 static void print_asc(unsigned char *buf,int len)
20 int i;
21 for (i=0;i<len;i++)
22 printf("%c",isprint(buf[i])?buf[i]:'.');
25 static void print_data(unsigned char *buf,int len)
27 int i=0;
28 if (len<=0) return;
29 printf("[%03X] ",i);
30 for (i=0;i<len;) {
31 printf("%02X ",(int)buf[i]);
32 i++;
33 if (i%8 == 0) printf(" ");
34 if (i%16 == 0) {
35 print_asc(&buf[i-16],8); printf(" ");
36 print_asc(&buf[i-8],8); printf("\n");
37 if (i<len) printf("[%03X] ",i);
40 if (i%16) {
41 int n;
43 n = 16 - (i%16);
44 printf(" ");
45 if (n>8) printf(" ");
46 while (n--) printf(" ");
48 n = i%16;
49 if (n > 8) n = 8;
50 print_asc(&buf[i-(i%16)],n); printf(" ");
51 n = (i%16) - n;
52 if (n>0) print_asc(&buf[i-n],n);
53 printf("\n");
57 static void help(void)
59 printf("
60 tdbtool:
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
69 ");
72 static void terror(char *why)
74 printf("%s\n", why);
77 static void create_tdb(void)
79 char *tok = strtok(NULL, " ");
80 if (!tok) {
81 help();
82 return;
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, " ");
92 if (!tok) {
93 help();
94 return;
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, " ");
104 TDB_DATA key, dbuf;
106 if (!k || !d) {
107 help();
108 return;
111 key.dptr = k;
112 key.dsize = strlen(k);
113 dbuf.dptr = d;
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, " ");
125 TDB_DATA key, dbuf;
127 if (!k || !d) {
128 help();
129 return;
132 key.dptr = k;
133 key.dsize = strlen(k);
134 dbuf.dptr = d;
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, " ");
145 TDB_DATA key, dbuf;
147 if (!k) {
148 help();
149 return;
152 key.dptr = k;
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, " ");
163 TDB_DATA key;
165 if (!k) {
166 help();
167 return;
170 key.dptr = k;
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);
184 return 0;
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;
192 return 0;
195 static void info_tdb(void)
197 int count;
198 total_bytes = 0;
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];
206 char *p;
207 fputs(prompt, stdout);
208 line[0] = 0;
209 p = fgets(line, sizeof(line)-1, stdin);
210 if (p) p = strchr(p, '\n');
211 if (p) *p = 0;
212 return p?line:NULL;
215 static int do_delete_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf,
216 void *state)
218 return tdb_delete(tdb, key);
221 int main(int argc, char *argv[])
223 char *line;
224 char *tok;
226 while ((line = getline("tdb> "))) {
228 /* Shell command */
230 if (line[0] == '!') {
231 system(line + 1);
232 continue;
235 tok = strtok(line," ");
236 if (strcmp(tok,"create") == 0) {
237 create_tdb();
238 continue;
239 } else if (strcmp(tok,"open") == 0) {
240 open_tdb();
241 continue;
244 /* all the rest require a open database */
245 if (!tdb) {
246 terror("database not open");
247 help();
248 continue;
251 if (strcmp(tok,"insert") == 0) {
252 insert_tdb();
253 } else if (strcmp(tok,"store") == 0) {
254 store_tdb();
255 } else if (strcmp(tok,"show") == 0) {
256 show_tdb();
257 } else if (strcmp(tok,"erase") == 0) {
258 tdb_traverse(tdb, do_delete_fn, NULL);
259 } else if (strcmp(tok,"delete") == 0) {
260 delete_tdb();
261 } else if (strcmp(tok,"dump") == 0) {
262 tdb_traverse(tdb, print_rec, NULL);
263 } else if (strcmp(tok,"info") == 0) {
264 info_tdb();
265 } else {
266 help();
270 if (tdb) tdb_close(tdb);
272 return 0;