2 * UCW Library -- Running of Commands
4 * (c) 2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/process.h>
20 exec_command_v(const char *cmd, va_list args)
26 while (arg = va_arg(cargs, char *))
29 char **argv = alloca(sizeof(char *) * cnt);
30 argv[0] = (char *)cmd;
33 while (arg = va_arg(cargs, char *))
39 echo_command_v(echo, sizeof(echo), cmd, args);
40 msg(L_ERROR, "Cannot execute %s: %m", echo);
45 run_command_v(const char *cmd, va_list args)
50 msg(L_ERROR, "fork() failed: %m");
54 exec_command_v(cmd, args);
58 char status_msg[EXIT_STATUS_MSG_SIZE];
59 p = waitpid(p, &stat, 0);
61 die("waitpid() failed: %m");
62 if (format_exit_status(status_msg, stat))
65 echo_command_v(echo, sizeof(echo), cmd, args);
66 msg(L_ERROR, "`%s' failed: %s", echo, status_msg);
74 echo_command_v(char *buf, int size, const char *cmd, va_list args)
76 char *limit = buf + size - 4;
78 const char *arg = cmd;
82 if (p != buf && p < limit)
86 memcpy(p, arg, limit-p);
93 while (arg = va_arg(args, char *));
98 run_command(const char *cmd, ...)
102 int e = run_command_v(cmd, args);
108 exec_command(const char *cmd, ...)
112 exec_command_v(cmd, args);
116 echo_command(char *buf, int len, const char *cmd, ...)
120 echo_command_v(buf, len, cmd, args);
129 echo_command(msg, sizeof(msg), "/bin/echo", "datel", "strakapoud", NULL);
130 msg(L_INFO, "Running <%s>", msg);
131 run_command("/bin/echo", "datel", "strakapoud", NULL);