Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 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 */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame^] | 23 | int open_transformer(int src_fd, int (*transformer)(int src_fd, int dst_fd)) |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 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 McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 39 | close(fd_pipe[0]); /* We don't wan't to read from the parent */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 40 | transformer(src_fd, fd_pipe[1]); |
| 41 | close(fd_pipe[1]); /* Send EOF */ |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 42 | close(src_fd); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 43 | exit(0); |
| 44 | /* notreached */ |
| 45 | } |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 46 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 47 | /* parent process */ |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 48 | close(fd_pipe[1]); /* Don't want to write to the child */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 49 | |
| 50 | return(fd_pipe[0]); |
| 51 | } |