5 int run(const char *cmd, char *const argv[])
11 fprintf(stderr, "fork() failed: %s\n", strerror(error));
16 printf("Forked child %d\n", child);
23 printf("Failed to execv command %s: %s\n", cmd, strerror(error));
28 int harvest_zombies(int pid)
35 pid = waitpid(pid, &status, 0);
37 printf("pid %d: terminated with exit code %d\n", pid, status);
44 * Runs a command cmd with params argv, connects stdin and stdout to
47 * Returns the pid of the executed process
49 int run_piped(const char *cmd, char *const argv[], int *readfd, int *writefd)
51 int rfd[2], wfd[2], error, pid;
55 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
61 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
69 fprintf(stderr, "fork() failed: %s\n", strerror(error));
83 dup2(wfd[0], STDIN_FILENO);
84 dup2(rfd[1], STDOUT_FILENO);
86 /* Now we have redirected both stdin and stdout to parent process */
89 printf("Failed to execv command %s: %s\n", cmd, strerror(error));
95 * Runs a command cmd with params argv, connects stdin and stdout to
98 * Returns the pid of the executed process
100 int run_piped_stream(const char *cmd, char *const argv[],
101 FILE **readf, FILE **writef)
103 int rfd, wfd, pid, error;
105 pid = run_piped(cmd, argv, &rfd, &wfd);
108 *readf = fdopen(rfd, "r");
109 if (*readf == NULL) {
112 "Error opening file stream for fd %d: %s\n",
113 rfd, strerror(error));
119 *writef = fdopen(wfd, "w");
120 if (*writef == NULL) {
123 "Error opening file stream for fd %d: %s\n",
124 wfd, strerror(error));