From 57e3834806a970cc27d477e26279edd5468ab3cb Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 17 Jun 2008 12:32:51 -0700 Subject: [PATCH] reg: Implement basic 'reg add'. --- programs/reg/En.rc | 3 + programs/reg/reg.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++-- programs/reg/reg.h | 3 + 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/programs/reg/En.rc b/programs/reg/En.rc index cd26cc1..75d66a8 100644 --- a/programs/reg/En.rc +++ b/programs/reg/En.rc @@ -27,4 +27,7 @@ STRINGTABLE STRING_ADD_USAGE, "REG ADD key_name [/v value_name | /ve] [/t type] [/s separator] [/d data] [/f]\n" STRING_DELETE_USAGE, "REG DELETE key_name [/v value_name | /ve | /va] [/f]\n" STRING_QUERY_USAGE, "REG QUERY key_name [/v value_name | /ve] [/s]\n" + STRING_INVALID_PARAMS, "Invalid parameters.\n" + STRING_INVALID_ROOTKEY, "Invalid root key.\n" + STRING_INVALID_SUBKEY, "Invalid subkey.\n" } diff --git a/programs/reg/reg.c b/programs/reg/reg.c index 32b533f..3a4e2f4 100644 --- a/programs/reg/reg.c +++ b/programs/reg/reg.c @@ -16,9 +16,15 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#include +#include #include #include "reg.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(reg); + static int reg_printfW(const WCHAR *msg, ...) { va_list va_args; @@ -66,14 +72,106 @@ static int reg_message(int msg, ...) return 0; } +static LPCTSTR reg_split_key(HKEY *hKey, WCHAR *key_name) +{ + unsigned int i; + LPCWSTR lpSubKey; + + static const struct keys { + WCHAR name[5]; + unsigned char size; + HKEY key; + } keys[] = + { + {{'h','k','c','r',0}, 4, HKEY_CLASSES_ROOT}, + {{'h','k','c','u',0}, 4, HKEY_CURRENT_USER}, + {{'h','k','l','m',0}, 4, HKEY_LOCAL_MACHINE}, + {{'h','k','u',0}, 3, HKEY_USERS}, + {{'h','k','p','d',0}, 4, HKEY_PERFORMANCE_DATA}, + {{'h','k','c','c',0}, 4, HKEY_CURRENT_CONFIG}, + {{'h','k','d','d',0}, 4, HKEY_DYN_DATA} + }; + + /* Determine key type. */ + *hKey = NULL; + for (i = 0; i < sizeof (keys)/sizeof(struct keys); i++) + { + if (CompareString(LOCALE_NEUTRAL, NORM_IGNORECASE, key_name, + keys[i].size, keys[i].name, keys[i].size) == CSTR_EQUAL) + *hKey = keys[i].key; + } + if (*hKey == NULL) + { + reg_message(STRING_INVALID_ROOTKEY); + return NULL; + } + + /* Find the subkey. */ + lpSubKey = key_name; + for (;;) + { + if (lpSubKey[0] == '\0') + { + reg_message(STRING_INVALID_SUBKEY); + return NULL; + } + if (lpSubKey[0] == '\\') + break; + lpSubKey++; + } + lpSubKey++; + + return lpSubKey; +} + static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty, WCHAR *type, WCHAR separator, WCHAR *data, BOOL force) { - const WCHAR stubW[] = {'S','T','U','B',' ','A','D','D',' ','-',' ','%','s', - ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0}; - reg_printfW(stubW, key_name, value_name, value_empty, type, data, force); + BOOL ret; + HKEY hKey = 0, hkResult; + LPCTSTR lpSubKey; - return 1; + /* Can't have a value be empty and have it set at the same time. */ + if (value_empty && value_name != NULL) + { + reg_message(STRING_INVALID_PARAMS); + return 1; + } + /* Can't have data or a type without a value. */ + if (!value_empty && value_name == NULL) + { + if (data != NULL || type != NULL) + { + reg_message(STRING_INVALID_PARAMS); + return 1; + } + } + + if (key_name[0] == '\\' && key_name[1] == '\\') + { + WINE_FIXME("Remote registry editing not implemented.\n"); + return 1; + } + + /* Split the key into hKey and lpSubKey. */ + lpSubKey = reg_split_key(&hKey, key_name); + + /* Write to registry. */ + ret = RegCreateKeyEx(hKey, lpSubKey, 0, NULL, 0, KEY_WRITE, NULL, &hkResult, NULL); + if (ret != ERROR_SUCCESS) + { + WINE_ERR("RegCreateKeyEx() failed. (%d)\n", ret); + return 1; + } + + ret = RegCloseKey(hkResult); + if (ret != ERROR_SUCCESS) + { + WINE_ERR("RegCloseKey() failed. (%d)\n", ret); + return 1; + } + + return 0; } static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty, diff --git a/programs/reg/reg.h b/programs/reg/reg.h index 80dea3e..0d1760f 100644 --- a/programs/reg/reg.h +++ b/programs/reg/reg.h @@ -23,3 +23,6 @@ #define STRING_ADD_USAGE 102 #define STRING_DELETE_USAGE 103 #define STRING_QUERY_USAGE 104 +#define STRING_INVALID_PARAMS 121 +#define STRING_INVALID_ROOTKEY 122 +#define STRING_INVALID_SUBKEY 123 -- 1.5.4.3