open_transformer.c 884 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include "libbb.h"
  8. #include "unarchive.h"
  9. /* transformer(), more than meets the eye */
  10. int open_transformer(int src_fd,
  11. USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd))
  12. {
  13. int fd_pipe[2];
  14. int pid;
  15. if (pipe(fd_pipe) != 0) {
  16. bb_perror_msg_and_die("can't create pipe");
  17. }
  18. pid = fork();
  19. if (pid == -1) {
  20. bb_perror_msg_and_die("fork failed");
  21. }
  22. if (pid == 0) {
  23. /* child process */
  24. close(fd_pipe[0]); /* We don't wan't to read from the parent */
  25. // FIXME: error check?
  26. transformer(src_fd, fd_pipe[1]);
  27. close(fd_pipe[1]); /* Send EOF */
  28. close(src_fd);
  29. exit(0);
  30. /* notreached */
  31. }
  32. /* parent process */
  33. close(fd_pipe[1]); /* Don't want to write to the child */
  34. return fd_pipe[0];
  35. }