ptg10805159
Section 19.5 ptyProgram 729
if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
err_sys("dup2 error to stdout");
if (dup2(fds, STDERR_FILENO) != STDERR_FILENO)
err_sys("dup2 error to stderr");
if (fds != STDIN_FILENO && fds != STDOUT_FILENO &&
fds != STDERR_FILENO)
close(fds);
return(0); /* child returns 0 just like fork() */
}else { /* parent */
*ptrfdm = fdm; /* return fd of master */
return(pid); /* parent returns pid of child */
}
}
Figure 19.10 Thepty_forkfunction
19.5 pty Program
The goal in writing theptyprogram is to be able to type
pty prog arg1 arg2
instead of
prog arg1 arg2
When we useptyto execute another program, that program is executed in a session of
its own, connected to a pseudo terminal.
Let’s look at the source code for theptyprogram. The first file (Figure19.11)
contains themainfunction. It calls thepty_forkfunction from the previous section.
#include "apue.h"
#include <termios.h>
#ifdef LINUX
#define OPTSTR "+d:einv"
#else
#define OPTSTR "d:einv"
#endif
static void set_noecho(int); /* at the end of this file */
void do_driver(char *); /* in the file driver.c */
void loop(int, int); /* in the file loop.c */
int
main(int argc, char *argv[])
{
int fdm, c, ignoreeof, interactive, noecho, verbose;
pid_t pid;
char *driver;
char slave_name[20];