I apologize in advance as I have never actually programmed on a Unix box... but I am pretty sure this does the trick. I'd compile and test if I had a Unix box =/
Code:
// remember to #include headers, i haven't done that
int main (int argc, char **argv)
{
int tnout[2]; // write pipe to the child
int tnin[2]; // read pipe from the child
pid_t childp;
if (pipe (tnout) || pipe (tnin))
{
fprintf (stderr, "Not enough file handles!\n");
exit (EXIT_FAILURE);
}
childp = fork ();
if (childp == -1)
{
fprintf (stderr, "Out of memory!\n");
exit (EXIT_FAILURE);
}
else if (childp == 0)
{ // child process
// close the unused pipe-ends
close (tnout[1]);
close (tnin[0]);
// set stdin, stdout, to the open pipe ends
dup2 (tnout[0], 0);
dup2 (tnin[1], 1);
// start child program
// replace "telnet localhost 23" with whatever program
// you wish to call and whichever exec* is convienent
execl ("telnet", "telnet", "localhost", "23", (char *) 0);
// shouldn't return
fprintf (stderr, "Couldn't start telnet!\n");
exit (EXIT_FAILURE);
}
// parent process
// tnout[1] can be used to write to the child's stdin
close (tnout[0]);
// tnin[0] can be used to read from the child's stdout
close (tnin[1]);
return 0;
}