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