How to create a pseudo-tty for reading output and writing to input
You want to call forkpty(). From the man page:
#include <pty.h> /* for openpty and forkpty */
pid_t forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp);
Link with -lutil.
The forkpty() function combines openpty(), fork(), and login_tty() to create a new process operating in a pseudo-terminal. The file descripâ tor of the master side of the pseudo-terminal is returned in amaster, and the filename of the slave in name if it is not NULL. The termp and winp parameters, if not NULL, will determine the terminal attributes and window size of the slave side of the pseudo-terminal.
Your parent process talks to the child by reading and writing from the file descriptor that forkpty stores in "amaster" - this is called the master pseudo-terminal device. The child just talks to stdin and stdout, which are connected to the slave pseudo-terminal device.
There's a package called "expect" which you should use. It uses a scripting language called tcl (pronounced tickle).