-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec-pipe.c
More file actions
65 lines (51 loc) · 1.09 KB
/
Copy pathexec-pipe.c
File metadata and controls
65 lines (51 loc) · 1.09 KB
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
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[], char *envp[]) {
int ififo[2];
if ( pipe(ififo) < 0 ) {
perror("pipe");
exit(EXIT_FAILURE);
}
int ofifo[2];
if ( pipe(ofifo) < 0 ) {
perror("pipe");
exit(EXIT_FAILURE);
}
int pid = fork();
if ( pid < 0 ) {
perror("fork");
exit(EXIT_FAILURE);
}
if ( pid == 0 ) {
if ( dup2(ififo[0], 0) < 0 ) {
perror("dup2");
exit(EXIT_FAILURE);
}
if ( dup2(ofifo[1], 1) < 0 ) {
perror("dup2");
exit(EXIT_FAILURE);
}
close(ififo[1]);
close(ofifo[0]);
char *args[] = {"bc", NULL};
execve("/usr/bin/bc", args, envp);
perror("exec");
exit(EXIT_FAILURE);
}
char wstr[100];
char rstr[100];
for ( int i = 0 ; i < 100 ; i++ ) {
sprintf(bcstr, "6 * %d\n", i);
int slen = strlen(bcstr);
if ( write(ififo[1], bcstr, slen) < slen ) {
perror("write");
}
}
close(ififo[1]);
wait(NULL);
printf("The child is dead.\n");
}