fork.c 759 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <reproc/run.h>
  5. #include "assert.h"
  6. int main(void)
  7. {
  8. reproc_t *process = reproc_new();
  9. const char *MESSAGE = "reproc stands for REdirected PROCess!";
  10. char *output = NULL;
  11. reproc_sink sink = reproc_sink_string(&output);
  12. int r = -1;
  13. r = reproc_start(process, NULL, (reproc_options){ .fork = true });
  14. if (r == 0) {
  15. printf("%s", MESSAGE);
  16. fclose(stdout); // `_exit` doesn't flush stdout.
  17. _exit(EXIT_SUCCESS);
  18. }
  19. ASSERT_OK(r);
  20. r = reproc_drain(process, sink, sink);
  21. ASSERT_OK(r);
  22. ASSERT(output != NULL);
  23. ASSERT_EQ_STR(output, MESSAGE);
  24. r = reproc_wait(process, REPROC_INFINITE);
  25. ASSERT_OK(r);
  26. reproc_destroy(process);
  27. reproc_free(output);
  28. }