]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: optional timing output file argument added
authorSami Kerola <kerolasa@iki.fi>
Sat, 9 Apr 2011 19:40:45 +0000 (21:40 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 12 Apr 2011 12:21:36 +0000 (14:21 +0200)
And update to manual page accordingly.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
term-utils/script.1
term-utils/script.c

index f411dc6e79a08ed4806195c00fcff4ccf99c0523..725bdb29019bfbd2bbd106f3436a4acb519d0481 100644 (file)
@@ -44,7 +44,7 @@
 .Op Fl e
 .Op Fl f
 .Op Fl q
-.Op Fl t
+.Op Fl t[=FILE]
 .Op Fl V
 .Op Fl h
 .Op Ar file
@@ -86,12 +86,16 @@ One person does `mkfifo foo; script -f foo' and another can
 supervise real-time what is being done using `cat foo'.
 .It Fl q, Fl Fl quiet
 Be quiet.
-.It Fl t, Fl Fl timing
+.It Fl t, Fl Fl timing[=FILE]
 Output timing data to standard error. This data contains two fields,
 separated by a space. The first field indicates how much time elapsed since
 the previous output. The second field indicates how many characters were
 output this time. This information can be used to replay typescripts with
 realistic typing and output delays.
+
+The timing option is able to take file path as an argument. The
+file is used as output detination instead of standard error when
+it is supplied.
 .It Fl V, Fl Fl version
 Output version information and exit.
 .It Fl h, Fl Fl help
index d57d441867e1ac4951ad509d890eb020bddb0267..603d392d3a563c9f6b5365408c112aa70e6d4b88 100644 (file)
@@ -76,7 +76,7 @@ void fixtty(void);
 void getmaster(void);
 void getslave(void);
 void doinput(void);
-void dooutput(void);
+void dooutput(FILE *timingfd);
 void doshell(void);
 
 char   *shell;
@@ -133,7 +133,7 @@ usage(FILE *out)
                " -r, --return            return exit code of the child process\n"
                " -f, --flush             run flush after each write\n"
                " -q, --quiet             be quiet\n"
-               " -t, --timing            output timing data to stderr\n"
+               " -t, --timing=FILE       output timing data to stderr, or to file\n"
                " -V, --version           output version information and exit\n"
                " -h, --help              display this help and exit\n\n"));
 
@@ -155,6 +155,7 @@ main(int argc, char **argv) {
        struct sigaction sa;
        extern int optind;
        int ch;
+       FILE *timingfd = stderr;
 
        static const struct option longopts[] = {
                { "append",     no_argument,       0, 'a' },
@@ -162,7 +163,7 @@ main(int argc, char **argv) {
                { "return",     no_argument,       0, 'e' },
                { "flush",      no_argument,       0, 'f' },
                { "quiet",      no_argument,       0, 'q' },
-               { "timing",     no_argument,       0, 't' },
+               { "timing",     optional_argument, 0, 't' },
                { "version",    no_argument,       0, 'V' },
                { "help",       no_argument,       0, 'h' },
                { NULL,         0, 0, 0 }
@@ -173,7 +174,7 @@ main(int argc, char **argv) {
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
 
-       while ((ch = getopt_long(argc, argv, "ac:efqtVh", longopts, NULL)) != -1)
+       while ((ch = getopt_long(argc, argv, "ac:efqt::Vh", longopts, NULL)) != -1)
                switch((char)ch) {
                case 'a':
                        aflg++;
@@ -191,6 +192,9 @@ main(int argc, char **argv) {
                        qflg++;
                        break;
                case 't':
+                       if (optarg)
+                               if ((timingfd = fopen(optarg, "w")) == NULL)
+                                       err(EXIT_FAILURE, _("cannot open timing file %s"), optarg);
                        tflg++;
                        break;
                case 'V':
@@ -260,7 +264,7 @@ main(int argc, char **argv) {
                        fail();
                }
                if (child)
-                       dooutput();
+                       dooutput(timingfd);
                else
                        doshell();
        } else {
@@ -269,6 +273,7 @@ main(int argc, char **argv) {
        }
        doinput();
 
+       fclose(timingfd);
        return EXIT_SUCCESS;
 }
 
@@ -328,7 +333,7 @@ my_strftime(char *buf, size_t len, const char *fmt, const struct tm *tm) {
 }
 
 void
-dooutput() {
+dooutput(FILE *timingfd) {
        register ssize_t cc;
        time_t tvec;
        char obuf[BUFSIZ];
@@ -370,7 +375,7 @@ dooutput() {
                        break;
                if (tflg) {
                        newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
-                       fprintf(stderr, "%f %zd\n", newtime - oldtime, cc);
+                       fprintf(timingfd, "%f %zd\n", newtime - oldtime, cc);
                        oldtime = newtime;
                }
                wrt = write(1, obuf, cc);