blob: fb149fc0b18b3fdb3b94b22b0dca28e59cab9f03 [file] [log] [blame]
Glenn L McGrath5699b852003-11-15 23:19:05 +00001/*
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 */
23extern 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 */
Glenn L McGrath20872be2003-11-18 21:31:19 +000039 close(fd_pipe[0]); /* We don't wan't to read from the parent */
Glenn L McGrath5699b852003-11-15 23:19:05 +000040 transformer(src_fd, fd_pipe[1]);
41 close(fd_pipe[1]); /* Send EOF */
Glenn L McGrath20872be2003-11-18 21:31:19 +000042 close(src_fd);
Glenn L McGrath5699b852003-11-15 23:19:05 +000043 exit(0);
44 /* notreached */
45 }
Glenn L McGrath20872be2003-11-18 21:31:19 +000046
Glenn L McGrath5699b852003-11-15 23:19:05 +000047 /* parent process */
Glenn L McGrath20872be2003-11-18 21:31:19 +000048 close(fd_pipe[1]); /* Don't want to write to the child */
Glenn L McGrath5699b852003-11-15 23:19:05 +000049
50 return(fd_pipe[0]);
51}