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