From: Karel Zak Date: Thu, 12 Mar 2026 11:52:33 +0000 (+0100) Subject: dmesg: remove delta variable, use time format list consistently X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff93403809d31ee4152c8bed3272a65f683276a1;p=thirdparty%2Futil-linux.git dmesg: remove delta variable, use time format list consistently 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 --- diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index ce05e5bfd..f4ff3e2c5 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -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); } }