070516b798f4f896fc82e3a7fca84dd11141267c
[ira/wip.git] / source / lib / registry / tools / regdiff.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 writediff(REG_KEY *oldkey, REG_KEY *newkey, FILE *out)
25 {
26         int i, numvals1, numvals2, numkeys2;
27
28         numkeys2 = reg_key_num_subkeys(newkey);
29         for(i = 0; i < numkeys2; i++) {
30                 REG_KEY *t1 = reg_key_get_subkey_by_index(newkey, i);
31                 REG_KEY *t2 = reg_key_get_subkey_by_name(oldkey, reg_key_name(t1));
32                 if(!t2) {
33                         fprintf(out, "[%s]\n", reg_key_get_path(t1));
34                 }
35                 writediff(t2, t1, out);
36         }
37
38         numvals2 = reg_key_num_values(newkey);
39         for(i = 0; i < numvals2; i++) {
40                 REG_VAL *t1 = reg_key_get_value_by_index(newkey, i);
41                 REG_VAL *t2 = reg_key_get_value_by_name(oldkey, reg_val_name(t1));
42                 if(!t2 || reg_val_size(t2) != reg_val_size(t1) || memcmp(reg_val_data_blk(t1), reg_val_data_blk(t2), reg_val_size(t1))) {
43                         fprintf(out, "\"%s\"=%s:%s\n", reg_val_name(t1), str_regtype(reg_val_type(t1)), reg_val_data_string(t1));
44                 }
45         }
46
47         numvals1 = reg_key_num_values(oldkey);
48         for(i = 0; i < numvals1; i++) {
49                 REG_VAL *t1 = reg_key_get_value_by_index(oldkey, i);
50                 if(!reg_key_get_value_by_name(newkey, reg_val_name(t1))) {
51                         fprintf(out, "\"%s\"=-\n", reg_val_name(t1));
52                 }
53         }
54 }
55
56 int main (int argc, char **argv)
57 {
58         uint32  setparms, checkparms;
59         int opt;
60         poptContext pc;
61         REG_KEY *root;
62         const char *backend1 = NULL, *backend2 = NULL;
63         const char *location2;
64         char *outputfile = NULL;
65         FILE *fd = stdout;
66         REG_HANDLE *h2;
67         REG_KEY *root1 = NULL, *root2;
68         int from_null = 0;
69         int fullpath = 0, no_values = 0;
70         struct poptOption long_options[] = {
71                 POPT_AUTOHELP
72                 {"backend", 'b', POPT_ARG_STRING, NULL, 'b', "backend to use", NULL},
73                 {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
74                 {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL" },
75                 POPT_TABLEEND
76         };
77
78         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
79         
80         while((opt = poptGetNextOpt(pc)) != -1) {
81                 switch(opt)     {
82                         case 'b':
83                                 if(!backend1 && !from_null) backend1 = poptGetOptArg(pc);
84                                 else if(!backend2) backend2 = poptGetOptArg(pc);
85                                 break;
86                 }
87         }
88         setup_logging(argv[0], True);
89
90         if(!from_null) {
91                 REG_HANDLE *h1;
92                 const char *location1;
93                 location1 = poptGetArg(pc);
94                 if(!location1) {
95                         poptPrintUsage(pc, stderr, 0);
96                         return 1;
97                 }
98
99                 if(!backend1) backend1 = "dir";
100
101                 h1 = reg_open(backend1, location1, True);
102                 if(!h1) {
103                         fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location1, backend1);
104                         return 1;
105                 }
106
107                 root1 = reg_get_root(h1);
108         }
109
110         location2 = poptGetArg(pc);
111         if(!location2) {
112                 poptPrintUsage(pc, stderr, 0);
113                 return 2;
114         }
115
116         if(!backend2) backend2 = "dir";
117         
118         h2 = reg_open(backend2, location2, True);
119         if(!h2) {
120                 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location2, backend2);
121                 return 1;
122         }
123         
124         root2 = reg_get_root(h2);
125         if(!root2) {
126                 fprintf(stderr, "Can't open root key for '%s:%s'\n", backend2, location2);
127                 return 1;
128         }
129
130         poptFreeContext(pc);
131
132         if(outputfile) {
133                 fd = fopen(outputfile, "w+");
134                 if(!fd) {
135                         fprintf(stderr, "Unable to open '%s'\n", outputfile);
136                         return 1;
137                 }
138         }
139
140         fprintf(fd, "REGEDIT4\n\n");
141         fprintf(fd, "; Generated using regdiff\n");
142
143         writediff(root1, root2, fd); 
144
145         fclose(fd);
146         
147         return 0;
148 }