2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
11 * Copyright (c) 1980 Regents of the University of California.
12 * All rights reserved. The Berkeley software License Agreement
13 * specifies the terms and conditions for redistribution.
16 /* Portions Copyright(c) 1988, Sun Microsystems, Inc. */
17 /* All Rights Reserved. */
20 * script: Produce a record of a terminal session.
29 #include <sys/stropts.h>
30 #include <sys/types.h>
32 #include <sys/termios.h>
39 void doinput() __NORETURN
;
44 void done() __NORETURN
;
50 int master
; /* file descriptor for master pseudo-tty */
51 int slave
; /* file descriptor for slave pseudo-tty */
54 char *fname
= "typescript";
62 char *mptname
= "/dev/ptmx"; /* master pseudo-tty device */
67 main(int argc
, char *argv
[])
72 (void) setlocale(LC_ALL
, "");
73 #if !defined(TEXT_DOMAIN)
74 #define TEXT_DOMAIN "SYS_TEST"
76 (void) textdomain(TEXT_DOMAIN
);
78 shell
= getenv("SHELL");
82 while (argc
> 0 && argv
[0][0] == '-') {
91 gettext("usage: script [ -a ] [ typescript ]\n"));
100 if ((fscript
= fopen(fname
, aflg
? "a" : "w")) == NULL
) {
104 setbuf(fscript
, NULL
);
105 chown(fname
, ruidt
, gidt
);
107 printf(gettext("Script started, file is %s\n"), fname
);
110 (void) signal(SIGCHLD
, finish
);
117 subchild
= child
= fork();
138 (void) fclose(fscript
);
139 sigset(SIGWINCH
, sigwinch
);
141 while ((cc
= read(0, ibuf
, BUFSIZ
)) != 0) {
143 if (errno
== EINTR
) { /* SIGWINCH probably */
149 (void) write(master
, ibuf
, cc
);
159 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0)
160 (void) ioctl(master
, TIOCSWINSZ
, &ws
);
163 #include <sys/wait.h>
170 register int die
= 0;
172 while ((pid
= wait(&status
)) > 0)
189 tvec
= time((time_t *)0);
190 strftime(tbuf
, BUFSIZ
, "%c", localtime(&tvec
));
191 fprintf(fscript
, gettext("Script started on %s\n"), tbuf
);
193 cc
= read(master
, obuf
, sizeof (obuf
));
196 (void) write(1, obuf
, cc
);
197 (void) fwrite(obuf
, 1, cc
, fscript
);
206 setpgrp(); /* relinquish control terminal */
208 (void) close(master
);
209 (void) fclose(fscript
);
210 (void) dup2(slave
, 0);
211 (void) dup2(slave
, 1);
212 (void) dup2(slave
, 2);
214 execl(shell
, shell
, "-i", (char *)0);
225 sbuf
.c_iflag
&= ~(INLCR
|IGNCR
|ICRNL
|IUCLC
|IXON
);
226 sbuf
.c_oflag
&= ~OPOST
;
227 sbuf
.c_lflag
&= ~(ICANON
|ISIG
|ECHO
);
229 sbuf
.c_cc
[VTIME
] = 0;
230 (void) ioctl(0, TCSETSF
, (char *)&sbuf
);
237 (void) kill(0, SIGTERM
);
248 tvec
= time((time_t *)0);
249 strftime(tbuf
, BUFSIZ
, "%c", localtime(&tvec
));
250 fprintf(fscript
, gettext("\nscript done on %s\n"), tbuf
);
251 (void) fclose(fscript
);
252 (void) close(master
);
254 (void) ioctl(0, TCSETSW
, (char *)&b
);
255 printf(gettext("Script done, file is %s\n"), fname
);
265 if ((master
= open(mptname
, O_RDWR
)) >= 0) { /* a pseudo-tty is free */
266 (void) ioctl(0, TCGETS
, (char *)&b
);
267 (void) ioctl(0, TIOCGWINSZ
, (char *)&size
);
269 } else { /* out of pseudo-tty's */
271 fprintf(stderr
, gettext("Out of pseudo-tty's\n"));
279 char *slavename
; /* name of slave pseudo-tty */
281 grantpt(master
); /* change permissions of slave */
282 unlockpt(master
); /* unlock slave */
283 slavename
= ptsname(master
); /* get name of slave */
284 slave
= open(slavename
, O_RDWR
); /* open slave */
285 if (slave
< 0) { /* error on opening slave */
289 ioctl(slave
, I_PUSH
, "ptem"); /* push pt hw emulation module */
290 ioctl(slave
, I_PUSH
, "ldterm"); /* push line discipline */
292 (void) ioctl(slave
, TCSETSF
, (char *)&b
);
293 (void) ioctl(slave
, TIOCSWINSZ
, (char *)&size
);