[PATCH v3 8/8] reg.exe: Clean up reg_delete

Jonathan Vollebregt jnvsor at gmail.com
Sat Sep 6 11:05:23 CDT 2014


---
 programs/reg/reg.c  | 102 ++++++++++++++++++++++++++--------------------------
 programs/reg/reg.h  |   1 +
 programs/reg/reg.rc |   1 +
 3 files changed, 54 insertions(+), 50 deletions(-)

diff --git a/programs/reg/reg.c b/programs/reg/reg.c
index 3bb09d3..284fc13 100644
--- a/programs/reg/reg.c
+++ b/programs/reg/reg.c
@@ -601,21 +601,16 @@ static int reg_add( const WCHAR *key_name,  const WCHAR *value_name,    const BO
     return 0;
 }
 
-static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
-    BOOL value_all, BOOL force)
+static int reg_delete(const WCHAR *key_name,    const WCHAR *value_name,  const BOOL value_empty,
+                      const BOOL value_all,     const BOOL force)
 {
-    HKEY subkey = path_get_key(key_name);
+    HKEY key = path_get_key(key_name);
 
-    if (!subkey)
+    if (!key)
         return 1;
 
-    if (value_name && value_empty)
-    {
-        reg_message(STRING_INVALID_CMDLINE);
-        return 1;
-    }
-
-    if (value_empty && value_all)
+    /* Mutually exclusive options */
+    if ((!!value_name + !!value_empty + !!value_all) > 1)
     {
         reg_message(STRING_INVALID_CMDLINE);
         return 1;
@@ -626,70 +621,77 @@ static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
         /* FIXME:  Prompt for delete */
     }
 
-    /* Delete subtree only if no /v* option is given */
-    if (!value_name && !value_empty && !value_all)
+    if (value_empty || (value_name && !value_name[0]))
     {
-        HKEY root = path_get_rootkey(key_name);
-
-        if (RegDeleteTreeW(root, key_name) != ERROR_SUCCESS)
+        RegDeleteValueW(key, NULL);
+    }
+    else if (value_name)
+    {
+        if (RegDeleteValueW(key, value_name) != ERROR_SUCCESS)
         {
+            RegCloseKey(key);
             reg_message(STRING_CANNOT_FIND);
             return 1;
         }
-        reg_message(STRING_SUCCESS);
-        return 0;
     }
-
-    if (value_all)
+    else if (value_all)
     {
-        LPWSTR szValue;
-        DWORD maxValue;
-        DWORD count;
-        LONG rc;
+        WCHAR enum_v_name[MAX_VALUE_NAME];
+        DWORD rc, count, max_size, i = 0;
 
-        rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-            &maxValue, NULL, NULL, NULL);
+        rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
+                &count, NULL, NULL, NULL, NULL);
         if (rc != ERROR_SUCCESS)
         {
-            /* FIXME: failure */
-            RegCloseKey(subkey);
+            RegCloseKey(key);
+            reg_message(STRING_CANNOT_FIND);
             return 1;
         }
-        maxValue++;
-        szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
 
-        while (1)
+        while (i < count)
         {
-            count = maxValue;
-            rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
-            if (rc == ERROR_SUCCESS)
+            max_size = MAX_VALUE_NAME;
+
+            rc = RegEnumValueW(key, i, enum_v_name, &max_size,
+                NULL, NULL, NULL, NULL);
+
+            if (rc != ERROR_SUCCESS)
             {
-                rc = RegDeleteValueW(subkey, szValue);
-                if (rc != ERROR_SUCCESS)
-                    break;
+                i++;
+                continue;
             }
-            else break;
-        }
-        if (rc != ERROR_SUCCESS)
-        {
-            /* FIXME  delete failed */
+            else if (RegDeleteValueW(key, enum_v_name) != ERROR_SUCCESS)
+            {
+                i++;
+                continue;
+            }
+
+            count--;
         }
+
+        RegDeleteValueW(key, NULL);
     }
-    else if (value_name)
+    /* Delete subtree only if no /v* option is given */
+    else
     {
-        if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
+        HKEY root = path_get_rootkey(key_name);
+
+        if (key == root)
         {
-            RegCloseKey(subkey);
+            RegCloseKey(key);
+            reg_message(STRING_NO_DEL_ROOT);
+            return 1;
+        }
+
+        if (RegDeleteTreeW(root, strchrW(key_name, '\\') + 1) != ERROR_SUCCESS)
+        {
+            RegCloseKey(key);
             reg_message(STRING_CANNOT_FIND);
             return 1;
         }
     }
-    else if (value_empty)
-    {
-        RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
-    }
 
-    RegCloseKey(subkey);
+    RegCloseKey(key);
     reg_message(STRING_SUCCESS);
     return 0;
 }
diff --git a/programs/reg/reg.h b/programs/reg/reg.h
index e593695..313a641 100644
--- a/programs/reg/reg.h
+++ b/programs/reg/reg.h
@@ -34,3 +34,4 @@
 #define STRING_UNHANDLED_TYPE   111
 #define STRING_NAN              112
 #define STRING_BINARY_INCMPLT   113
+#define STRING_NO_DEL_ROOT      114
diff --git a/programs/reg/reg.rc b/programs/reg/reg.rc
index 8b4b407..13e854b 100644
--- a/programs/reg/reg.rc
+++ b/programs/reg/reg.rc
@@ -39,4 +39,5 @@ STRINGTABLE
     STRING_UNHANDLED_TYPE, "Error: Unhandled Type"
     STRING_NAN, "Error: /d for this type requires number\n"
     STRING_BINARY_INCMPLT, "Error: REG_BINARY input must have even number of nibbles\n"
+    STRING_NO_DEL_ROOT, "Error: Cannot delete root key\n"
 }
-- 
2.1.0




More information about the wine-patches mailing list