]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/script.c
2 * Copyright (c) 1980 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
36 * - added Native Language Support
38 * 2000-07-30 Per Andreas Buer <per@linpro.no> - added "q"-option
48 #include <sys/types.h>
51 #include <sys/ioctl.h>
54 #include <sys/signal.h>
62 #include "../defines.h"
91 char line
[] = "/dev/ptyXX";
98 static char *progname
;
101 die_if_link(char *fn
) {
104 if (lstat(fn
, &s
) == 0 && (S_ISLNK(s
.st_mode
) || s
.st_nlink
> 1)) {
106 _("Warning: `%s' is a link.\n"
107 "Use `%s [options] %s' if you really "
109 "Script not started.\n"),
116 main(int argc
, char **argv
) {
122 if ((p
= strrchr(progname
, '/')) != NULL
)
126 setlocale(LC_ALL
, "");
127 bindtextdomain(PACKAGE
, LOCALEDIR
);
131 if (!strcmp(argv
[1], "-V") || !strcmp(argv
[1], "--version")) {
132 printf(_("%s from %s\n"),
133 progname
, util_linux_version
);
138 while ((ch
= getopt(argc
, argv
, "afqt")) != -1)
155 _("usage: script [-a] [-f] [-q] [-t] [file]\n"));
164 fname
= "typescript";
167 if ((fscript
= fopen(fname
, aflg
? "a" : "w")) == NULL
) {
172 shell
= getenv("SHELL");
174 shell
= _PATH_BSHELL
;
178 printf(_("Script started, file is %s\n"), fname
);
181 (void) signal(SIGCHLD
, finish
);
188 subchild
= child
= fork();
198 (void) signal(SIGWINCH
, resize
);
209 (void) fclose(fscript
);
211 while ((cc
= read(0, ibuf
, BUFSIZ
)) > 0)
212 (void) write(master
, ibuf
, cc
);
216 #include <sys/wait.h>
222 register int die
= 0;
224 while ((pid
= wait3(&status
, WNOHANG
, 0)) > 0)
234 /* transmit window change information to the child */
235 (void) ioctl(0, TIOCGWINSZ
, (char *)&win
);
236 (void) ioctl(slave
, TIOCSWINSZ
, (char *)&win
);
238 kill(child
, SIGWINCH
);
242 * Stop extremely silly gcc complaint on %c:
243 * warning: `%c' yields only last 2 digits of year in some locales
246 my_strftime(char *buf
, size_t len
, const char *fmt
, const struct tm
*tm
) {
247 strftime(buf
, len
, fmt
, tm
);
256 double oldtime
=time(NULL
), newtime
;
262 tvec
= time((time_t *)NULL
);
263 my_strftime(obuf
, sizeof obuf
, "%c\n", localtime(&tvec
));
264 fprintf(fscript
, _("Script started on %s"), obuf
);
268 gettimeofday(&tv
, NULL
);
269 cc
= read(master
, obuf
, sizeof (obuf
));
273 newtime
= tv
.tv_sec
+ (double) tv
.tv_usec
/ 1000000;
274 fprintf(stderr
, "%f %i\n", newtime
- oldtime
, cc
);
277 (void) write(1, obuf
, cc
);
278 (void) fwrite(obuf
, 1, cc
, fscript
);
280 (void) fflush(fscript
);
290 t = open(_PATH_TTY, O_RDWR);
292 (void) ioctl(t, TIOCNOTTY, (char *)0);
297 (void) close(master
);
298 (void) fclose(fscript
);
299 (void) dup2(slave
, 0);
300 (void) dup2(slave
, 1);
301 (void) dup2(slave
, 2);
304 execl(shell
, strrchr(shell
, '/') + 1, "-i", 0);
306 execl(shell
, "sh", "-i", 0);
318 rtt
.c_lflag
&= ~ECHO
;
319 (void) tcsetattr(0, TCSAFLUSH
, &rtt
);
325 (void) kill(0, SIGTERM
);
336 tvec
= time((time_t *)NULL
);
337 my_strftime(buf
, sizeof buf
, "%c\n", localtime(&tvec
));
338 fprintf(fscript
, _("\nScript done on %s"), buf
);
340 (void) fclose(fscript
);
341 (void) close(master
);
343 (void) tcsetattr(0, TCSAFLUSH
, &tt
);
345 printf(_("Script done, file is %s\n"), fname
);
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"));
360 char *pty
, *bank
, *cp
;
363 pty
= &line
[strlen("/dev/ptyp")];
364 for (bank
= "pqrs"; *bank
; bank
++) {
365 line
[strlen("/dev/pty")] = *bank
;
367 if (stat(line
, &stb
) < 0)
369 for (cp
= "0123456789abcdef"; *cp
; cp
++) {
371 master
= open(line
, O_RDWR
);
373 char *tp
= &line
[strlen("/dev/")];
376 /* verify slave side is usable */
378 ok
= access(line
, R_OK
|W_OK
) == 0;
381 (void) tcgetattr(0, &tt
);
382 (void) ioctl(0, TIOCGWINSZ
,
386 (void) close(master
);
390 fprintf(stderr
, _("Out of pty's\n"));
392 #endif /* not HAVE_openpty */
398 line
[strlen("/dev/")] = 't';
399 slave
= open(line
, O_RDWR
);
404 (void) tcsetattr(slave
, TCSAFLUSH
, &tt
);
405 (void) ioctl(slave
, TIOCSWINSZ
, (char *)&win
);
408 (void) ioctl(slave
, TIOCSCTTY
, 0);