Hello,
I want to write a program A, that starts another program B and takes over its STDIN and STDOUT, so that everything B reads A has to put in to the STDIN and everything B writes, A gets.
I am aware of all the other ways to do IPC, but I want to manipulate B randomly on bit-level. So I want to keep the part of the source code for IPC as small and simple as possible.
I managed to take over the STDOUT of B, but I cant take over the STDIN. The source code is at the end. If I put an integer into the pipe, cin >> buff does not block, but seems to be skipped. If I put a char-Array into the pipe, cin blocks forever. To take read() or scanf() does not help neither.
Does anyone has a working code example or knows what I'm doing wrong?
thank you
Max
The main parts of the source code in A are:
Code:
int pipefd_stdin[2];
...
if(vfork() == 0){
...
dup2(pipefd_stdin[READ_END], STDIN_FILENO);
...
}else{
if(write(pipefd_stdin[WRITE_END], &buff, sizeof(buff)) < 0){
std::cout << "ERROR!\n";
}
}
Programm B can be given completly and is:
Code:
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <iostream>
int main()
{
char z[4];
int i = 1000;
//std::cout << "Program B started\n";
std::cerr << "TEST " << i << "\n";
i = 90;
std::cin >> i;
std::cerr << "TEST2 " << i << "\n";
//90, if an int was given, else unreachable
}