4 * Copyright 2008 Javier Merino <cibervicho@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
27 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 in fds[1] our stdin */
75 if (write(fds
[1], buf
, r
*sizeof(char)) == -1) {
76 fprintf(stderr
, "Write error to %s\n", argv
[1]);
79 r
= read(0, buf
, BUFSIZ
*sizeof(char));
87 if (waitpid(child_pid
, &child_status
, 0) != child_pid
) {
91 if (WIFEXITED(child_status
)) {
92 return (WEXITSTATUS(child_status
));
93 } else if (WIFSIGNALED(child_status
)) {
94 raise(WTERMSIG(child_status
));