From: Karel Zak Date: Thu, 12 Mar 2026 12:04:35 +0000 (+0100) Subject: dmesg: add replace_time_fmt() and replace_delta_fmt() helpers X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a2a8c7ad667aff5e70871ccbdd1cd0502f3fd49e;p=thirdparty%2Futil-linux.git dmesg: add replace_time_fmt() and replace_delta_fmt() helpers Introduce replace_time_fmt() to replace one time format with another in the format list, and replace_delta_fmt() which combines exclude_time_fmt() + replace_time_fmt() into a single call. This deduplicates the for-loop pattern repeated in the delta merging post-processing. Signed-off-by: Karel Zak --- diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index 5496bdbe4..1440dd4e4 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -423,6 +423,27 @@ static void exclude_time_fmt(struct dmesg_control *ctl, unsigned int time_format } } +static void replace_time_fmt(struct dmesg_control *ctl, + unsigned int old_fmt, unsigned int new_fmt) +{ + size_t idx; + + for (idx = 0; idx < ctl->ntime_fmts; idx++) { + if (ctl->time_fmts[idx] == old_fmt) { + ctl->time_fmts[idx] = new_fmt; + break; + } + } +} + +static void replace_delta_fmt(struct dmesg_control *ctl, + unsigned int delta_fmt, + unsigned int old_fmt, unsigned int new_fmt) +{ + exclude_time_fmt(ctl, delta_fmt); + replace_time_fmt(ctl, old_fmt, new_fmt); +} + /* * LEVEL ::= | * ::= @len is set: number in range <0..N>, where N < ARRAY_SIZE(level_names) @@ -1899,35 +1920,18 @@ int main(int argc, char *argv[]) /* -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; - } - } - } + && is_time_fmt_set(&ctl, DMESG_TIMEFTM_CTIME)) + replace_delta_fmt(&ctl, DMESG_TIMEFTM_TIME_DELTA, + DMESG_TIMEFTM_CTIME, DMESG_TIMEFTM_CTIME_DELTA); /* --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)) { - 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)) { - exclude_time_fmt(&ctl, DMESG_TIMEFTM_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; - } - } - } + if (is_time_fmt_set(&ctl, DMESG_TIMEFTM_TIME)) + replace_delta_fmt(&ctl, DMESG_TIMEFTM_DELTA, + DMESG_TIMEFTM_TIME, DMESG_TIMEFTM_TIME_DELTA); + else if (is_time_fmt_set(&ctl, DMESG_TIMEFTM_CTIME)) + replace_delta_fmt(&ctl, DMESG_TIMEFTM_DELTA, + DMESG_TIMEFTM_CTIME, DMESG_TIMEFTM_CTIME_DELTA); } if (!ctl.json)