script.c (1c8af8787354e20c2b38cab5801698133ff8b403) script.c (236d2f558312262d86504309292a2358283f626c)
1/*
2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
1/*
2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
36"@(#) Copyright (c) 1980, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
41static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
42static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45 "$Id$";
42#endif /* not lint */
43
44#include <sys/types.h>
45#include <sys/wait.h>
46#include <sys/stat.h>
47#include <sys/ioctl.h>
48#include <sys/time.h>
49
46#endif /* not lint */
47
48#include <sys/types.h>
49#include <sys/wait.h>
50#include <sys/stat.h>
51#include <sys/ioctl.h>
52#include <sys/time.h>
53
50#include <errno.h>
54#include <err.h>
51#include <fcntl.h>
55#include <fcntl.h>
56#include <libutil.h>
52#include <paths.h>
53#include <signal.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <termios.h>
58#include <unistd.h>
59
60FILE *fscript;
61int master, slave;
62int child, subchild;
63int outcc;
64char *fname;
65
66struct termios tt;
67
68void done __P((void)) __dead2;
69void dooutput __P((void));
70void doshell __P((void));
57#include <paths.h>
58#include <signal.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <termios.h>
63#include <unistd.h>
64
65FILE *fscript;
66int master, slave;
67int child, subchild;
68int outcc;
69char *fname;
70
71struct termios tt;
72
73void done __P((void)) __dead2;
74void dooutput __P((void));
75void doshell __P((void));
71void err __P((const char *, ...));
72void fail __P((void));
73void finish __P((int));
74void scriptflush __P((int));
76void fail __P((void));
77void finish __P((int));
78void scriptflush __P((int));
79static void usage __P((void));
75
76int
77main(argc, argv)
78 int argc;
79 char *argv[];
80{
81 register int cc;
82 struct termios rtt;

--- 4 unchanged lines hidden (view full) ---

87 aflg = 0;
88 while ((ch = getopt(argc, argv, "a")) != -1)
89 switch(ch) {
90 case 'a':
91 aflg = 1;
92 break;
93 case '?':
94 default:
80
81int
82main(argc, argv)
83 int argc;
84 char *argv[];
85{
86 register int cc;
87 struct termios rtt;

--- 4 unchanged lines hidden (view full) ---

92 aflg = 0;
93 while ((ch = getopt(argc, argv, "a")) != -1)
94 switch(ch) {
95 case 'a':
96 aflg = 1;
97 break;
98 case '?':
99 default:
95 (void)fprintf(stderr, "usage: script [-a] [file]\n");
96 exit(1);
100 usage();
97 }
98 argc -= optind;
99 argv += optind;
100
101 if (argc > 0)
102 fname = argv[0];
103 else
104 fname = "typescript";
105
106 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
101 }
102 argc -= optind;
103 argv += optind;
104
105 if (argc > 0)
106 fname = argv[0];
107 else
108 fname = "typescript";
109
110 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
107 err("%s: %s", fname, strerror(errno));
111 err(1, "%s", fname);
108
109 (void)tcgetattr(STDIN_FILENO, &tt);
110 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
111 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
112
113 (void)tcgetattr(STDIN_FILENO, &tt);
114 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
115 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
112 err("openpty: %s", strerror(errno));
116 err(1, "openpty");
113
114 (void)printf("Script started, output file is %s\n", fname);
115 rtt = tt;
116 cfmakeraw(&rtt);
117 rtt.c_lflag &= ~ECHO;
118 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
119
120 (void)signal(SIGCHLD, finish);
121 child = fork();
122 if (child < 0) {
117
118 (void)printf("Script started, output file is %s\n", fname);
119 rtt = tt;
120 cfmakeraw(&rtt);
121 rtt.c_lflag &= ~ECHO;
122 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
123
124 (void)signal(SIGCHLD, finish);
125 child = fork();
126 if (child < 0) {
123 perror("fork");
127 warn("fork");
124 fail();
125 }
126 if (child == 0) {
127 subchild = child = fork();
128 if (child < 0) {
128 fail();
129 }
130 if (child == 0) {
131 subchild = child = fork();
132 if (child < 0) {
129 perror("fork");
133 warn("fork");
130 fail();
131 }
132 if (child)
133 dooutput();
134 else
135 doshell();
136 }
137
138 (void)fclose(fscript);
139 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0)
140 (void)write(master, ibuf, cc);
141 done();
142}
143
134 fail();
135 }
136 if (child)
137 dooutput();
138 else
139 doshell();
140 }
141
142 (void)fclose(fscript);
143 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0)
144 (void)write(master, ibuf, cc);
145 done();
146}
147
148static void
149usage()
150{
151 (void)fprintf(stderr, "usage: script [-a] [file]\n");
152 exit(1);
153}
154
144void
145finish(signo)
146 int signo;
147{
148 register int die, pid;
149 union wait status;
150
151 die = 0;

--- 51 unchanged lines hidden (view full) ---

203 shell = getenv("SHELL");
204 if (shell == NULL)
205 shell = _PATH_BSHELL;
206
207 (void)close(master);
208 (void)fclose(fscript);
209 login_tty(slave);
210 execl(shell, "sh", "-i", NULL);
155void
156finish(signo)
157 int signo;
158{
159 register int die, pid;
160 union wait status;
161
162 die = 0;

--- 51 unchanged lines hidden (view full) ---

214 shell = getenv("SHELL");
215 if (shell == NULL)
216 shell = _PATH_BSHELL;
217
218 (void)close(master);
219 (void)fclose(fscript);
220 login_tty(slave);
221 execl(shell, "sh", "-i", NULL);
211 perror(shell);
222 warn(shell);
212 fail();
213}
214
215void
216fail()
217{
218
219 (void)kill(0, SIGTERM);

--- 11 unchanged lines hidden (view full) ---

231 (void)fclose(fscript);
232 (void)close(master);
233 } else {
234 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
235 (void)printf("Script done, output file is %s\n", fname);
236 }
237 exit(0);
238}
223 fail();
224}
225
226void
227fail()
228{
229
230 (void)kill(0, SIGTERM);

--- 11 unchanged lines hidden (view full) ---

242 (void)fclose(fscript);
243 (void)close(master);
244 } else {
245 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
246 (void)printf("Script done, output file is %s\n", fname);
247 }
248 exit(0);
249}
239
240#if __STDC__
241#include <stdarg.h>
242#else
243#include <varargs.h>
244#endif
245
246void
247#if __STDC__
248err(const char *fmt, ...)
249#else
250err(fmt, va_alist)
251 char *fmt;
252 va_dcl
253#endif
254{
255 va_list ap;
256#if __STDC__
257 va_start(ap, fmt);
258#else
259 va_start(ap);
260#endif
261 (void)fprintf(stderr, "script: ");
262 (void)vfprintf(stderr, fmt, ap);
263 va_end(ap);
264 (void)fprintf(stderr, "\n");
265 exit(1);
266 /* NOTREACHED */
267}