annotate TOOLS/modify_reg.c @ 26031:559a244d5679

Add MPLAYER_ prefix to the multiple inclusion guards of generated header files.
author diego
date Fri, 22 Feb 2008 17:38:20 +0000
parents 73aafdcc92fc
children 1ca484e74f18
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
1 /*
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
2 * modify_reg.c
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
3 * A program to modify a registry file.
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
4 *
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
5 * Copyright (C) 2007 Alan Nisota
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
6 *
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
7 * This file is part of MPlayer, a free movie player.
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
8 *
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
9 * MPlayer is free software; you can redistribute it and/or modify
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
11 * the Free Software Foundation; either version 2, or (at your option)
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
12 * any later version.
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
13 *
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
14 * MPlayer is distributed in the hope that it will be useful,
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
17 * GNU General Public License for more details.
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
18 *
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
20 * along with MPlayer; if not, write to the Free Software
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
22 */
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
23
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
24 #include <unistd.h>
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
25 #include <getopt.h>
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
26 #include "loader/registry.c"
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
27
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
28 #include "mp_msg.h"
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
29 #ifdef __GNUC__
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
30 #define mp_msg(t, l, m, args...) fprintf(stderr, m, ##args)
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
31 #else
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
32 #define mp_msg(t, l, ...) fprintf(stderr, __VA_ARGS__)
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
33 #endif
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
34
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
35 #include "get_path.c"
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
36
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
37 static void remove_key(long handle, const char* name) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
38 int i, len;
24180
73aafdcc92fc warning fix:
diego
parents: 23996
diff changeset
39 char *fullname;
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
40
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
41 fullname = build_keyname(handle, name);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
42 len = strlen(fullname);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
43 for (i=0; i < reg_size;) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
44 if (!strncmp(regs[i].name, fullname, len)) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
45 free(regs[i].value);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
46 free(regs[i].name);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
47 memmove(&regs[i], &regs[i+1], --reg_size*sizeof(struct reg_value));
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
48 } else {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
49 i++;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
50 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
51 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
52 free(fullname);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
53 save_registry();
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
54 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
55
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
56 void parse_key(char *raw, HKEY *root, char *path, char *key) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
57 char *tmpkey, *start;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
58 tmpkey = strrchr(raw, '\\');
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
59 if (tmpkey == raw || tmpkey == NULL) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
60 printf("Couldn't process key \"%s\"\n", raw);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
61 return;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
62 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
63 start = strchr(raw, '\\');
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
64 if (start == raw || start == NULL) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
65 printf("Couldn't process key \"%s\"\n", raw);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
66 return;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
67 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
68 if (strncmp(raw, "HKEY_CURRENT_USER\\", 18) == 0 ||
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
69 strncmp(raw, "HKCU\\", 5) == 0) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
70 *root = HKEY_CURRENT_USER;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
71 } else if (strncmp(raw, "HKEY_LOCAL_MACHINE\\", 19) == 0 ||
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
72 strncmp(raw, "HKLM\\", 5) == 0) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
73 *root = HKEY_LOCAL_MACHINE;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
74 } else {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
75 printf("Couldn't process key \"%s\"\n", raw);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
76 return;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
77 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
78 strncpy(key, tmpkey + 1, strlen(tmpkey)-1);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
79 key[strlen(tmpkey)-1] = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
80 while(*start == '\\')
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
81 start++;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
82 while(*(tmpkey-1) == '\\')
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
83 tmpkey--;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
84 strncpy(path, start, tmpkey - start);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
85 path[tmpkey - start] = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
86 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
87
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
88 int main(int argc, char *argv[]) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
89 int i;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
90 long type = REG_SZ;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
91 char c, path[256], key[256], *value = NULL;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
92 HKEY root = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
93 int Option_Index;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
94 int list = 0, del = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
95 int newkey, status;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
96 static struct option Long_Options[] = {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
97 {"registry", 1, 0, 'r'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
98 {"list", 0, 0, 'l'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
99 {"key", 1, 0, 'k'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
100 {"value", 1, 0, 'v'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
101 {"type", 1, 0, 't'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
102 {"del", 0, 0, 'd'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
103 };
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
104 while(1) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
105 c = getopt_long(argc, argv, "r:lk:v:t:id", Long_Options, &Option_Index);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
106 if (c == EOF)
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
107 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
108 switch(c) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
109 case 'r':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
110 localregpathname = optarg;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
111 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
112 case 'l':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
113 list = 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
114 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
115 case 'k':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
116 parse_key(optarg, &root, path, key);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
117 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
118 case 'v':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
119 value = optarg;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
120 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
121 case 't':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
122 if (!strcmp(optarg, "string"))
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
123 type = REG_SZ;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
124 else if (!strcmp(optarg, "dword"))
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
125 type = REG_DWORD;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
126 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
127 case 'd':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
128 del = 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
129 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
130 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
131 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
132 if (localregpathname == NULL || (! list && ! root)) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
133 printf("Must specify '-r' and either '-k' or '-l'\n");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
134 return 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
135 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
136 if (del && (list || value)) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
137 printf("Can't specify '-d' along with '-l' or '-v'\n");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
138 return 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
139 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
140 open_registry();
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
141 insert_handle(HKEY_LOCAL_MACHINE, "HKLM");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
142 insert_handle(HKEY_CURRENT_USER, "HKCU");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
143
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
144 if (del) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
145 char tmpname[256];
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
146 sprintf(tmpname, "%s\\%s", path, key);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
147 remove_key(root, tmpname);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
148 return 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
149 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
150
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
151 if (list) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
152 for (i=0; i < reg_size; i++) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
153 if (regs[i].type == DIR) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
154 printf("Directory: %s\n", regs[i].name);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
155 } else if (regs[i].type == REG_DWORD) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
156 DWORD v = *(DWORD *)regs[i].value;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
157 printf("%s :: %08x type: DWORD\n", regs[i].name, v);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
158 } else if (regs[i].type == REG_SZ) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
159 printf("%s :: '%s' len: %d type: String\n",
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
160 regs[i].name, regs[i].value, regs[i].len);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
161 } else {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
162 printf("%s :: '%s' len: %d type: %08x\n",
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
163 regs[i].name, regs[i].value, regs[i].len, regs[i].type);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
164 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
165 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
166 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
167 if (root) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
168 RegCreateKeyExA(root, path, 0, 0, 0, 0, 0, &newkey, &status);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
169 if (value != NULL) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
170 int len;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
171 DWORD v;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
172 if (type == REG_DWORD) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
173 len = sizeof(DWORD);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
174 v = strtoul(value, NULL, 0);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
175 value = (char *)&v;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
176 } else
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
177 len = strlen(value)+1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
178 printf("%08x -- %d\n", *value, len);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
179 RegSetValueExA(newkey, key, 0, type, value, len);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
180 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
181 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
182 return 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
183 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
184