Skip to content
Snippets Groups Projects
Commit 14f75a55 authored by Jiří Kalvoda's avatar Jiří Kalvoda
Browse files

lib::dualpopen

parent 0670787a
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -26,6 +26,8 @@ namespace lib
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
{
void format_args(char * verdict, int & verdict_len, int argc, char const* const* argv);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment