diff --git a/lib.cpp b/lib.cpp index 6ec7e96f9c373656f8e9b44917c40e7af2dfaae0..a1363fd4a094e6ce917f61c42cda59418a6b21ad 100644 --- a/lib.cpp +++ b/lib.cpp @@ -5,9 +5,35 @@ #include <cassert> #include <cstring> #include <fcntl.h> +#include <unistd.h> namespace lib { + std::pair<FILE *, FILE*> dualpopen(const char * cmd) + { + int fd_stdin[2]; + int fd_stdout[2]; + assert(pipe(fd_stdin) == 0); + assert(pipe(fd_stdout) == 0); + int r = fork(); + assert(r>=0); + if(r>0) + { + close(fd_stdin[0]); + close(fd_stdout[1]); + return {fdopen(fd_stdin[1], "w"), fdopen(fd_stdout[0], "r")}; + } + else + { + close(fd_stdin[1]); + close(fd_stdout[0]); + assert(dup2(fd_stdin[0], 0) == 0); + assert(dup2(fd_stdout[1], 1) == 1); + execl("/bin/sh", "sh", "-c", cmd, (char *) NULL); + } + exit(1); + } + vector<int> gen_arr_other_car_of_type(int n,vector <int> input) { assert(input.size() == 2*n); diff --git a/lib.h b/lib.h index 13b7a53bda16167a4d46413a0455194854a91613..ed6ed9ce5f84777e60dddd376e72f1902f3d4fb4 100644 --- a/lib.h +++ b/lib.h @@ -25,6 +25,8 @@ namespace lib vector<int> gen_arr_other_car_of_type(int n,vector <int> input); int calc_score(int n, vector<bool> out); int calc_mistakes(int n, vector<int> input, vector<bool> out); + + std::pair<FILE *, FILE*> dualpopen(const char * cmd); namespace internal {