]> git.ozlabs.org Git - ccan/blob - ccan/pipecmd/_info
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / pipecmd / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * pipecmd - code to fork and run a command in a pipe.
7  *
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.
10  *
11  * License: CC0 (Public domain)
12  * Author: Rusty Russell <rusty@rustcorp.com.au>
13  *
14  * Example:
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>
20  * #include <unistd.h>
21  * #include <ctype.h>
22  *
23  * // Runs ourselves with an argument, upcases output.
24  * int main(int argc, char **argv)
25  * {
26  *      pid_t child;
27  *      int outputfd, i, status;
28  *      char input[12];
29  *
30  *      if (argc == 2) {
31  *              if (write(STDOUT_FILENO, "hello world\n", 12) != 12)
32  *                      exit(1);
33  *              exit(0);
34  *      }
35  *      child = pipecmd(NULL, &outputfd, NULL, argv[0], "ignoredarg", NULL);
36  *      if (child < 0)
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]));
44  *      exit(0);
45  * }
46  */
47 int main(int argc, char *argv[])
48 {
49         /* Expect exactly one argument */
50         if (argc != 2)
51                 return 1;
52
53         if (strcmp(argv[1], "depends") == 0) {
54                 printf("ccan/closefrom\n");
55                 printf("ccan/noerr\n");
56                 return 0;
57         }
58
59         return 1;
60 }