]>
git.saurik.com Git - apple/shell_cmds.git/blob - script/script.c
2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.bin/script/script.c,v 1.24 2004/02/15 17:30:13 cperciva Exp $");
39 static const char copyright
[] =
40 "@(#) Copyright (c) 1980, 1992, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid
[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
48 #include <sys/types.h>
51 #include <sys/ioctl.h>
59 #endif /* !__APPLE__ */
69 #endif /* __APPLE__ */
79 void done(int) __dead2
;
81 void doshell(char **);
84 static void usage(void);
87 main(int argc
, char *argv
[])
90 struct termios rtt
, stt
;
92 int aflg
, kflg
, ch
, n
;
93 struct timeval tv
, *tvp
;
101 while ((ch
= getopt(argc
, argv
, "aqkt:")) != -1)
113 flushtime
= atoi(optarg
);
115 err(1, "invalid flush time %d", flushtime
);
129 fname
= "typescript";
131 if ((fscript
= fopen(fname
, aflg
? "a" : "w")) == NULL
)
134 if (ttyflg
= isatty(STDIN_FILENO
)) {
135 if (tcgetattr(STDIN_FILENO
, &tt
) == -1)
137 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &win
) == -1)
139 if (openpty(&master
, &slave
, NULL
, &tt
, &win
) == -1)
142 if (openpty(&master
, &slave
, NULL
, NULL
, NULL
) == -1)
148 (void)printf("Script started, output file is %s\n", fname
);
149 (void)fprintf(fscript
, "Script started on %s", ctime(&tvec
));
155 rtt
.c_lflag
&= ~ECHO
;
156 (void)tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &rtt
);
168 #endif /* __APPLE__ */
178 FD_SET(master
, &rfd
);
179 FD_SET(STDIN_FILENO
, &rfd
);
181 tv
.tv_sec
= flushtime
;
184 n
= select(master
+ 1, &rfd
, 0, 0, tvp
);
185 if (n
< 0 && errno
!= EINTR
)
187 if (n
> 0 && FD_ISSET(STDIN_FILENO
, &rfd
)) {
188 cc
= read(STDIN_FILENO
, ibuf
, BUFSIZ
);
192 (void)write(master
, ibuf
, 0);
194 (void)write(master
, ibuf
, cc
);
195 if (kflg
&& tcgetattr(master
, &stt
) >= 0 &&
196 ((stt
.c_lflag
& ECHO
) == 0)) {
197 (void)fwrite(ibuf
, 1, cc
, fscript
);
201 if (n
> 0 && FD_ISSET(master
, &rfd
)) {
202 cc
= read(master
, obuf
, sizeof (obuf
));
205 (void)write(STDOUT_FILENO
, obuf
, cc
);
206 (void)fwrite(obuf
, 1, cc
, fscript
);
209 if (tvec
- start
>= flushtime
) {
221 (void)fprintf(stderr
,
222 "usage: script [-akq] [-t time] [file [command ...]]\n");
233 while ((pid
= wait3(&status
, WNOHANG
, 0)) > 0)
236 if (WIFEXITED(status
))
237 e
= WEXITSTATUS(status
);
238 else if (WIFSIGNALED(status
))
239 e
= WTERMSIG(status
);
240 else /* can't happen */
253 shell
= getenv("SHELL");
255 shell
= _PATH_BSHELL
;
258 (void)fclose(fscript
);
264 execl(shell
, shell
, "-i", (char *)NULL
);
273 (void)kill(0, SIGTERM
);
283 (void)tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &tt
);
286 (void)fprintf(fscript
,"\nScript done on %s", ctime(&tvec
));
287 (void)printf("\nScript done, output file is %s\n", fname
);
289 (void)fclose(fscript
);