parent
481da73b16
commit
025898f437
3 changed files with 54 additions and 23 deletions
@ -0,0 +1,9 @@ |
|||||||
|
#ifndef _POPEN2 |
||||||
|
#define _POPEN2 |
||||||
|
|
||||||
|
#include <cstdio> |
||||||
|
|
||||||
|
// Like popen(), but returns two FILE*: child's stdin and stdout, respectively.
|
||||||
|
std::pair<FILE *, FILE *> popen2(const char *command); |
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,43 @@ |
|||||||
|
#ifndef _POPEN2 |
||||||
|
#define _POPEN2 |
||||||
|
|
||||||
|
#include <popen2.hpp> |
||||||
|
|
||||||
|
#include <unistd.h> |
||||||
|
|
||||||
|
#include <cstdlib> |
||||||
|
#include <cstdio> |
||||||
|
#include <cstring> |
||||||
|
#include <utility> |
||||||
|
|
||||||
|
|
||||||
|
// Like popen(), but returns two FILE*: child's stdin and stdout, respectively.
|
||||||
|
std::pair<FILE *, FILE *> popen2(const char *command) { |
||||||
|
// pipes[0]: parent writes, child reads (child's stdin)
|
||||||
|
// pipes[1]: child writes, parent reads (child's stdout)
|
||||||
|
int pipes[2][2]; |
||||||
|
|
||||||
|
pipe(pipes[0]); |
||||||
|
pipe(pipes[1]); |
||||||
|
|
||||||
|
if (fork() > 0) { |
||||||
|
// parent
|
||||||
|
close(pipes[0][0]); |
||||||
|
close(pipes[1][1]); |
||||||
|
|
||||||
|
return {fdopen(pipes[0][1], "w"), fdopen(pipes[1][0], "r")}; |
||||||
|
} else { |
||||||
|
// child
|
||||||
|
close(pipes[0][1]); |
||||||
|
close(pipes[1][0]); |
||||||
|
|
||||||
|
dup2(pipes[0][0], STDIN_FILENO); |
||||||
|
dup2(pipes[1][1], STDOUT_FILENO); |
||||||
|
|
||||||
|
execl("/bin/sh", "/bin/sh", "-c", command, NULL); |
||||||
|
|
||||||
|
exit(1); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#endif |
Loading…
Reference in new issue