summaryrefslogtreecommitdiffstats
path: root/term-utils/script.c
diff options
context:
space:
mode:
authorSami Kerola2015-03-03 23:06:01 +0100
committerSami Kerola2015-06-08 22:53:37 +0200
commit9580536a7ffc1b962cfcd8026604a5402fb2a54f (patch)
treea49f206b4cf4664192dab4df27a14567d49a8c2c /term-utils/script.c
parentscript: add noreturn function attributes (diff)
downloadkernel-qcow2-util-linux-9580536a7ffc1b962cfcd8026604a5402fb2a54f.tar.gz
kernel-qcow2-util-linux-9580536a7ffc1b962cfcd8026604a5402fb2a54f.tar.xz
kernel-qcow2-util-linux-9580536a7ffc1b962cfcd8026604a5402fb2a54f.zip
script: move timing file opening close to use of it
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>
Diffstat (limited to 'term-utils/script.c')
-rw-r--r--term-utils/script.c31
1 files changed, 17 insertions, 14 deletions
diff --git a/term-utils/script.c b/term-utils/script.c
index 00285f260..37051632f 100644
--- a/term-utils/script.c
+++ b/term-utils/script.c
@@ -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;