Hi tech.

While trying to test liveshell[1], I noticed the script(1) from base is
missing the -f option, flushing the output after each write, which is
kinda critical for monitoring the output file by another process. the
Linux script utility uses fflush(3) after each write (duhh) but I'm not
sure if anything more than removing buffering operation from
the stream is necessary.

So here's the diff which is working for me

1: http://liveshell.43z.one/

--
Bijan Ebrahimi


diff --git usr.bin/script/script.1 usr.bin/script/script.1
index f10ec2d4b..48a9d683f 100644
--- usr.bin/script/script.1
+++ usr.bin/script/script.1
@@ -38,7 +38,7 @@
 .Nd make typescript of terminal session
 .Sh SYNOPSIS
 .Nm script
-.Op Fl a
+.Op Fl af
 .Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
@@ -71,6 +71,8 @@ Run
 .Ar command
 instead of an interactive shell.
 To run a command with arguments, enclose both in quotes.
+.It Fl f
+Flush the output after each write.
 .El
 .Pp
 The script ends when the forked program exits (a control-D
diff --git usr.bin/script/script.c usr.bin/script/script.c
index 2e4173941..b23e1c4d4 100644
--- usr.bin/script/script.c
+++ usr.bin/script/script.c
@@ -1,4 +1,4 @@
-/*     $OpenBSD: script.c,v 1.34 2018/01/21 20:18:20 jasper Exp $      */
+/*     $OpenBSD: script.c,v 0.34 2018/01/21 20:18:20 jasper Exp $      */
 /*     $NetBSD: script.c,v 1.3 1994/12/21 08:55:43 jtc Exp $   */

 /*
@@ -104,11 +104,11 @@ main(int argc, char *argv[])
        char ibuf[BUFSIZ];
        char *cmd;
        ssize_t cc, off;
-       int aflg, ch;
+       int aflg, fflg, ch;

        cmd = NULL;
-       aflg = 0;
-       while ((ch = getopt(argc, argv, "ac:")) != -1)
+       aflg = fflg = 0;
+       while ((ch = getopt(argc, argv, "ac:f")) != -1)
                switch(ch) {
                case 'a':
                        aflg = 1;
@@ -116,8 +116,11 @@ main(int argc, char *argv[])
                case 'c':
                        cmd = optarg;
                        break;
+               case 'f':
+                       fflg = 1;
+                       break;
                default:
-                       fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
+                       fprintf(stderr, "usage: %s [-af] [-c command] [file]\n",
                            __progname);
                        exit(1);
                }
@@ -132,6 +135,9 @@ main(int argc, char *argv[])
        if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
                err(1, "%s", fname);

+       if (fflg)
+               setvbuf(fscript, NULL, _IONBF, 0);
+
        if (isatty(0)) {
                if (tcgetattr(STDIN_FILENO, &tt) == 0 &&
                    ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == 0)

Reply via email to