Last active
September 14, 2021 10:14
-
-
Save Hermann-SW2/209918befa911209e06c5812e438dc37 to your computer and use it in GitHub Desktop.
C script demo application, demoing pipeing input onto two pipes
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
#!/bin/bash | |
exc=/tmp/headtail.c | |
if [ $0 -nt $exc ]; then sed -n "/^\/\*\*$/,\$p" $0 | gcc -x c - -o $exc; fi | |
cat | $exc | |
exit | |
/** | |
*/ | |
#include <stdio.h> | |
#define max 1000 | |
int main(int argc, char *argv[]) | |
{ | |
char line[max+1]; | |
FILE *pip1 = popen("bash -c \"head -3\"", "w"); | |
FILE *pip2 = popen("bash -c \"tail -2\"", "w"); | |
fgets(line, max, stdin); | |
while (! feof(stdin)) | |
{ | |
fputs(line, pip1); | |
fputs(line, pip2); | |
fgets(line, max, stdin); | |
} | |
pclose(pip1); | |
pclose(pip2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.raspberrypi.org/forums/viewtopic.php?f=33&t=319473#p1913176