]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: add COMMAND= to log header
authorKarel Zak <kzak@redhat.com>
Tue, 25 Jan 2022 15:05:46 +0000 (16:05 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 25 Jan 2022 15:05:46 +0000 (16:05 +0100)
Fixes: https://github.com/util-linux/util-linux/pull/1566
Co-Author: Henrik Bach <bach.henrik@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/script.c

index c8b55c8ef60ba9bd11c25a869c6609ebeb14a7d4..6542314121bd8984795c8b66129166568c772969 100644 (file)
@@ -131,6 +131,7 @@ struct script_control {
 
        const char *ttyname;
        const char *ttytype;
+       const char *command;
        int ttycols;
        int ttylines;
 
@@ -389,21 +390,26 @@ static int log_start(struct script_control *ctl,
        switch (log->format) {
        case SCRIPT_FMT_RAW:
        {
+               int x = 0;
                char buf[FORMAT_TIMESTAMP_MAX];
                time_t tvec = script_time((time_t *)NULL);
 
                strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
                fprintf(log->fp, _("Script started on %s ["), buf);
 
+               if (ctl->command)
+                       x += fprintf(log->fp, "COMMAND=\"%s\"", ctl->command);
+
                if (ctl->isterm) {
                        init_terminal_info(ctl);
 
                        if (ctl->ttytype)
-                               fprintf(log->fp, "TERM=\"%s\" ", ctl->ttytype);
+                               x += fprintf(log->fp, "%*sTERM=\"%s\"", !!x, "", ctl->ttytype);
                        if (ctl->ttyname)
-                               fprintf(log->fp, "TTY=\"%s\" ", ctl->ttyname);
+                               x += fprintf(log->fp, "%*sTTY=\"%s\"", !!x, "", ctl->ttyname);
 
-                       fprintf(log->fp, "COLUMNS=\"%d\" LINES=\"%d\"", ctl->ttycols, ctl->ttylines);
+                       fprintf(log->fp, "%*sCOLUMNS=\"%d\" LINES=\"%d\"", !!x, "",
+                                       ctl->ttycols, ctl->ttylines);
                } else
                        fprintf(log->fp, _("<not executed on terminal>"));
 
@@ -756,7 +762,7 @@ int main(int argc, char **argv)
        struct ul_pty_callbacks *cb;
        int ch, format = 0, caught_signal = 0, rc = 0, echo = 1;
        const char *outfile = NULL, *infile = NULL;
-       const char *timingfile = NULL, *shell = NULL, *command = NULL;
+       const char *timingfile = NULL, *shell = NULL;
 
        enum { FORCE_OPTION = CHAR_MAX + 1 };
 
@@ -811,7 +817,7 @@ int main(int argc, char **argv)
                        ctl.append = 1;
                        break;
                case 'c':
-                       command = optarg;
+                       ctl.command = optarg;
                        break;
                case 'E':
                        if (strcmp(optarg, "auto") == 0)
@@ -969,13 +975,13 @@ int main(int argc, char **argv)
                shname = shname ? shname + 1 : shell;
 
                if (access(shell, X_OK) == 0) {
-                       if (command)
-                               execl(shell, shname, "-c", command, (char *)NULL);
+                       if (ctl.command)
+                               execl(shell, shname, "-c", ctl.command, (char *)NULL);
                        else
                                execl(shell, shname, "-i", (char *)NULL);
                } else {
-                       if (command)
-                               execlp(shname, "-c", command, (char *)NULL);
+                       if (ctl.command)
+                               execlp(shname, "-c", ctl.command, (char *)NULL);
                        else
                                execlp(shname, "-i", (char *)NULL);
                }
@@ -1010,8 +1016,8 @@ int main(int argc, char **argv)
                        log_info(&ctl, "LINES", "%d", ctl.ttylines);
                }
                log_info(&ctl, "SHELL", "%s", shell);
-               if (command)
-                       log_info(&ctl, "COMMAND", "%s", command);
+               if (ctl.command)
+                       log_info(&ctl, "COMMAND", "%s", ctl.command);
                log_info(&ctl, "TIMING_LOG", "%s", timingfile);
                if (outfile)
                        log_info(&ctl, "OUTPUT_LOG", "%s", outfile);