periodic: Remove obsolete daily/status-named and weekly/clean-kvmdb
[dragonfly.git] / usr.bin / script / script.c
blobe3931ec17b70d37568e47dc8316c1095b3a095d3
1 /*
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
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)script.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/usr.bin/script/script.c,v 1.11.2.2 2004/03/13 09:21:00 cperciva Exp $
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <libutil.h>
44 #include <paths.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
50 #include <unistd.h>
52 static FILE *fscript;
53 static int master, slave;
54 static const char *fname;
55 static int qflg, ttyflg;
57 struct termios tt;
59 static void done(int) __dead2;
60 static void doshell(char **);
61 static int reap(pid_t);
62 static void usage(void);
64 int
65 main(int argc, char **argv)
67 int cc;
68 struct termios rtt, stt;
69 struct winsize win;
70 int aflg, kflg, ch, n;
71 struct timeval tv, *tvp;
72 time_t tvec, start;
73 char obuf[BUFSIZ];
74 char ibuf[BUFSIZ];
75 fd_set rfd;
76 int flushtime = 30;
77 pid_t child;
79 aflg = kflg = 0;
80 while ((ch = getopt(argc, argv, "aqkt:")) != -1) {
81 switch(ch) {
82 case 'a':
83 aflg = 1;
84 break;
85 case 'q':
86 qflg = 1;
87 break;
88 case 'k':
89 kflg = 1;
90 break;
91 case 't':
92 flushtime = atoi(optarg);
93 if (flushtime < 0)
94 errx(1, "invalid flush time %d", flushtime);
95 break;
96 case '?':
97 default:
98 usage();
101 argc -= optind;
102 argv += optind;
104 if (argc > 0) {
105 fname = argv[0];
106 argv++;
107 argc--;
108 } else
109 fname = "typescript";
111 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
112 err(1, "%s", fname);
114 if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
115 if (tcgetattr(STDIN_FILENO, &tt) == -1)
116 err(1, "tcgetattr");
117 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
118 err(1, "ioctl");
119 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
120 err(1, "openpty");
121 } else {
122 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
123 err(1, "openpty");
126 if (!qflg) {
127 tvec = time(NULL);
128 printf("Script started, output file is %s\n", fname);
129 fprintf(fscript, "Script started on %s", ctime(&tvec));
130 fflush(fscript);
133 if (ttyflg) {
134 rtt = tt;
135 cfmakeraw(&rtt);
136 rtt.c_lflag &= ~ECHO;
137 tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
140 child = fork();
141 if (child < 0) {
142 warn("fork");
143 done(1);
145 if (child == 0)
146 doshell(argv);
147 close(slave);
149 if (flushtime > 0)
150 tvp = &tv;
151 else
152 tvp = NULL;
154 start = time(0);
155 FD_ZERO(&rfd);
156 for (;;) {
157 FD_SET(master, &rfd);
158 FD_SET(STDIN_FILENO, &rfd);
159 if (flushtime > 0) {
160 tv.tv_sec = flushtime;
161 tv.tv_usec = 0;
163 n = select(master + 1, &rfd, 0, 0, tvp);
164 if (n < 0 && errno != EINTR)
165 break;
166 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
167 cc = read(STDIN_FILENO, ibuf, sizeof(ibuf));
168 if (cc < 0)
169 break;
170 if (cc == 0)
171 write(master, ibuf, 0);
172 if (cc > 0) {
173 write(master, ibuf, cc);
174 if (kflg && tcgetattr(master, &stt) >= 0 &&
175 ((stt.c_lflag & ECHO) == 0)) {
176 fwrite(ibuf, 1, cc, fscript);
180 if (n > 0 && FD_ISSET(master, &rfd)) {
181 cc = read(master, obuf, sizeof(obuf));
182 if (cc <= 0)
183 break;
184 write(STDOUT_FILENO, obuf, cc);
185 fwrite(obuf, 1, cc, fscript);
187 tvec = time(0);
188 if (tvec - start >= flushtime) {
189 fflush(fscript);
190 start = tvec;
193 done(reap(child));
196 static void
197 usage(void)
199 fprintf(stderr,
200 "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n");
201 exit(1);
204 static int
205 reap(pid_t child)
207 int e;
208 pid_t pid;
209 int status;
211 e = 0;
212 while ((pid = wait3(&status, WNOHANG, NULL)) > 0) {
213 if (pid == child) {
214 if (WIFEXITED(status))
215 e = WEXITSTATUS(status);
216 else if (WIFSIGNALED(status))
217 e = WTERMSIG(status);
218 else /* can't happen */
219 e = 1;
223 return(e);
226 static void
227 doshell(char **av)
229 const char *shell;
231 shell = getenv("SHELL");
232 if (shell == NULL)
233 shell = _PATH_BSHELL;
235 close(master);
236 fclose(fscript);
237 login_tty(slave);
238 if (av[0] != NULL) {
239 execvp(av[0], av);
240 warn("%s", av[0]);
241 } else {
242 execl(shell, shell, "-i", NULL);
243 warn("%s", shell);
245 kill(0, SIGTERM);
246 done(1);
249 static void
250 done(int eno)
252 time_t tvec;
254 if (ttyflg)
255 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
256 tvec = time(NULL);
257 if (!qflg) {
258 fprintf(fscript,"\nScript done on %s", ctime(&tvec));
259 printf("\nScript done, output file is '%s'\n", fname);
261 fclose(fscript);
262 close(master);
263 exit(eno);