View | Details | Raw Unified | Return to bug 248372
Collapse All | Expand All

(-)script.c (-7 / +50 lines)
Lines 400-418 Link Here
400
}
400
}
401
401
402
static void
402
static void
403
termset()
404
{
405
	struct termios tattr;
406
407
	ttyflg = isatty(STDOUT_FILENO);
408
	if (!ttyflg)
409
		return;
410
411
	if (tcgetattr(STDOUT_FILENO, &tt) != 0)
412
		err(1, "tcgetattr");
413
414
	tattr = tt;
415
	cfmakeraw(&tattr);
416
	tattr.c_lflag |= ISIG;
417
	if (tcsetattr(STDOUT_FILENO, TCSANOW, &tattr) != 0)
418
		err(1, "tcsetattr");
419
}
420
421
static void
422
termreset()
423
{
424
	if (ttyflg)
425
		tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt);
426
427
	ttyflg = 0;
428
}
429
430
static void
403
consume(FILE *fp, off_t len, char *buf, int reg)
431
consume(FILE *fp, off_t len, char *buf, int reg)
404
{
432
{
405
	size_t l;
433
	size_t l;
406
434
407
	if (reg) {
435
	if (reg) {
408
		if (fseeko(fp, len, SEEK_CUR) == -1)
436
		if (fseeko(fp, len, SEEK_CUR) == -1) {
437
			termreset();
409
			err(1, NULL);
438
			err(1, NULL);
439
		}
410
	}
440
	}
411
	else {
441
	else {
412
		while (len > 0) {
442
		while (len > 0) {
413
			l = MIN(DEF_BUF, len);
443
			l = MIN(DEF_BUF, len);
414
			if (fread(buf, sizeof(char), l, fp) != l)
444
			if (fread(buf, sizeof(char), l, fp) != l) {
445
				termreset();
415
				err(1, "cannot read buffer");
446
				err(1, "cannot read buffer");
447
			}
416
			len -= l;
448
			len -= l;
417
		}
449
		}
418
	}
450
	}
Lines 439-444 Link Here
439
	time_t tclock;
471
	time_t tclock;
440
	int reg;
472
	int reg;
441
473
474
	ttyflg = 0;
475
442
	if (fstat(fileno(fp), &pst) == -1)
476
	if (fstat(fileno(fp), &pst) == -1)
443
		err(1, "fstat failed");
477
		err(1, "fstat failed");
444
478
Lines 446-462 Link Here
446
480
447
	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
481
	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
448
		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
482
		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
449
			if (reg)
483
			if (reg) {
484
				termreset();
450
				err(1, "reading playback header");
485
				err(1, "reading playback header");
451
			else
486
			}
452
				break;
487
			break;
453
		}
488
		}
454
		swapstamp(stamp);
489
		swapstamp(stamp);
455
		save_len = sizeof(stamp);
490
		save_len = sizeof(stamp);
456
491
457
		if (reg && stamp.scr_len >
492
		if (reg && stamp.scr_len >
458
		    (uint64_t)(pst.st_size - save_len) - nread)
493
		    (uint64_t)(pst.st_size - save_len) - nread) {
494
			termreset();
459
			errx(1, "invalid stamp");
495
			errx(1, "invalid stamp");
496
		}
460
497
461
		save_len += stamp.scr_len;
498
		save_len += stamp.scr_len;
462
		tclock = stamp.scr_sec;
499
		tclock = stamp.scr_sec;
Lines 470-477 Link Here
470
				ctime(&tclock));
507
				ctime(&tclock));
471
			tsi = tso;
508
			tsi = tso;
472
			(void)consume(fp, stamp.scr_len, buf, reg);
509
			(void)consume(fp, stamp.scr_len, buf, reg);
510
			termset();
473
			break;
511
			break;
474
		case 'e':
512
		case 'e':
513
			termreset();
475
			if (!qflg)
514
			if (!qflg)
476
				(void)printf("\nScript done on %s",
515
				(void)printf("\nScript done on %s",
477
				    ctime(&tclock));
516
				    ctime(&tclock));
Lines 493-509 Link Here
493
			tsi = tso;
532
			tsi = tso;
494
			while (stamp.scr_len > 0) {
533
			while (stamp.scr_len > 0) {
495
				l = MIN(DEF_BUF, stamp.scr_len);
534
				l = MIN(DEF_BUF, stamp.scr_len);
496
				if (fread(buf, sizeof(char), l, fp) != l)
535
				if (fread(buf, sizeof(char), l, fp) != l) {
536
					termreset();
497
					err(1, "cannot read buffer");
537
					err(1, "cannot read buffer");
538
				}
498
539
499
				(void)write(STDOUT_FILENO, buf, l);
540
				(void)write(STDOUT_FILENO, buf, l);
500
				stamp.scr_len -= l;
541
				stamp.scr_len -= l;
501
			}
542
			}
502
			break;
543
			break;
503
		default:
544
		default:
545
			termreset();
504
			errx(1, "invalid direction");
546
			errx(1, "invalid direction");
505
		}
547
		}
506
	}
548
	}
549
	termreset();
507
	(void)fclose(fp);
550
	(void)fclose(fp);
508
	exit(0);
551
	exit(0);
509
}
552
}

Return to bug 248372