]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: add replace_time_fmt() and replace_delta_fmt() helpers
authorKarel Zak <kzak@redhat.com>
Thu, 12 Mar 2026 12:04:35 +0000 (13:04 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 12 Mar 2026 12:04:35 +0000 (13:04 +0100)
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 <kzak@redhat.com>
sys-utils/dmesg.c

index 5496bdbe4fdfa611fd92e4e77affa5ff814b6278..1440dd4e4c34ee6b8d81be8a2c81d38f6337fb69 100644 (file)
@@ -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     ::= <number> | <name>
  *  <number> ::= @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)