Created
March 16, 2012 00:09
-
-
Save JAChapmanII/2047782 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <unistd.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| int main(int argc, char **argv) { | |
| printf("argv[1]: %s\n", argv[1]); | |
| // this is this function, not the subproc | |
| int outOfThis[2], intoThis[2]; | |
| if((pipe(outOfThis) < 0) || (pipe(intoThis) < 0)) | |
| return -2; | |
| int p = fork(); | |
| if(p < 0) | |
| return -1; | |
| // read | write | |
| if(p == 0) { | |
| // dup read end of "out" onto stdin | |
| dup2(outOfThis[0], 0); | |
| // close not needed outOfThis | |
| close(outOfThis[0]); | |
| close(outOfThis[1]); | |
| // dup write end of "in" onto stdout | |
| dup2(intoThis[1], 1); | |
| // close not needed intoThis | |
| close(intoThis[0]); | |
| close(intoThis[1]); | |
| char *args[] = { argv[1], "arg", NULL }; | |
| execv(argv[1], args); | |
| perror("oh shit"); | |
| } else { | |
| // we shouldn't read our output or write into our input | |
| close(outOfThis[0]); | |
| close(intoThis[1]); | |
| char *out = "1\n"; | |
| for(int i = 0; i < 5; ++i) { | |
| if(write(outOfThis[1], out, 3) < 3) { | |
| fprintf(stderr, "write failed in main\n"); | |
| return -4; | |
| } | |
| printf("wrote \"%s\" in main\n", out); | |
| char buf[4096] = { 0 }; | |
| if(read(intoThis[0], buf, 4096) < 1) { | |
| fprintf(stderr, "read failed in main\n"); | |
| return -5; | |
| } | |
| printf("read \"%s\" in main\n", out); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python2 | |
| import sys | |
| while not sys.stdin.closed: | |
| line = sys.stdin.readline() | |
| print line |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| while read line; do | |
| echo $line | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment