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