Created
August 7, 2012 14:49
-
-
Save adambarta/3285962 to your computer and use it in GitHub Desktop.
FAN - pipe standard input to multiple command standard inputs
This file contains 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 <stdio.h> | |
#define BUF 4096 | |
static volatile int run = 0; | |
int main(char argc, char *argv[]) | |
{ | |
unsigned char buf[BUF]; | |
FILE *fn[argc - 1]; | |
int i, rb, wb; | |
if (argc < 2){ | |
fprintf(stderr, "usage %s [\"cmd\"] [\"cmd\"] ... [\"cmd\"]\n\tCopy standard input to multiple standard inputs of other commands\n\te.g echo \"abc\" | fan \"tr a z\" \"tr a y\"\n", argv[0]); | |
return 0; | |
} | |
for (i=0; i<argc-1; i++){ | |
fn[i] = popen(argv[i+1], "w"); | |
if (fn[i] == NULL){ | |
fprintf(stderr, "err: popen (%s)\n", argv[i+1]); | |
goto unroll; | |
} | |
} | |
run = 1; | |
while (run){ | |
rb = fread(buf, sizeof(unsigned char), BUF, stdin); | |
if (rb == 0){ | |
run = 0; | |
break; | |
} | |
for (i=0; i<argc-1; i++){ | |
wb = fwrite(buf, sizeof(unsigned char), rb, fn[i]); | |
if (wb == 0){ | |
run = 0; | |
break; | |
} | |
} | |
} | |
i = argc-1; | |
unroll: | |
for (;i>0;i--){ | |
if (pclose(fn[i-1]) < 0){ | |
fprintf(stderr, "err: pclose\n"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example use
echo "abc" | fan "tr a z" "tr b y" "tr a Z"
produces
Zbc
ayc
zbc