6 * pipecmd - code to fork and run a command in a pipe.
8 * This code is a classic example of how to run a command in a child, while
9 * handling the case where the exec fails.
11 * License: CC0 (Public domain)
12 * Author: Rusty Russell <rusty@rustcorp.com.au>
15 * // Outputs HELLO WORLD
16 * #include <ccan/pipecmd/pipecmd.h>
17 * #include <ccan/err/err.h>
18 * #include <sys/types.h>
19 * #include <sys/wait.h>
23 * // Runs ourselves with an argument, upcases output.
24 * int main(int argc, char **argv)
27 * int outputfd, i, status;
31 * if (write(STDOUT_FILENO, "hello world\n", 12) != 12)
35 * child = pipecmd(NULL, &outputfd, NULL, argv[0], "ignoredarg", NULL);
37 * err(1, "Creating child");
38 * if (read(outputfd, input, sizeof(input)) != sizeof(input))
39 * err(1, "Reading input");
40 * if (waitpid(child, &status, 0) != child)
41 * err(1, "Waiting for child");
42 * for (i = 0; i < sizeof(input); i++)
43 * printf("%c", toupper(input[i]));
47 int main(int argc, char *argv[])
49 /* Expect exactly one argument */
53 if (strcmp(argv[1], "depends") == 0) {
54 printf("ccan/noerr\n");