summaryrefslogtreecommitdiff
path: root/pipe.c
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2012-09-26 15:04:18 +0200
committerPeter Wu <lekensteyn@gmail.com>2012-09-26 15:04:18 +0200
commit2497c1392b81e093f4de2541a98746aebd449a7f (patch)
treedb87e59d4c70c677ddb1899d9161b004f6e454ac /pipe.c
downloadc-files-2497c1392b81e093f4de2541a98746aebd449a7f.tar.gz
Initial commit
Diffstat (limited to 'pipe.c')
-rw-r--r--pipe.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/pipe.c b/pipe.c
new file mode 100644
index 0000000..ae114db
--- /dev/null
+++ b/pipe.c
@@ -0,0 +1,35 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+#define F(cmd) ( (r=cmd),r<0?(perror(#cmd),exit(1),r):r )
+
+int main() {
+ int r;
+ int pA[2];
+ int f = F(open("mehlog", O_CREAT | O_APPEND | O_WRONLY, 0644));
+ pipe(pA);
+ //pipe(pB);
+ if (!F(fork())) {
+ close(f);
+ dup2(pA[1], STDOUT_FILENO);
+ close(pA[0]);
+ close(pA[1]);
+ F(execlp("cat", "cat", NULL));
+ }
+ close(pA[1]);
+ if (!F(fork())) {
+ dup2(f, STDOUT_FILENO);
+ dup2(pA[0], STDIN_FILENO);
+ close(pA[0]);
+ close(f);
+ F(execlp("rev", "rev", NULL));
+ }
+ close(pA[0]);
+ close(f);
+ wait(NULL);
+ return 0;
+}