1392 Chapter 64
Listing 64-3: A simple implementation of script(1)
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– pty/script.c
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <termios.h>
#include <sys/select.h>
#include "pty_fork.h" /* Declaration of ptyFork() */
#include "tty_functions.h" /* Declaration of ttySetRaw() */
#include "tlpi_hdr.h"
#define BUF_SIZE 256
#define MAX_SNAME 1000
struct termios ttyOrig;
static void /* Reset terminal mode on program exit */
ttyReset(void)
{
if (tcsetattr(STDIN_FILENO, TCSANOW, &ttyOrig) == -1)
errExit("tcsetattr");
}
int
main(int argc, char *argv[])
{
char slaveName[MAX_SNAME];
char *shell;
int masterFd, scriptFd;
struct winsize ws;
fd_set inFds;
char buf[BUF_SIZE];
ssize_t numRead;
pid_t childPid;
q if (tcgetattr(STDIN_FILENO, &ttyOrig) == -1)
errExit("tcgetattr");
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
errExit("ioctl-TIOCGWINSZ");
w childPid = ptyFork(&masterFd, slaveName, MAX_SNAME, &ttyOrig, &ws);
if (childPid == -1)
errExit("ptyFork");
if (childPid == 0) { /* Child: execute a shell on pty slave */
e shell = getenv("SHELL");
if (shell == NULL || *shell == '\0')
shell = "/bin/sh";
r execlp(shell, shell, (char *) NULL);
errExit("execlp"); /* If we get here, something went wrong */
}