Add myself as libcli/dns maintainer
[Samba/gebeck_regimport.git] / lib / tdb2 / tools / tdb2dump.c
blob40230a26431a8cf4bfd92f2b5b966b7f5bc25dde
1 /*
2 simple tdb2 dump util
3 Copyright (C) Andrew Tridgell 2001
4 Copyright (C) Rusty Russell 2011
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.
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.
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/>.
19 #include "config.h"
20 #include "tdb2.h"
21 #ifdef HAVE_LIBREPLACE
22 #include <replace.h>
23 #include <system/filesys.h>
24 #include <system/locale.h>
25 #else
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #endif
35 static void print_data(TDB_DATA d)
37 unsigned char *p = (unsigned char *)d.dptr;
38 int len = d.dsize;
39 while (len--) {
40 if (isprint(*p) && !strchr("\"\\", *p)) {
41 fputc(*p, stdout);
42 } else {
43 printf("\\%02X", *p);
45 p++;
49 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
51 printf("{\n");
52 printf("key(%d) = \"", (int)key.dsize);
53 print_data(key);
54 printf("\"\n");
55 printf("data(%d) = \"", (int)dbuf.dsize);
56 print_data(dbuf);
57 printf("\"\n");
58 printf("}\n");
59 return 0;
62 static int dump_tdb(const char *fname, const char *keyname)
64 struct tdb_context *tdb;
65 TDB_DATA key, value;
67 tdb = tdb_open(fname, 0, O_RDONLY, 0, NULL);
68 if (!tdb) {
69 printf("Failed to open %s\n", fname);
70 return 1;
73 if (!keyname) {
74 tdb_traverse(tdb, traverse_fn, NULL);
75 } else {
76 key = tdb_mkdata(keyname, strlen(keyname));
77 if (tdb_fetch(tdb, key, &value) != 0) {
78 return 1;
79 } else {
80 print_data(value);
81 free(value.dptr);
85 return 0;
88 static void usage( void)
90 printf( "Usage: tdb2dump [options] <filename>\n\n");
91 printf( " -h this help message\n");
92 printf( " -k keyname dumps value of keyname\n");
95 int main(int argc, char *argv[])
97 char *fname, *keyname=NULL;
98 int c;
100 if (argc < 2) {
101 printf("Usage: tdb2dump <fname>\n");
102 exit(1);
105 while ((c = getopt( argc, argv, "hk:")) != -1) {
106 switch (c) {
107 case 'h':
108 usage();
109 exit( 0);
110 case 'k':
111 keyname = optarg;
112 break;
113 default:
114 usage();
115 exit( 1);
119 fname = argv[optind];
121 return dump_tdb(fname, keyname);