The Linux Programming Interface

(nextflipdebug5) #1
Pseudoterminals 1393

/* Parent: relay data between terminal and pty master */

t scriptFd = open((argc > 1)? argv[1] : "typescript",
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH);
if (scriptFd == -1)
errExit("open typescript");


y ttySetRaw(STDIN_FILENO, &ttyOrig);


u if (atexit(ttyReset) != 0)
errExit("atexit");


i for (;;) {
FD_ZERO(&inFds);
FD_SET(STDIN_FILENO, &inFds);
FD_SET(masterFd, &inFds);


o if (select(masterFd + 1, &inFds, NULL, NULL, NULL) == -1)
errExit("select");


a if (FD_ISSET(STDIN_FILENO, &inFds)) { / stdin --> pty /
numRead = read(STDIN_FILENO, buf, BUF_SIZE);
if (numRead <= 0)
exit(EXIT_SUCCESS);


if (write(masterFd, buf, numRead) != numRead)
fatal("partial/failed write (masterFd)");
}

s if (FD_ISSET(masterFd, &inFds)) { / pty --> stdout+file /
numRead = read(masterFd, buf, BUF_SIZE);
if (numRead <= 0)
exit(EXIT_SUCCESS);


if (write(STDOUT_FILENO, buf, numRead) != numRead)
fatal("partial/failed write (STDOUT_FILENO)");
if (write(scriptFd, buf, numRead) != numRead)
fatal("partial/failed write (scriptFd)");
}
}
}
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– pty/script.c
In the following shell session, we demonstrate the use of the program in Listing 64-3.
We begin by displaying the name of the pseudoterminal used by the xterm on which
the login shell is running and the process ID of the login shell. This information is
useful later in the shell session.

$ tty
/dev/pts/1
$ echo $$
7979
Free download pdf