]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: move timing file opening close to use of it
authorSami Kerola <kerolasa@iki.fi>
Tue, 3 Mar 2015 22:06:01 +0000 (22:06 +0000)
committerSami Kerola <kerolasa@iki.fi>
Mon, 8 Jun 2015 20:53:37 +0000 (21:53 +0100)
This allows removing almost immediate closure of file handle in the
doshell() function that does not use the file.

Proposed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
term-utils/script.c

index 00285f2609242ffb08b2f8cf342491c8ba5108a6..37051632f9b84f970e94b98c29d3e3f4b1411a90 100644 (file)
@@ -90,6 +90,7 @@ struct script_control {
        char *cflg;             /* command to be executed */
        char *fname;            /* output file path */
        FILE *typescriptfp;     /* output file pointer */
+       char *tname;            /* timing file path */
        FILE *timingfp;         /* timing file pointer */
        struct timeval oldtime; /* previous write or command start time */
        int master;             /* pseudoterminal master file descriptor */
@@ -186,6 +187,9 @@ static void __attribute__((__noreturn__)) done(struct script_control *ctl)
                else
                        exit(WEXITSTATUS(ctl->childstatus));
        }
+       if (ctl->timingfp)
+               fclose(ctl->timingfp);
+       fclose(ctl->typescriptfp);
        exit(EXIT_SUCCESS);
 }
 
@@ -288,8 +292,17 @@ static void do_io(struct script_control *ctl)
        time_t tvec = script_time((time_t *)NULL);
        char buf[128];
 
-       if (ctl->tflg && !ctl->timingfp)
-               ctl->timingfp = fdopen(STDERR_FILENO, "w");
+       if ((ctl->typescriptfp = fopen(ctl->fname, ctl->aflg ? "a" : "w")) == NULL) {
+               warn(_("cannot open %s"), ctl->fname);
+               fail(ctl);
+       }
+       if (ctl->tflg) {
+               if (!ctl->tname) {
+                       if (!(ctl->timingfp = fopen("/dev/stderr", "w")))
+                               err(EXIT_FAILURE, _("cannot open %s"), "/dev/stderr");
+               } else if (!(ctl->timingfp = fopen(ctl->tname, "w")))
+                       err(EXIT_FAILURE, _("cannot open %s"), ctl->tname);
+       }
 
        pfd[0].fd = STDIN_FILENO;
        pfd[0].events = POLLIN;
@@ -366,11 +379,6 @@ static void __attribute__((__noreturn__)) doshell(struct script_control *ctl)
 
        /* close things irrelevant for this process */
        close(ctl->master);
-       if (ctl->typescriptfp)
-               fclose(ctl->typescriptfp);
-       if (ctl->timingfp)
-               fclose(ctl->timingfp);
-       ctl->typescriptfp = ctl->timingfp = NULL;
 
        dup2(ctl->slave, STDIN_FILENO);
        dup2(ctl->slave, STDOUT_FILENO);
@@ -544,8 +552,8 @@ int main(int argc, char **argv)
                        ctl.qflg = 1;
                        break;
                case 't':
-                       if (optarg && !(ctl.timingfp = fopen(optarg, "w")))
-                               err(EXIT_FAILURE, _("cannot open %s"), optarg);
+                       if (optarg)
+                               ctl.tname = optarg;
                        ctl.tflg = 1;
                        break;
                case 'V':
@@ -568,11 +576,6 @@ int main(int argc, char **argv)
                die_if_link(&ctl);
        }
 
-       if ((ctl.typescriptfp = fopen(ctl.fname, ctl.aflg ? "a" : "w")) == NULL) {
-               warn(_("cannot open %s"), ctl.fname);
-               fail(&ctl);
-       }
-
        ctl.shell = getenv("SHELL");
        if (ctl.shell == NULL)
                ctl.shell = _PATH_BSHELL;