egor duda writes:
[oweals/busybox.git] / archival / libunarchive / open_transformer.c
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
17 #include <stdlib.h>
18 #include <unistd.h>
19
20 #include "libbb.h"
21
22 /* transformer(), more than meets the eye */
23 extern int open_transformer(int src_fd, int (*transformer)(int src_fd, int dst_fd))
24 {
25         int fd_pipe[2];
26         int pid;
27
28         if (pipe(fd_pipe) != 0) {
29                 bb_perror_msg_and_die("Can't create pipe");
30         }
31
32         pid = fork();
33         if (pid == -1) {
34                 bb_perror_msg_and_die("Fork failed");
35         }
36
37         if (pid == 0) {
38                 /* child process */
39             close(fd_pipe[0]); /* We don't wan't to read from the parent */
40             transformer(src_fd, fd_pipe[1]);
41             close(fd_pipe[1]); /* Send EOF */
42                 close(src_fd);
43             exit(0);
44             /* notreached */
45         }
46
47         /* parent process */
48         close(fd_pipe[1]); /* Don't want to write to the child */
49
50         return(fd_pipe[0]);
51 }