summaryrefslogtreecommitdiffstats
path: root/TOOLS/modify_reg.c
blob: 888d9eac327519278fa745cc2c6d4701a986deb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  A program to modify a registry file.
 *
 *  Copyright (C) 2007 Alan Nisota
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <unistd.h>
#include <getopt.h>
#include "loader/registry.c"

#include "mp_msg.h"
#ifdef __GNUC__
#define mp_msg(t, l, m, args...) fprintf(stderr, m, ##args)
#else
#define mp_msg(t, l, ...) fprintf(stderr, __VA_ARGS__)
#endif

#include "get_path.c"

static void remove_key(long handle, const char* name) {
    int i, len;
    char *fullname;

    fullname = build_keyname(handle, name);
    len = strlen(fullname);
    for (i=0; i < reg_size;) {
        if (!strncmp(regs[i].name, fullname, len)) {
            free(regs[i].value);
            free(regs[i].name);
            memmove(&regs[i], &regs[i+1], --reg_size*sizeof(struct reg_value));
        } else {
            i++;
        }
    }
    free(fullname);
    save_registry();
}

static void parse_key(char *raw, HKEY *root, char *path, char *key) {
    char *tmpkey, *start;
    tmpkey = strrchr(raw, '\\');
    if (tmpkey == raw || tmpkey == NULL) {
        printf("Couldn't process key \"%s\"\n", raw);
        return;
    }
    start = strchr(raw, '\\');
    if (start == raw || start == NULL) {
        printf("Couldn't process key \"%s\"\n", raw);
        return;
    }
    if (strncmp(raw, "HKEY_CURRENT_USER\\", 18) == 0 ||
            strncmp(raw, "HKCU\\", 5) == 0) {
        *root = HKEY_CURRENT_USER;
    } else if (strncmp(raw, "HKEY_LOCAL_MACHINE\\", 19) == 0 ||
            strncmp(raw, "HKLM\\", 5) == 0) {
        *root = HKEY_LOCAL_MACHINE;
    } else {
        printf("Couldn't process key \"%s\"\n", raw);
        return;
    }
    strncpy(key, tmpkey + 1, strlen(tmpkey)-1);
    key[strlen(tmpkey)-1] = 0;
    while(*start == '\\')
        start++;
    while(*(tmpkey-1) == '\\')
        tmpkey--;
    strncpy(path, start, tmpkey - start);
    path[tmpkey - start] = 0;
}

int main(int argc, char *argv[]) {
    int i;
    long type = REG_SZ;
    char c, path[256], key[256], *value = NULL;
    HKEY root = 0;
    int Option_Index;
    int list = 0, del = 0;
    int newkey, status;
    static struct option Long_Options[] = {
        {"registry", 1, 0, 'r'},
        {"list", 0, 0, 'l'},
        {"key", 1, 0, 'k'},
        {"value", 1, 0, 'v'},
        {"type", 1, 0, 't'},
        {"del", 0, 0, 'd'},
    };
    while(1) {
        c = getopt_long(argc, argv, "r:lk:v:t:id", Long_Options, &Option_Index);
        if (c == EOF)
            break;
        switch(c) {
          case 'r':
            localregpathname = optarg;
            break;
          case 'l':
            list = 1;
            break;
          case 'k':
            parse_key(optarg, &root, path, key);
            break;
          case 'v':
            value = optarg;
            break;
          case 't':
            if (!strcmp(optarg, "string"))
                type = REG_SZ;
            else if (!strcmp(optarg, "dword"))
                type = REG_DWORD;
            break;
          case 'd':
            del = 1;
            break;
        }
    }
    if (localregpathname == NULL || (! list && ! root)) {
        printf("Must specify '-r' and either '-k' or '-l'\n");
        return 1;
    }
    if (del && (list || value)) {
        printf("Can't specify '-d' along with '-l' or '-v'\n");
        return 1;
    }
    open_registry();
    insert_handle(HKEY_LOCAL_MACHINE, "HKLM");
    insert_handle(HKEY_CURRENT_USER, "HKCU");

    if (del) {
        char tmpname[256];
        sprintf(tmpname, "%s\\%s", path, key);
        remove_key(root, tmpname);
        return 0;
    }

    if (list) {
        for (i=0; i < reg_size; i++) {
            if (regs[i].type == DIR) {
                printf("Directory:  %s\n", regs[i].name);
            } else if (regs[i].type == REG_DWORD) {
                DWORD v = *(DWORD *)regs[i].value;
                printf("%s :: %08x  type: DWORD\n", regs[i].name, v);
            } else if (regs[i].type == REG_SZ) {
                printf("%s :: '%s' len: %d type: String\n",
                        regs[i].name, regs[i].value, regs[i].len);
            } else {
                printf("%s :: '%s' len: %d type: %08x\n",
                        regs[i].name, regs[i].value, regs[i].len, regs[i].type);
            }
        }
    }
    if (root) {
        RegCreateKeyExA(root, path, 0, 0, 0, 0, 0, &newkey, &status);
        if (value != NULL) {
            int len;
            DWORD v;
            if (type == REG_DWORD) {
                len = sizeof(DWORD);
                v = strtoul(value, NULL, 0);
                value = (char *)&v;
            } else
                len = strlen(value)+1;
            printf("%08x -- %d\n", *value, len);
            RegSetValueExA(newkey, key, 0, type, value, len);
        }
    }
    return 0;
}