summaryrefslogtreecommitdiff
path: root/ifne.c
blob: 9af19fa0f905802af64b90b23fc1dd9963864a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

/* 
 *
 * Copyright 2008 Javier Merino <cibervicho@gmail.com>
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int main(int argc, char **argv) {
	ssize_t r;
	int fds[2];
	int child_status;
	pid_t child_pid;
	char buf[BUFSIZ];

	if (argc < 2) {
		/* Noop */
		return EXIT_SUCCESS;
	}

	r = read(0, buf, BUFSIZ*sizeof(char));

	if (r == 0)
		return EXIT_SUCCESS;
	else if (r == -1) {
		perror("read");
		return EXIT_FAILURE;
	}

	if (pipe(fds)) {
		perror("pipe");
		return EXIT_FAILURE;
	}

	child_pid = fork();
	if (!child_pid) {
		/* child process: rebind stdin and exec the subcommand */
		close(fds[1]);
		if (dup2(fds[0], 0)) {
			perror("dup2");
			return EXIT_FAILURE;
                }

		execvp(argv[1], &argv[1]);
		perror(argv[1]);
		close(fds[0]);
		return EXIT_FAILURE;
	} else if (child_pid == -1) {
		perror("fork");
		return EXIT_FAILURE;
	}

	/* Parent: write in fds[1] our stdin */
	close(fds[0]);

	do {
		if (write(fds[1], buf, r*sizeof(char)) == -1) {
			fprintf(stderr, "Write error to %s\n", argv[1]);
			exit(EXIT_FAILURE);
		}
		r = read(0, buf, BUFSIZ*sizeof(char));
	} while (r > 0);
	if (r == -1) {
		perror("read");
		exit(EXIT_FAILURE);
	}
	
	close(fds[1]);
	if (waitpid(child_pid, &child_status, 0) != child_pid) {
		perror("waitpid");
		return EXIT_FAILURE;
	}
	if (WIFEXITED(child_status)) {
		return (WEXITSTATUS(child_status));
	} else if (WIFSIGNALED(child_status)) {
		raise(WTERMSIG(child_status));
		return EXIT_FAILURE;
	}

	return EXIT_FAILURE;
}