+ char *cmd;
+ int len, i, j;
+ char *out;
+
+ if (argc < 1) {
+ reply_with_error ("sh: expecting a command to run");
+ return NULL;
+ }
+
+ /* guestfish splits the parameter(s) into a list of strings,
+ * and we have to reassemble them here. Not ideal. XXX
+ */
+ for (i = len = 0; i < argc; ++i)
+ len += strlen (argv[i]) + 1;
+ cmd = malloc (len);
+ if (!cmd) {
+ reply_with_perror ("malloc");
+ return NULL;
+ }
+ for (i = j = 0; i < argc; ++i) {
+ len = strlen (argv[i]);
+ memcpy (&cmd[j], argv[i], len);
+ j += len;
+ cmd[j] = ' ';
+ j++;
+ }
+ cmd[j-1] = '\0';
+
+ command (&out, NULL, "/bin/sh", "-c", cmd, NULL);
+ free (cmd);
+ return out;
+}
+
+/* Print the environment that commands get (by running external printenv). */
+static char *
+debug_env (const char *subcmd, int argc, char *const *const argv)
+{
+ int r;
+ char *out, *err;
+
+ r = command (&out, &err, "printenv", NULL);
+ if (r == -1) {
+ reply_with_error ("printenv: %s", err);
+ free (out);
+ free (err);
+ return NULL;
+ }
+
+ free (err);
+
+ return out;
+}
+
+/* List files in the appliance. */
+static char *
+debug_ls (const char *subcmd, int argc, char *const *const argv)
+{
+ int len = count_strings (argv);
+ const char *cargv[len+3];
+ int i;
+
+ cargv[0] = "ls";
+ cargv[1] = "-a";
+ for (i = 0; i < len; ++i)
+ cargv[2+i] = argv[i];
+ cargv[2+len] = NULL;
+
+ int r;
+ char *out, *err;
+
+ r = commandv (&out, &err, (void *) cargv);
+ if (r == -1) {
+ reply_with_error ("ls: %s", err);
+ free (out);
+ free (err);
+ return NULL;
+ }
+
+ free (err);
+
+ return out;
+}
+
+/* List files in the appliance. */
+static char *
+debug_ll (const char *subcmd, int argc, char *const *const argv)
+{
+ int len = count_strings (argv);
+ const char *cargv[len+3];
+ int i;
+
+ cargv[0] = "ls";
+ cargv[1] = "-la";
+ for (i = 0; i < len; ++i)
+ cargv[2+i] = argv[i];
+ cargv[2+len] = NULL;
+