/* * Copyright (c) 1980 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* 1999-02-22 Arkadiusz Mi¶kiewicz * - added Native Language Support */ /* * script */ #include #include #include #include #include #include #include #include #include #include #include #include "nls.h" #ifdef __linux__ #include #include #endif #include "../defines.h" #ifdef HAVE_openpty #include #endif void finish(int); void done(void); void fail(void); void fixtty(void); void getmaster(void); void getslave(void); void doinput(void); void dooutput(void); void doshell(void); char *shell; FILE *fscript; int master; int slave; int child; int subchild; char *fname; struct termios tt; struct winsize win; int lb; int l; #ifndef HAVE_openpty char line[] = "/dev/ptyXX"; #endif int aflg; int main(int argc, char **argv) { extern int optind; int ch; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); while ((ch = getopt(argc, argv, "a")) != EOF) switch((char)ch) { case 'a': aflg++; break; case '?': default: fprintf(stderr, _("usage: script [-a] [file]\n")); exit(1); } argc -= optind; argv += optind; if (argc > 0) fname = argv[0]; else fname = "typescript"; if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) { perror(fname); fail(); } shell = getenv("SHELL"); if (shell == NULL) shell = _PATH_BSHELL; getmaster(); printf(_("Script started, file is %s\n"), fname); fixtty(); (void) signal(SIGCHLD, finish); child = fork(); if (child < 0) { perror("fork"); fail(); } if (child == 0) { subchild = child = fork(); if (child < 0) { perror("fork"); fail(); } if (child) dooutput(); else doshell(); } doinput(); return 0; } void doinput() { register int cc; char ibuf[BUFSIZ]; (void) fclose(fscript); #ifdef HAVE_openpty (void) close(slave); #endif while ((cc = read(0, ibuf, BUFSIZ)) > 0) (void) write(master, ibuf, cc); done(); } #include void finish(int dummy) { union wait status; register int pid; register int die = 0; while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0) if (pid == child) die = 1; if (die) done(); } void dooutput() { register int cc; time_t tvec; char obuf[BUFSIZ]; (void) close(0); #ifdef HAVE_openpty (void) close(slave); #endif tvec = time((time_t *)NULL); fprintf(fscript, _("Script started on %s"), ctime(&tvec)); for (;;) { cc = read(master, obuf, sizeof (obuf)); if (cc <= 0) break; (void) write(1, obuf, cc); (void) fwrite(obuf, 1, cc, fscript); } done(); } void doshell() { /*** int t; t = open(_PATH_TTY, O_RDWR); if (t >= 0) { (void) ioctl(t, TIOCNOTTY, (char *)0); (void) close(t); } ***/ getslave(); (void) close(master); (void) fclose(fscript); (void) dup2(slave, 0); (void) dup2(slave, 1); (void) dup2(slave, 2); (void) close(slave); #ifdef __linux__ execl(shell, strrchr(shell, '/') + 1, "-i", 0); #else execl(shell, "sh", "-i", 0); #endif perror(shell); fail(); } void fixtty() { struct termios rtt; rtt = tt; cfmakeraw(&rtt); rtt.c_lflag &= ~ECHO; (void) tcsetattr(0, TCSAFLUSH, &rtt); } void fail() { (void) kill(0, SIGTERM); done(); } void done() { time_t tvec; if (subchild) { tvec = time((time_t *)NULL); fprintf(fscript,_("\nScript done on %s"), ctime(&tvec)); (void) fclose(fscript); (void) close(master); } else { (void) tcsetattr(0, TCSAFLUSH, &tt); printf(_("Script done, file is %s\n"), fname); } exit(0); } void getmaster() { #ifdef HAVE_openpty (void) tcgetattr(0, &tt); (void) ioctl(0, TIOCGWINSZ, (char *)&win); if (openpty(&master, &slave, NULL, &tt, &win) < 0) { fprintf(stderr, _("openpty failed\n")); fail(); } #else char *pty, *bank, *cp; struct stat stb; pty = &line[strlen("/dev/ptyp")]; for (bank = "pqrs"; *bank; bank++) { line[strlen("/dev/pty")] = *bank; *pty = '0'; if (stat(line, &stb) < 0) break; for (cp = "0123456789abcdef"; *cp; cp++) { *pty = *cp; master = open(line, O_RDWR); if (master >= 0) { char *tp = &line[strlen("/dev/")]; int ok; /* verify slave side is usable */ *tp = 't'; ok = access(line, R_OK|W_OK) == 0; *tp = 'p'; if (ok) { (void) tcgetattr(0, &tt); (void) ioctl(0, TIOCGWINSZ, (char *)&win); return; } (void) close(master); } } } fprintf(stderr, _("Out of pty's\n")); fail(); #endif /* not HAVE_openpty */ } void getslave() { #ifndef HAVE_openpty line[strlen("/dev/")] = 't'; slave = open(line, O_RDWR); if (slave < 0) { perror(line); fail(); } (void) tcsetattr(slave, TCSAFLUSH, &tt); (void) ioctl(slave, TIOCSWINSZ, (char *)&win); #endif (void) setsid(); (void) ioctl(slave, TIOCSCTTY, 0); }