]> git.ozlabs.org Git - ccan/blob - ccan/tdb/tools/tdbdump.c
Use -O not -O3: reduces ccan/tdb test time from 24 to 18 seconds.
[ccan] / ccan / tdb / tools / tdbdump.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple tdb dump util
4    Copyright (C) Andrew Tridgell              2001
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <ccan/tdb/tdb.h>
21 #include <stdlib.h>
22 #include <getopt.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30
31 static void print_data(TDB_DATA d)
32 {
33         unsigned char *p = (unsigned char *)d.dptr;
34         int len = d.dsize;
35         while (len--) {
36                 if (isprint(*p) && !strchr("\"\\", *p)) {
37                         fputc(*p, stdout);
38                 } else {
39                         printf("\\%02X", *p);
40                 }
41                 p++;
42         }
43 }
44
45 static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
46 {
47         printf("{\n");
48         printf("key(%d) = \"", (int)key.dsize);
49         print_data(key);
50         printf("\"\n");
51         printf("data(%d) = \"", (int)dbuf.dsize);
52         print_data(dbuf);
53         printf("\"\n");
54         printf("}\n");
55         return 0;
56 }
57
58 static int dump_tdb(const char *fname, const char *keyname)
59 {
60         TDB_CONTEXT *tdb;
61         TDB_DATA key, value;
62         
63         tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
64         if (!tdb) {
65                 printf("Failed to open %s\n", fname);
66                 return 1;
67         }
68
69         if (!keyname) {
70                 tdb_traverse(tdb, traverse_fn, NULL);
71         } else {
72                 key.dptr = (void *)keyname;
73                 key.dsize = strlen( keyname);
74                 value = tdb_fetch(tdb, key);
75                 if (!value.dptr) {
76                         return 1;
77                 } else {
78                         print_data(value);
79                         free(value.dptr);
80                 }
81         }
82
83         return 0;
84 }
85
86 static void usage( void)
87 {
88         printf( "Usage: tdbdump [options] <filename>\n\n");
89         printf( "   -h          this help message\n");
90         printf( "   -k keyname  dumps value of keyname\n");
91 }
92
93  int main(int argc, char *argv[])
94 {
95         char *fname, *keyname=NULL;
96         int c;
97
98         if (argc < 2) {
99                 printf("Usage: tdbdump <fname>\n");
100                 exit(1);
101         }
102
103         while ((c = getopt( argc, argv, "hk:")) != -1) {
104                 switch (c) {
105                 case 'h':
106                         usage();
107                         exit( 0);
108                 case 'k':
109                         keyname = optarg;
110                         break;
111                 default:
112                         usage();
113                         exit( 1);
114                 }
115         }
116
117         fname = argv[optind];
118
119         return dump_tdb(fname, keyname);
120 }