r355: Fix a bunch of compiler warnings in the registry code.
[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         REG_KEY *subkey;
27         REG_VAL *value;
28         WERROR error;
29         int i;
30
31         for(i = 0; i < l; i++) putchar(' ');
32         if(fullpath) printf("%s\n", reg_key_get_path(p));
33         else printf("%s\n", reg_key_name(p));
34
35         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(p, i, &subkey)); i++) {
36                 print_tree(l+1, subkey, fullpath, novals);
37                 reg_key_free(subkey);
38         }
39
40         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
41                 DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n", reg_key_get_path(p), win_errstr(error)));
42         }
43
44         if(!novals) {
45                 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(p, i, &value)); i++) {
46                         int j;
47                         char *desc;
48                         for(j = 0; j < l+1; j++) putchar(' ');
49                         desc = reg_val_description(value);
50                         printf("%s\n", desc);
51                         free(desc);
52                         reg_val_free(value);
53                 }
54
55                 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
56                         DEBUG(0, ("Error occured while fetching values for '%s': %s\n", reg_key_get_path(p), win_errstr(error)));
57                 }
58         }
59 }
60
61 int main (int argc, char **argv)
62 {
63         int opt;
64         char *backend = "dir", *credentials = NULL;
65         poptContext pc;
66         REG_KEY *root;
67         REG_HANDLE *h;
68         WERROR error;
69         int fullpath = 0, no_values = 0;
70         struct poptOption long_options[] = {
71                 POPT_AUTOHELP
72                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
73                 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
74                 {"credentials", 'c', POPT_ARG_STRING, &credentials, 0, "credentials (user%password)", NULL},
75                 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
76                 POPT_TABLEEND
77         };
78
79         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
80         
81         while((opt = poptGetNextOpt(pc)) != -1) {
82         }
83
84         setup_logging("regtree", True);
85
86         error = reg_open(backend, poptPeekArg(pc), credentials, &h);
87         if(!W_ERROR_IS_OK(error)) {
88                 fprintf(stderr, "Unable to open '%s' with backend '%s':%s \n", poptGetArg(pc), backend, win_errstr(error));
89                 return 1;
90         }
91         poptFreeContext(pc);
92
93         error = reg_get_root(h, &root);
94         if(!W_ERROR_IS_OK(error)) return 1;
95
96         print_tree(0, root, fullpath, no_values);
97         
98         return 0;
99 }