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 * write(STDOUT_FILENO, "hello world\n", 12);
34 * child = pipecmd(&outputfd, NULL, argv[0], "ignoredarg", NULL);
36 * err(1, "Creating child");
37 * if (read(outputfd, input, sizeof(input)) != sizeof(input))
38 * err(1, "Reading input");
39 * if (waitpid(child, &status, 0) != child)
40 * err(1, "Waiting for child");
41 * for (i = 0; i < sizeof(input); i++)
42 * printf("%c", toupper(input[i]));
46 int main(int argc, char *argv[])
48 /* Expect exactly one argument */
52 if (strcmp(argv[1], "depends") == 0) {
53 printf("ccan/noerr\n");