Преглед изворни кода

libdpkg: Add new command_shell() to execute a shell instance

The function can invoke an interactive shell or a command through the
shell.
Guillem Jover пре 15 година
родитељ
комит
7f9153a39e
2 измењених фајлова са 28 додато и 0 уклоњено
  1. 26 0
      lib/dpkg/command.c
  2. 2 0
      lib/dpkg/command.h

+ 26 - 0
lib/dpkg/command.c

@@ -181,3 +181,29 @@ command_exec(struct command *cmd)
 		execvp(cmd->filename, (char * const *)cmd->argv);
 	ohshite(_("unable to execute %s (%s)"), cmd->name, cmd->filename);
 }
+
+/**
+ * Execute a shell with a possible command.
+ *
+ * @param cmd The command string to execute, if it's NULL an interactive
+ *            shell will be executed instead.
+ * @param name The description of the command to execute.
+ */
+void
+command_shell(const char *cmd, const char *name)
+{
+	const char *shell;
+	const char *mode;
+
+	shell = getenv(SHELLENV);
+	if (shell == NULL || shell[0] == '\0')
+		shell = DEFAULTSHELL;
+
+	if (cmd == NULL)
+		mode = "-i";
+	else
+		mode = "-c";
+
+	execlp(shell, shell, mode, cmd, NULL);
+	ohshite(_("unable to execute %s (%s)"), name, cmd);
+}

+ 2 - 0
lib/dpkg/command.h

@@ -48,6 +48,8 @@ void command_add_args(struct command *cmd, ...) DPKG_ATTR_SENTINEL;
 
 void command_exec(struct command *cmd) DPKG_ATTR_NORET;
 
+void command_shell(const char *cmd, const char *name);
+
 DPKG_END_DECLS
 
 #endif /* LIBDPKG_COMMAND_H */