3
0

open_transformer.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include "libbb.h"
  19. /* transformer(), more than meets the eye */
  20. extern int open_transformer(int src_fd, int (*transformer)(int src_fd, int dst_fd))
  21. {
  22. int fd_pipe[2];
  23. int pid;
  24. if (pipe(fd_pipe) != 0) {
  25. bb_perror_msg_and_die("Can't create pipe");
  26. }
  27. pid = fork();
  28. if (pid == -1) {
  29. bb_perror_msg_and_die("Fork failed");
  30. }
  31. if (pid == 0) {
  32. /* child process */
  33. close(fd_pipe[0]); /* We don't wan't to read from the parent */
  34. transformer(src_fd, fd_pipe[1]);
  35. close(fd_pipe[1]); /* Send EOF */
  36. close(src_fd);
  37. exit(0);
  38. /* notreached */
  39. }
  40. /* parent process */
  41. close(fd_pipe[1]); /* Don't want to write to the child */
  42. return(fd_pipe[0]);
  43. }