]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/script.c
Imported from util-linux-2.11y tarball.
[thirdparty/util-linux.git] / misc-utils / script.c
1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * 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
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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 /*
35 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
36 * - added Native Language Support
37 *
38 * 2000-07-30 Per Andreas Buer <per@linpro.no> - added "q"-option
39 */
40
41 /*
42 * script
43 */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <paths.h>
47 #include <time.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <termios.h>
51 #include <sys/ioctl.h>
52 #include <sys/time.h>
53 #include <sys/file.h>
54 #include <sys/signal.h>
55 #include "nls.h"
56
57 #ifdef __linux__
58 #include <unistd.h>
59 #include <string.h>
60 #endif
61
62 #include "../defines.h"
63 #ifdef HAVE_openpty
64 #include <pty.h>
65 #endif
66
67 void finish(int);
68 void done(void);
69 void fail(void);
70 void resize(int);
71 void fixtty(void);
72 void getmaster(void);
73 void getslave(void);
74 void doinput(void);
75 void dooutput(void);
76 void doshell(void);
77
78 char *shell;
79 FILE *fscript;
80 int master;
81 int slave;
82 int child;
83 int subchild;
84 char *fname;
85
86 struct termios tt;
87 struct winsize win;
88 int lb;
89 int l;
90 #ifndef HAVE_openpty
91 char line[] = "/dev/ptyXX";
92 #endif
93 int aflg = 0;
94 int fflg = 0;
95 int qflg = 0;
96 int tflg = 0;
97
98 static char *progname;
99
100 static void
101 die_if_link(char *fn) {
102 struct stat s;
103
104 if (lstat(fn, &s) == 0 && (S_ISLNK(s.st_mode) || s.st_nlink > 1)) {
105 fprintf(stderr,
106 _("Warning: `%s' is a link.\n"
107 "Use `%s [options] %s' if you really "
108 "want to use it.\n"
109 "Script not started.\n"),
110 fn, progname, fn);
111 exit(1);
112 }
113 }
114
115 int
116 main(int argc, char **argv) {
117 extern int optind;
118 char *p;
119 int ch;
120
121 progname = argv[0];
122 if ((p = strrchr(progname, '/')) != NULL)
123 progname = p+1;
124
125
126 setlocale(LC_ALL, "");
127 bindtextdomain(PACKAGE, LOCALEDIR);
128 textdomain(PACKAGE);
129
130 if (argc == 2) {
131 if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
132 printf(_("%s from %s\n"),
133 progname, util_linux_version);
134 return 0;
135 }
136 }
137
138 while ((ch = getopt(argc, argv, "afqt")) != -1)
139 switch((char)ch) {
140 case 'a':
141 aflg++;
142 break;
143 case 'f':
144 fflg++;
145 break;
146 case 'q':
147 qflg++;
148 break;
149 case 't':
150 tflg++;
151 break;
152 case '?':
153 default:
154 fprintf(stderr,
155 _("usage: script [-a] [-f] [-q] [-t] [file]\n"));
156 exit(1);
157 }
158 argc -= optind;
159 argv += optind;
160
161 if (argc > 0)
162 fname = argv[0];
163 else {
164 fname = "typescript";
165 die_if_link(fname);
166 }
167 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
168 perror(fname);
169 fail();
170 }
171
172 shell = getenv("SHELL");
173 if (shell == NULL)
174 shell = _PATH_BSHELL;
175
176 getmaster();
177 if (!qflg)
178 printf(_("Script started, file is %s\n"), fname);
179 fixtty();
180
181 (void) signal(SIGCHLD, finish);
182 child = fork();
183 if (child < 0) {
184 perror("fork");
185 fail();
186 }
187 if (child == 0) {
188 subchild = child = fork();
189 if (child < 0) {
190 perror("fork");
191 fail();
192 }
193 if (child)
194 dooutput();
195 else
196 doshell();
197 } else
198 (void) signal(SIGWINCH, resize);
199 doinput();
200
201 return 0;
202 }
203
204 void
205 doinput() {
206 register int cc;
207 char ibuf[BUFSIZ];
208
209 (void) fclose(fscript);
210
211 while ((cc = read(0, ibuf, BUFSIZ)) > 0)
212 (void) write(master, ibuf, cc);
213 done();
214 }
215
216 #include <sys/wait.h>
217
218 void
219 finish(int dummy) {
220 int status;
221 register int pid;
222 register int die = 0;
223
224 while ((pid = wait3(&status, WNOHANG, 0)) > 0)
225 if (pid == child)
226 die = 1;
227
228 if (die)
229 done();
230 }
231
232 void
233 resize(int dummy) {
234 /* transmit window change information to the child */
235 (void) ioctl(0, TIOCGWINSZ, (char *)&win);
236 (void) ioctl(slave, TIOCSWINSZ, (char *)&win);
237
238 kill(child, SIGWINCH);
239 }
240
241 /*
242 * Stop extremely silly gcc complaint on %c:
243 * warning: `%c' yields only last 2 digits of year in some locales
244 */
245 static void
246 my_strftime(char *buf, size_t len, const char *fmt, const struct tm *tm) {
247 strftime(buf, len, fmt, tm);
248 }
249
250 void
251 dooutput() {
252 register int cc;
253 time_t tvec;
254 char obuf[BUFSIZ];
255 struct timeval tv;
256 double oldtime=time(NULL), newtime;
257
258 (void) close(0);
259 #ifdef HAVE_openpty
260 (void) close(slave);
261 #endif
262 tvec = time((time_t *)NULL);
263 my_strftime(obuf, sizeof obuf, "%c\n", localtime(&tvec));
264 fprintf(fscript, _("Script started on %s"), obuf);
265
266 for (;;) {
267 if (tflg)
268 gettimeofday(&tv, NULL);
269 cc = read(master, obuf, sizeof (obuf));
270 if (cc <= 0)
271 break;
272 if (tflg) {
273 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
274 fprintf(stderr, "%f %i\n", newtime - oldtime, cc);
275 oldtime = newtime;
276 }
277 (void) write(1, obuf, cc);
278 (void) fwrite(obuf, 1, cc, fscript);
279 if (fflg)
280 (void) fflush(fscript);
281 }
282 done();
283 }
284
285 void
286 doshell() {
287 /***
288 int t;
289
290 t = open(_PATH_TTY, O_RDWR);
291 if (t >= 0) {
292 (void) ioctl(t, TIOCNOTTY, (char *)0);
293 (void) close(t);
294 }
295 ***/
296 getslave();
297 (void) close(master);
298 (void) fclose(fscript);
299 (void) dup2(slave, 0);
300 (void) dup2(slave, 1);
301 (void) dup2(slave, 2);
302 (void) close(slave);
303 #ifdef __linux__
304 execl(shell, strrchr(shell, '/') + 1, "-i", 0);
305 #else
306 execl(shell, "sh", "-i", 0);
307 #endif
308 perror(shell);
309 fail();
310 }
311
312 void
313 fixtty() {
314 struct termios rtt;
315
316 rtt = tt;
317 cfmakeraw(&rtt);
318 rtt.c_lflag &= ~ECHO;
319 (void) tcsetattr(0, TCSAFLUSH, &rtt);
320 }
321
322 void
323 fail() {
324
325 (void) kill(0, SIGTERM);
326 done();
327 }
328
329 void
330 done() {
331 time_t tvec;
332
333 if (subchild) {
334 if (!qflg) {
335 char buf[BUFSIZ];
336 tvec = time((time_t *)NULL);
337 my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec));
338 fprintf(fscript, _("\nScript done on %s"), buf);
339 }
340 (void) fclose(fscript);
341 (void) close(master);
342 } else {
343 (void) tcsetattr(0, TCSAFLUSH, &tt);
344 if (!qflg)
345 printf(_("Script done, file is %s\n"), fname);
346 }
347 exit(0);
348 }
349
350 void
351 getmaster() {
352 #ifdef HAVE_openpty
353 (void) tcgetattr(0, &tt);
354 (void) ioctl(0, TIOCGWINSZ, (char *)&win);
355 if (openpty(&master, &slave, NULL, &tt, &win) < 0) {
356 fprintf(stderr, _("openpty failed\n"));
357 fail();
358 }
359 #else
360 char *pty, *bank, *cp;
361 struct stat stb;
362
363 pty = &line[strlen("/dev/ptyp")];
364 for (bank = "pqrs"; *bank; bank++) {
365 line[strlen("/dev/pty")] = *bank;
366 *pty = '0';
367 if (stat(line, &stb) < 0)
368 break;
369 for (cp = "0123456789abcdef"; *cp; cp++) {
370 *pty = *cp;
371 master = open(line, O_RDWR);
372 if (master >= 0) {
373 char *tp = &line[strlen("/dev/")];
374 int ok;
375
376 /* verify slave side is usable */
377 *tp = 't';
378 ok = access(line, R_OK|W_OK) == 0;
379 *tp = 'p';
380 if (ok) {
381 (void) tcgetattr(0, &tt);
382 (void) ioctl(0, TIOCGWINSZ,
383 (char *)&win);
384 return;
385 }
386 (void) close(master);
387 }
388 }
389 }
390 fprintf(stderr, _("Out of pty's\n"));
391 fail();
392 #endif /* not HAVE_openpty */
393 }
394
395 void
396 getslave() {
397 #ifndef HAVE_openpty
398 line[strlen("/dev/")] = 't';
399 slave = open(line, O_RDWR);
400 if (slave < 0) {
401 perror(line);
402 fail();
403 }
404 (void) tcsetattr(slave, TCSAFLUSH, &tt);
405 (void) ioctl(slave, TIOCSWINSZ, (char *)&win);
406 #endif
407 (void) setsid();
408 (void) ioctl(slave, TIOCSCTTY, 0);
409 }