enable scrub on Debian
[libguestfs.git] / hivex / hivexsh.c
index 6f33f41..332b773 100644 (file)
@@ -58,6 +58,8 @@
 #include "hivex.h"
 #include "byte_conversions.h"
 
+#define HIVEX_MAX_VALUES         1000
+
 static int quit = 0;
 static int is_tty;
 static hive_h *h = NULL;
@@ -76,9 +78,11 @@ static char *rl_gets (const char *prompt_string);
 static void sort_strings (char **strings, int len);
 static int get_xdigit (char c);
 static int dispatch (char *cmd, char *args);
+static int cmd_add (char *name);
 static int cmd_cd (char *path);
 static int cmd_close (char *path);
 static int cmd_commit (char *path);
+static int cmd_del (char *args);
 static int cmd_help (char *args);
 static int cmd_load (char *hivefile);
 static int cmd_ls (char *args);
@@ -180,7 +184,6 @@ main (int argc, char *argv[])
 
     char *cmd = buf;
     char *args;
-    size_t i = 0;
 
     if (buf[len] == '\0') {
       /* This is mostly safe.  Although the cmd_* functions do sometimes
@@ -410,12 +413,16 @@ dispatch (char *cmd, char *args)
     return -1;
   }
 
-  if (STRCASEEQ (cmd, "cd"))
+  if (STRCASEEQ (cmd, "add"))
+    return cmd_add (args);
+  else if (STRCASEEQ (cmd, "cd"))
     return cmd_cd (args);
   else if (STRCASEEQ (cmd, "close") || STRCASEEQ (cmd, "unload"))
     return cmd_close (args);
   else if (STRCASEEQ (cmd, "commit"))
     return cmd_commit (args);
+  else if (STRCASEEQ (cmd, "del"))
+    return cmd_del (args);
   else if (STRCASEEQ (cmd, "ls"))
     return cmd_ls (args);
   else if (STRCASEEQ (cmd, "lsval"))
@@ -556,10 +563,14 @@ cmd_cd (char *path)
       continue;
     }
 
+    errno = 0;
     new_cwd = hivex_node_get_child (h, new_cwd, elem);
     if (new_cwd == 0) {
-      fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
-               elem);
+      if (errno)
+        perror ("hivexsh: cd");
+      else
+        fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
+                 elem);
       return -1;
     }
   }
@@ -777,7 +788,7 @@ cmd_lsval (char *key)
       case hive_t_dword:
       case hive_t_dword_be: {
         int32_t j = hivex_value_dword (h, values[i]);
-        printf ("dword:%08" PRIx32 "\"", j);
+        printf ("dword:%08" PRIx32, j);
         break;
       }
 
@@ -789,7 +800,8 @@ cmd_lsval (char *key)
       case hive_t_full_resource_description:
       case hive_t_resource_requirements_list:
       default: {
-        char *data = hivex_value_value (h, values[i], &t, &len);
+        unsigned char *data =
+          (unsigned char *) hivex_value_value (h, values[i], &t, &len);
         if (!data)
           goto error;
 
@@ -830,7 +842,7 @@ cmd_setval (char *nrvals_str)
              "setval", "nrvals", "xstrtol", xerr);
     return -1;
   }
-  if (nrvals < 0 || nrvals > 1000) {
+  if (nrvals < 0 || nrvals > HIVEX_MAX_VALUES) {
     fprintf (stderr, _("%s: %s: integer out of range\n"),
              "setval", "nrvals");
     return -1;
@@ -848,7 +860,6 @@ cmd_setval (char *nrvals_str)
   /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
    * explained in the man page.
    */
-  int prompt = isatty (0) ? 2 : 0;
   int i, j;
   for (i = 0; i < nrvals; ++i) {
     /* Read key. */
@@ -902,7 +913,7 @@ cmd_setval (char *nrvals_str)
     }
     else if (STRPREFIX (buf, "expandstring:")) {
       buf += 13;
-      values[i].t = hive_t_string;
+      values[i].t = hive_t_expand_string;
       int nr_chars = strlen (buf);
       values[i].len = 2 * (nr_chars + 1);
       values[i].value = malloc (values[i].len);
@@ -1044,3 +1055,40 @@ cmd_setval (char *nrvals_str)
 
   return ret;
 }
+
+static int
+cmd_del (char *args)
+{
+  if (STRNEQ (args, "")) {
+    fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
+             "del");
+    return -1;
+  }
+
+  if (cwd == hivex_root (h)) {
+    fprintf (stderr, _("hivexsh: del: the root node cannot be deleted\n"));
+    return -1;
+  }
+
+  hive_node_h new_cwd = hivex_node_parent (h, cwd);
+
+  if (hivex_node_delete_child (h, cwd) == -1) {
+    perror ("hivexsh: del");
+    return -1;
+  }
+
+  cwd = new_cwd;
+  set_prompt_string ();
+  return 0;
+}
+
+static int
+cmd_add (char *name)
+{
+  hive_node_h node = hivex_node_add_child (h, cwd, name);
+  if (node == 0) {
+    perror ("hivexsh: add");
+    return -1;
+  }
+  return 0;
+}