Skip to content

Instantly share code, notes, and snippets.

@d0k
Created June 24, 2010 14:32
Show Gist options
  • Save d0k/451519 to your computer and use it in GitHub Desktop.
Save d0k/451519 to your computer and use it in GitHub Desktop.
tee stdin into a process and stdout
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
int main(int argc, char *argv[]) {
if (argc < 2)
return 1;
int p[2];
pipe(p);
pid_t child = fork();
if (child == 0) {
close(p[1]);
dup2(p[0], STDIN_FILENO);
execvp(argv[1], argv + 1);
_exit(127);
}
char buf[BUFSIZ];
ssize_t bytes;
while ((bytes = read(STDIN_FILENO, buf, BUFSIZ)) != 0) {
write(STDOUT_FILENO, buf, bytes);
write(p[1], buf, bytes);
}
close(p[1]);
int status;
waitpid(child, &status, 0);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment