]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: remove delta variable, use time format list consistently
authorKarel Zak <kzak@redhat.com>
Thu, 12 Mar 2026 11:52:33 +0000 (12:52 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 12 Mar 2026 11:52:33 +0000 (12:52 +0100)
Replace the separate 'delta' flag variable with direct use of the
time format list for all timestamp-related options:

 -d (--show-delta) now calls include_time_fmt(DMESG_TIMEFTM_TIME_DELTA)
 -t (--notime) now calls include_time_fmt(DMESG_TIMEFTM_NONE) without
    reset_time_fmts(), same as --time-format notime

This makes -d and -t behave the same way as their --time-format
equivalents, storing all requested formats in the format list
for post-getopt resolution.

The delta merging logic is split into two blocks:
 - TIME_DELTA + CTIME → CTIME_DELTA (for -d -T)
 - DELTA + TIME/CTIME → TIME_DELTA/CTIME_DELTA (for --time-format delta)

Addresses: https://github.com/util-linux/util-linux/issues/4110
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/dmesg.c

index ce05e5bfd7bfc9ad446b9d002b9c016f19187993..f4ff3e2c5782a9b3ce7e2cbb7a28d7e6e6c7e037 100644 (file)
@@ -1640,7 +1640,6 @@ int main(int argc, char *argv[])
        int  c, nopager = 0;
        int  console_level = 0;
        int  klog_rc = 0;
-       int  delta = 0;
        ssize_t n;
        static struct dmesg_control ctl = {
                .filename = NULL,
@@ -1733,7 +1732,7 @@ int main(int argc, char *argv[])
                        ctl.action = SYSLOG_ACTION_CONSOLE_OFF;
                        break;
                case 'd':
-                       delta = 1;
+                       include_time_fmt(&ctl, DMESG_TIMEFTM_TIME_DELTA);
                        break;
                case 'E':
                        ctl.action = SYSLOG_ACTION_CONSOLE_ON;
@@ -1811,7 +1810,6 @@ int main(int argc, char *argv[])
                        include_time_fmt(&ctl, DMESG_TIMEFTM_CTIME);
                        break;
                case 't':
-                       reset_time_fmts(&ctl);
                        include_time_fmt(&ctl, DMESG_TIMEFTM_NONE);
                        break;
                case 'u':
@@ -1864,7 +1862,6 @@ int main(int argc, char *argv[])
        if (ctl.json) {
                reset_time_fmts(&ctl);
                ctl.ntime_fmts = 0;
-               delta = 0;
                ctl.force_prefix = 0;
                ctl.raw = 0;
                ctl.noesc = 1;
@@ -1882,17 +1879,26 @@ int main(int argc, char *argv[])
                        ctl.suspended_time = dmesg_get_suspended_time();
        }
 
-       if (delta || is_time_fmt_set(&ctl, DMESG_TIMEFTM_DELTA)) {
+       /* -d uses TIME_DELTA; merge with -T (CTIME) if both specified */
+       if (is_time_fmt_set(&ctl, DMESG_TIMEFTM_TIME_DELTA)
+           && is_time_fmt_set(&ctl, DMESG_TIMEFTM_CTIME)) {
+               exclude_time_fmt(&ctl, DMESG_TIMEFTM_TIME_DELTA);
+               for (n = 0; (size_t) n < ctl.ntime_fmts; n++) {
+                       if (ctl.time_fmts[n] == DMESG_TIMEFTM_CTIME) {
+                               ctl.time_fmts[n] = DMESG_TIMEFTM_CTIME_DELTA;
+                               break;
+                       }
+               }
+       }
+
+       /* --time-format delta: merge with TIME or CTIME if also specified */
+       if (is_time_fmt_set(&ctl, DMESG_TIMEFTM_DELTA)) {
                if (is_time_fmt_set(&ctl, DMESG_TIMEFTM_TIME)) {
-                       if (ctl.ntime_fmts == 0) {
-                               ctl.time_fmts[ctl.ntime_fmts++] = DMESG_TIMEFTM_TIME_DELTA;
-                       } else {
-                               exclude_time_fmt(&ctl, DMESG_TIMEFTM_DELTA);
-                               for (n = 0; (size_t) n < ctl.ntime_fmts; n++) {
-                                       if (ctl.time_fmts[n] == DMESG_TIMEFTM_TIME) {
-                                               ctl.time_fmts[n] = DMESG_TIMEFTM_TIME_DELTA;
-                                               break;
-                                       }
+                       exclude_time_fmt(&ctl, DMESG_TIMEFTM_DELTA);
+                       for (n = 0; (size_t) n < ctl.ntime_fmts; n++) {
+                               if (ctl.time_fmts[n] == DMESG_TIMEFTM_TIME) {
+                                       ctl.time_fmts[n] = DMESG_TIMEFTM_TIME_DELTA;
+                                       break;
                                }
                        }
                } else if (is_time_fmt_set(&ctl, DMESG_TIMEFTM_CTIME)) {
@@ -1903,8 +1909,6 @@ int main(int argc, char *argv[])
                                        break;
                                }
                        }
-               } else {
-                       include_time_fmt(&ctl, DMESG_TIMEFTM_DELTA);
                }
        }