ptg10805159
Section 19.6 Using theptyProgram 733
if (signal_intr(SIGTERM, sig_term) == SIG_ERR)
err_sys("signal_intr error for SIGTERM");
for ( ; ; ) {
if ((nread = read(ptym, buf, BUFFSIZE)) <= 0)
break; /* signal caught, error, or EOF */
if (writen(STDOUT_FILENO, buf, nread) != nread)
err_sys("writen error to stdout");
}
/*
*There are three ways to get here: sig_term() below caught the
*SIGTERM from the child, we read an EOF on the pty master (which
*means we have to signal the child to stop), or an error.
*/
if (sigcaught == 0) /* tell child if it didn’t send us the signal */
kill(child, SIGTERM);
/*
*Parent returns to caller.
*/
}
/*
*The child sends us SIGTERM when it gets EOF on the pty slave or
*when read() fails. We probably interrupted the read() of ptym.
*/
static void
sig_term(int signo)
{
sigcaught = 1; /* just set flag and return */
}
Figure 19.12Theloopfunction
Note that because we use two processes, one has to notify the other when it terminates.
We use theSIGTERMsignal for this notification.
19.6 Using the pty Program
We’ll now look at various examples with theptyprogram, seeing the need for the
command-line options.
If our shell is the Korn shell, we can execute the command
pty ksh
and get a brand-new invocation of the shell, running under a pseudo terminal.
If the filettynameis the program we showed in Figure18.16, we can run thepty
program as follows: