blob: 93f01be6f21e8118a54f3e8abeef9d7f643a7f0f [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
Glenn L McGrath5699b852003-11-15 23:19:05 +00006#include "libbb.h"
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +00007#include "unarchive.h"
8
Glenn L McGrath5699b852003-11-15 23:19:05 +00009/* transformer(), more than meets the eye */
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000010int open_transformer(int src_fd,
11 USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd))
Glenn L McGrath5699b852003-11-15 23:19:05 +000012{
13 int fd_pipe[2];
14 int pid;
15
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000016 xpipe(fd_pipe);
Glenn L McGrath5699b852003-11-15 23:19:05 +000017
18 pid = fork();
19 if (pid == -1) {
Denis Vlasenkoea620772006-10-14 02:23:43 +000020 bb_perror_msg_and_die("fork failed");
Glenn L McGrath5699b852003-11-15 23:19:05 +000021 }
22
23 if (pid == 0) {
24 /* child process */
Denis Vlasenkoea620772006-10-14 02:23:43 +000025 close(fd_pipe[0]); /* We don't wan't to read from the parent */
26 // FIXME: error check?
27 transformer(src_fd, fd_pipe[1]);
Denis Vlasenko7e0fbf92007-09-04 19:33:22 +000028 if (ENABLE_FEATURE_CLEAN_UP) {
29 close(fd_pipe[1]); /* Send EOF */
30 close(src_fd);
31 }
Denis Vlasenkoea620772006-10-14 02:23:43 +000032 exit(0);
33 /* notreached */
Glenn L McGrath5699b852003-11-15 23:19:05 +000034 }
Glenn L McGrath20872be2003-11-18 21:31:19 +000035
Glenn L McGrath5699b852003-11-15 23:19:05 +000036 /* parent process */
Glenn L McGrath20872be2003-11-18 21:31:19 +000037 close(fd_pipe[1]); /* Don't want to write to the child */
Glenn L McGrath5699b852003-11-15 23:19:05 +000038
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000039 return fd_pipe[0];
Glenn L McGrath5699b852003-11-15 23:19:05 +000040}