3 * Copyright 2008 Javier Merino <cibervicho@gmail.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 * Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
26 int main(int argc
, char **argv
) {
39 r
= read(0, buf
, BUFSIZ
*sizeof(char));
55 /* child process: rebind stdin and exec the subcommand */
57 if (dup2(fds
[0], 0)) {
62 execvp(argv
[1], &argv
[1]);
66 } else if (child_pid
== -1) {
71 /* Parent: write stdin to fds[1] */
73 outf
= fdopen(fds
[1], "w");
79 if (fwrite(buf
, r
*sizeof(char), 1, outf
) < 1) {
80 fprintf(stderr
, "Write error to %s\n", argv
[1]);
83 r
= read(0, buf
, BUFSIZ
*sizeof(char));
91 if (waitpid(child_pid
, &child_status
, 0) != child_pid
) {
95 if (WIFEXITED(child_status
)) {
96 return (WEXITSTATUS(child_status
));
97 } else if (WIFSIGNALED(child_status
)) {
98 raise(WTERMSIG(child_status
));