r20: Add the registry library. Still needs a lot of work,
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / tools / regtree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple registry frontend
4    
5    Copyright (C) Jelmer Vernooij 2004
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 void print_tree(int l, REG_KEY *p, int fullpath, int novals)
25 {
26         int num_subkeys, i, num_values;
27
28         for(i = 0; i < l; i++) putchar(' ');
29         if(fullpath) printf("%s\n", reg_key_get_path(p));
30         else printf("%s\n", reg_key_name(p));
31
32         num_subkeys = reg_key_num_subkeys(p);
33         for(i = 0; i < num_subkeys; i++) {
34                 REG_KEY *subkey = reg_key_get_subkey_by_index(p, i);
35                 print_tree(l+1, subkey, fullpath, novals);
36                 reg_key_free(subkey);
37         }
38
39         if(!novals) {
40                 num_values = reg_key_num_values(p);
41                 for(i = 0; i < num_values; i++) {
42                         int j;
43                         char *desc;
44                         REG_VAL *value = reg_key_get_value_by_index(p, i);
45                         for(j = 0; j < l+1; j++) putchar(' ');
46                         desc = reg_val_description(value);
47                         printf("%s\n", desc);
48                         free(desc);
49                         reg_val_free(value);
50                 }
51         }
52 }
53
54 int main (int argc, char **argv)
55 {
56         uint32  setparms, checkparms;
57         int opt;
58         char *backend = "dir";
59         poptContext pc;
60         REG_KEY *root;
61         REG_HANDLE *h;
62         int fullpath = 0, no_values = 0;
63         struct poptOption long_options[] = {
64                 POPT_AUTOHELP
65                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
66                 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
67                 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
68                 POPT_TABLEEND
69         };
70
71         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
72         
73         while((opt = poptGetNextOpt(pc)) != -1) {
74         }
75
76         setup_logging("regtree", True);
77
78         h = reg_open(backend, poptPeekArg(pc), True);
79         if(!h) {
80                 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", poptGetArg(pc), backend);
81                 return 1;
82         }
83         poptFreeContext(pc);
84
85         root = reg_get_root(h);
86         if(!root) return 1;
87
88         print_tree(0, root, fullpath, no_values);
89         
90         return 0;
91 }