2002-04-12 01:58:40 +02:00
|
|
|
/*
|
|
|
|
* Windows regedit.exe registry editor implementation.
|
|
|
|
*
|
|
|
|
* Copyright 2002 Andriy Palamarchuk
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
2012-01-23 12:02:39 +01:00
|
|
|
#include <stdlib.h>
|
2002-04-12 01:58:40 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include "regproc.h"
|
|
|
|
|
2005-06-04 12:01:25 +02:00
|
|
|
static const char *usage =
|
2003-12-08 23:48:07 +01:00
|
|
|
"Usage:\n"
|
|
|
|
" regedit filename\n"
|
|
|
|
" regedit /E filename [regpath]\n"
|
|
|
|
" regedit /D regpath\n"
|
|
|
|
"\n"
|
|
|
|
"filename - registry file name\n"
|
|
|
|
"regpath - name of the registry key\n"
|
|
|
|
"\n"
|
2007-05-15 14:01:05 +02:00
|
|
|
"When called without any switches, adds the content of the specified\n"
|
|
|
|
"file to the registry\n"
|
2003-12-08 23:48:07 +01:00
|
|
|
"\n"
|
|
|
|
"Switches:\n"
|
|
|
|
" /E - exports contents of the specified registry key to the specified\n"
|
|
|
|
" file. Exports the whole registry if no key is specified.\n"
|
|
|
|
" /D - deletes specified registry key\n"
|
|
|
|
" /S - silent execution, can be used with any other switch.\n"
|
2007-05-09 18:18:29 +02:00
|
|
|
" Default. The only existing mode, exists for compatibility with Windows regedit.\n"
|
2003-12-08 23:48:07 +01:00
|
|
|
" /V - advanced mode, can be used with any other switch.\n"
|
|
|
|
" Ignored, exists for compatibility with Windows regedit.\n"
|
|
|
|
" /L - location of system.dat file. Can be used with any other switch.\n"
|
|
|
|
" Ignored. Exists for compatibility with Windows regedit.\n"
|
|
|
|
" /R - location of user.dat file. Can be used with any other switch.\n"
|
|
|
|
" Ignored. Exists for compatibility with Windows regedit.\n"
|
|
|
|
" /? - print this help. Any other switches are ignored.\n"
|
2007-05-09 18:18:29 +02:00
|
|
|
" /C - create registry from file. Not implemented.\n"
|
2003-12-08 23:48:07 +01:00
|
|
|
"\n"
|
|
|
|
"The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
|
|
|
|
"This program is command-line compatible with Microsoft Windows\n"
|
2005-06-23 11:47:17 +02:00
|
|
|
"regedit.\n";
|
2002-04-12 01:58:40 +02:00
|
|
|
|
|
|
|
typedef enum {
|
2016-06-22 14:59:13 +02:00
|
|
|
ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
|
2002-04-12 01:58:40 +02:00
|
|
|
} REGEDIT_ACTION;
|
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
static BOOL PerformRegAction(REGEDIT_ACTION action, char **argv, int *i)
|
2003-08-07 05:10:13 +02:00
|
|
|
{
|
2003-12-08 23:48:07 +01:00
|
|
|
switch (action) {
|
|
|
|
case ACTION_ADD: {
|
2016-06-22 14:59:13 +02:00
|
|
|
char *filename = argv[*i];
|
2003-12-08 23:48:07 +01:00
|
|
|
FILE *reg_file;
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
if (filename[0]) {
|
2004-01-13 06:07:07 +01:00
|
|
|
char* realname = NULL;
|
2007-10-09 21:04:28 +02:00
|
|
|
|
|
|
|
if (strcmp(filename, "-") == 0)
|
2004-01-13 06:07:07 +01:00
|
|
|
{
|
2011-04-16 10:07:34 +02:00
|
|
|
reg_file = stdin;
|
2004-01-13 06:07:07 +01:00
|
|
|
}
|
2007-10-09 21:04:28 +02:00
|
|
|
else
|
2004-01-13 06:07:07 +01:00
|
|
|
{
|
2007-10-09 21:04:28 +02:00
|
|
|
int size;
|
|
|
|
|
2011-04-16 10:07:34 +02:00
|
|
|
size = SearchPathA(NULL, filename, NULL, 0, NULL, NULL);
|
|
|
|
if (size > 0)
|
2007-10-09 21:04:28 +02:00
|
|
|
{
|
2011-04-16 10:07:34 +02:00
|
|
|
realname = HeapAlloc(GetProcessHeap(), 0, size);
|
|
|
|
size = SearchPathA(NULL, filename, NULL, size, realname, NULL);
|
2007-10-09 21:04:28 +02:00
|
|
|
}
|
2011-04-16 10:07:34 +02:00
|
|
|
if (size == 0)
|
2007-10-09 21:04:28 +02:00
|
|
|
{
|
2016-06-22 14:03:07 +02:00
|
|
|
fprintf(stderr, "regedit: File not found \"%s\" (%d)\n",
|
|
|
|
filename, GetLastError());
|
2007-10-09 21:04:28 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-11-20 01:06:51 +01:00
|
|
|
reg_file = fopen(realname, "rb");
|
2011-04-16 10:07:34 +02:00
|
|
|
if (reg_file == NULL)
|
2007-10-09 21:04:28 +02:00
|
|
|
{
|
|
|
|
perror("");
|
2016-06-22 14:03:07 +02:00
|
|
|
fprintf(stderr, "regedit: Can't open file \"%s\"\n", filename);
|
2007-10-09 21:04:28 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
2008-07-09 23:39:08 +02:00
|
|
|
import_registry_file(reg_file);
|
2004-01-13 06:07:07 +01:00
|
|
|
if (realname)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(),0,realname);
|
|
|
|
fclose(reg_file);
|
|
|
|
}
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTION_DELETE: {
|
2016-06-22 14:03:29 +02:00
|
|
|
WCHAR *reg_key_nameW = GetWideString(argv[*i]);
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2016-06-22 14:03:29 +02:00
|
|
|
delete_registry_key(reg_key_nameW);
|
|
|
|
HeapFree(GetProcessHeap(), 0, reg_key_nameW);
|
2003-12-08 23:48:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTION_EXPORT: {
|
2016-06-22 14:59:13 +02:00
|
|
|
char *filename = argv[*i];
|
2008-09-08 21:44:08 +02:00
|
|
|
WCHAR* filenameW;
|
2003-12-08 23:48:07 +01:00
|
|
|
|
2008-09-08 21:44:08 +02:00
|
|
|
filenameW = GetWideString(filename);
|
2016-06-22 14:59:13 +02:00
|
|
|
if (filenameW[0]) {
|
|
|
|
char *reg_key_name = argv[++(*i)];
|
2008-09-08 21:44:08 +02:00
|
|
|
WCHAR* reg_key_nameW;
|
2003-12-08 23:48:07 +01:00
|
|
|
|
2008-09-08 21:44:08 +02:00
|
|
|
reg_key_nameW = GetWideString(reg_key_name);
|
2008-09-14 02:21:23 +02:00
|
|
|
export_registry_key(filenameW, reg_key_nameW, REG_FORMAT_4);
|
2008-09-08 21:44:08 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, reg_key_nameW);
|
2003-12-08 23:48:07 +01:00
|
|
|
} else {
|
2008-09-14 02:21:23 +02:00
|
|
|
export_registry_key(filenameW, NULL, REG_FORMAT_4);
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
2008-09-08 21:44:08 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, filenameW);
|
2003-12-08 23:48:07 +01:00
|
|
|
break;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
default:
|
2016-06-22 14:03:07 +02:00
|
|
|
fprintf(stderr, "regedit: Unhandled action!\n");
|
2002-04-12 01:58:40 +02:00
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
2003-08-15 05:47:04 +02:00
|
|
|
return TRUE;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
char *get_token(char *input, char **next)
|
|
|
|
{
|
|
|
|
char *ch = input;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
while (*ch && isspace(*ch))
|
|
|
|
ch++;
|
|
|
|
|
|
|
|
str = ch;
|
|
|
|
|
|
|
|
if (*ch == '"') {
|
|
|
|
ch++;
|
|
|
|
str = ch;
|
|
|
|
for (;;) {
|
|
|
|
while (*ch && (*ch != '"'))
|
|
|
|
ch++;
|
|
|
|
|
|
|
|
if (!*ch)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*(ch - 1) == '\\') {
|
|
|
|
ch++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (*ch && !isspace(*ch))
|
|
|
|
ch++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ch) {
|
|
|
|
*ch = 0;
|
|
|
|
ch++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*next = ch;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-06-13 13:18:50 +02:00
|
|
|
BOOL ProcessCmdLine(LPSTR lpCmdLine)
|
|
|
|
{
|
2016-06-22 14:59:13 +02:00
|
|
|
char *s = lpCmdLine;
|
|
|
|
char **argv;
|
|
|
|
char *tok;
|
|
|
|
int argc = 0, i = 1;
|
|
|
|
REGEDIT_ACTION action = ACTION_ADD;
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
if (!*lpCmdLine)
|
|
|
|
return FALSE;
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
while (*s) {
|
|
|
|
if (isspace(*s))
|
|
|
|
i++;
|
2007-06-13 13:18:50 +02:00
|
|
|
s++;
|
2016-06-22 14:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s = lpCmdLine;
|
|
|
|
argv = HeapAlloc(GetProcessHeap(), 0, i * sizeof(char *));
|
|
|
|
|
|
|
|
for (i = 0; *s; i++)
|
|
|
|
{
|
|
|
|
tok = get_token(s, &s);
|
|
|
|
argv[i] = HeapAlloc(GetProcessHeap(), 0, strlen(tok) + 1);
|
|
|
|
strcpy(argv[i], tok);
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (argv[i][0] != '/' && argv[i][0] != '-')
|
|
|
|
break; /* No flags specified. */
|
|
|
|
|
|
|
|
if (!argv[i][1] && argv[i][0] == '-')
|
|
|
|
break; /* '-' is a filename. It indicates we should use stdin. */
|
|
|
|
|
|
|
|
if (argv[i][1] && argv[i][2] && argv[i][2] != ':')
|
|
|
|
break; /* This is a file path beginning with '/'. */
|
|
|
|
|
|
|
|
switch (toupper(argv[i][1]))
|
2007-10-09 21:04:28 +02:00
|
|
|
{
|
2016-06-22 14:59:13 +02:00
|
|
|
case '?':
|
|
|
|
fprintf(stderr, usage);
|
|
|
|
exit(0);
|
2007-10-09 21:04:28 +02:00
|
|
|
break;
|
2016-06-22 14:59:13 +02:00
|
|
|
case 'D':
|
|
|
|
action = ACTION_DELETE;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
action = ACTION_EXPORT;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
case 'R':
|
|
|
|
/* unhandled */;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
case 'V':
|
|
|
|
/* ignored */;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "regedit: Invalid switch [%ls]\n", argv[i]);
|
|
|
|
exit(1);
|
2007-06-13 13:18:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 14:03:29 +02:00
|
|
|
if (i == argc)
|
|
|
|
{
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case ACTION_ADD:
|
|
|
|
case ACTION_EXPORT:
|
|
|
|
fprintf(stderr, "regedit: No file name was specified\n\n");
|
|
|
|
break;
|
|
|
|
case ACTION_DELETE:
|
|
|
|
fprintf(stderr,"regedit: No registry key was specified for removal\n\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(stderr, usage);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
PerformRegAction(action, argv, &i);
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
HeapFree(GetProcessHeap(), 0, argv[i]);
|
|
|
|
HeapFree(GetProcessHeap(), 0, argv);
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
return TRUE;
|
2007-06-13 13:18:50 +02:00
|
|
|
}
|