blob: 578b92963995c73d62c1fca45884583bd8b84d03 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath5699b852003-11-15 23:19:05 +00002/*
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath5699b852003-11-15 23:19:05 +00004 */
5
6#include <stdlib.h>
7#include <unistd.h>
8
9#include "libbb.h"
10
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +000011#include "unarchive.h"
12
Glenn L McGrath5699b852003-11-15 23:19:05 +000013/* transformer(), more than meets the eye */
Rob Landleydfba7412006-03-06 20:47:33 +000014int open_transformer(int src_fd, int (*transformer)(int src_fd, int dst_fd))
Glenn L McGrath5699b852003-11-15 23:19:05 +000015{
16 int fd_pipe[2];
17 int pid;
18
19 if (pipe(fd_pipe) != 0) {
20 bb_perror_msg_and_die("Can't create pipe");
21 }
22
23 pid = fork();
24 if (pid == -1) {
25 bb_perror_msg_and_die("Fork failed");
26 }
27
28 if (pid == 0) {
29 /* child process */
Glenn L McGrath20872be2003-11-18 21:31:19 +000030 close(fd_pipe[0]); /* We don't wan't to read from the parent */
Glenn L McGrath5699b852003-11-15 23:19:05 +000031 transformer(src_fd, fd_pipe[1]);
32 close(fd_pipe[1]); /* Send EOF */
Glenn L McGrath20872be2003-11-18 21:31:19 +000033 close(src_fd);
Glenn L McGrath5699b852003-11-15 23:19:05 +000034 exit(0);
35 /* notreached */
36 }
Glenn L McGrath20872be2003-11-18 21:31:19 +000037
Glenn L McGrath5699b852003-11-15 23:19:05 +000038 /* parent process */
Glenn L McGrath20872be2003-11-18 21:31:19 +000039 close(fd_pipe[1]); /* Don't want to write to the child */
Glenn L McGrath5699b852003-11-15 23:19:05 +000040
41 return(fd_pipe[0]);
42}