From 826c19d3dc416244267d3d21f7a901a7163d7d22 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 12 Aug 2023 19:01:56 +0200 Subject: [PATCH] readutmp: Reduce code duplication. * lib/readutmp.c (have_boot_time): New function. (read_utmp_from_file): Invoke it, instead of duplicating the same loop. --- ChangeLog | 6 +++ lib/readutmp.c | 108 +++++++++++++++++++++---------------------------- 2 files changed, 52 insertions(+), 62 deletions(-) diff --git a/ChangeLog b/ChangeLog index d407d7b4c6..f1db83be86 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2023-08-12 Bruno Haible + + readutmp: Reduce code duplication. + * lib/readutmp.c (have_boot_time): New function. + (read_utmp_from_file): Invoke it, instead of duplicating the same loop. + 2023-08-12 Paul Eggert c-file-type: new module diff --git a/lib/readutmp.c b/lib/readutmp.c index 9a5b34054a..d3ed04e242 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -297,6 +297,20 @@ finish_utmp (struct utmp_alloc a) return a; } +/* Determine whether A already contains an entry of type BOOT_TIME. */ +_GL_ATTRIBUTE_MAYBE_UNUSED +static bool +have_boot_time (struct utmp_alloc a) +{ + for (idx_t i = 0; i < a.filled; i++) + { + struct gl_utmp *ut = &a.utmp[i]; + if (UT_TYPE_BOOT_TIME (ut)) + return true; + } + return false; +} + # if !HAVE_UTMPX_H && HAVE_UTMP_H && defined UTMP_NAME_FUNCTION && !HAVE_DECL_GETUTENT struct utmp *getutent (void); # endif @@ -411,7 +425,6 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0 && file_is_utmp) { - bool have_boot_time = false; for (idx_t i = 0; i < a.filled; i++) { struct gl_utmp *ut = &a.utmp[i]; @@ -420,11 +433,10 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, /* Workaround for Raspbian: */ if (ut->ut_ts.tv_sec <= 60 && runlevel_ts.tv_sec != 0) ut->ut_ts = runlevel_ts; - have_boot_time = true; break; } } - if (!have_boot_time) + if (!have_boot_time (a)) { /* Workaround for Alpine Linux: */ struct timespec boot_time; @@ -445,29 +457,17 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, it produces wrong values after the date has been bumped in the running system. */ if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0 - && strcmp (file, UTMP_FILE) == 0) + && strcmp (file, UTMP_FILE) == 0 + && !have_boot_time (a)) { - bool have_boot_time = false; - for (idx_t i = 0; i < a.filled; i++) - { - struct gl_utmp *ut = &a.utmp[i]; - if (UT_TYPE_BOOT_TIME (ut)) - { - have_boot_time = true; - break; - } - } - if (!have_boot_time) - { - struct timespec boot_time; - if (get_android_boot_time (&boot_time) >= 0) - a = add_utmp (a, options, - "reboot", strlen ("reboot"), - "", 0, - "", 0, - "", 0, - 0, BOOT_TIME, boot_time, 0, 0, 0); - } + struct timespec boot_time; + if (get_android_boot_time (&boot_time) >= 0) + a = add_utmp (a, options, + "reboot", strlen ("reboot"), + "", 0, + "", 0, + "", 0, + 0, BOOT_TIME, boot_time, 0, 0, 0); } # endif @@ -557,21 +557,17 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, # if defined __OpenBSD__ if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0 - && strcmp (file, UTMP_FILE) == 0) + && strcmp (file, UTMP_FILE) == 0 + && !have_boot_time (a)) { - /* OpenBSD's 'struct utmp' does not have an ut_type field. */ - bool have_boot_time = false; - if (!have_boot_time) - { - struct timespec boot_time; - if (get_openbsd_boot_time (&boot_time) >= 0) - a = add_utmp (a, options, - "reboot", strlen ("reboot"), - "", 0, - "", 0, - "", 0, - 0, BOOT_TIME, boot_time, 0, 0, 0); - } + struct timespec boot_time; + if (get_openbsd_boot_time (&boot_time) >= 0) + a = add_utmp (a, options, + "reboot", strlen ("reboot"), + "", 0, + "", 0, + "", 0, + 0, BOOT_TIME, boot_time, 0, 0, 0); } # endif @@ -581,29 +577,17 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, && defined CTL_KERN && defined KERN_BOOTTIME \ && !defined __minix if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0 - && strcmp (file, UTMP_FILE) == 0) + && strcmp (file, UTMP_FILE) == 0 + && !have_boot_time (a)) { - bool have_boot_time = false; - for (idx_t i = 0; i < a.filled; i++) - { - struct gl_utmp *ut = &a.utmp[i]; - if (UT_TYPE_BOOT_TIME (ut)) - { - have_boot_time = true; - break; - } - } - if (!have_boot_time) - { - struct timespec boot_time; - if (get_bsd_boot_time_final_fallback (&boot_time) >= 0) - a = add_utmp (a, options, - "reboot", strlen ("reboot"), - "", 0, - "", 0, - "", 0, - 0, BOOT_TIME, boot_time, 0, 0, 0); - } + struct timespec boot_time; + if (get_bsd_boot_time_final_fallback (&boot_time) >= 0) + a = add_utmp (a, options, + "reboot", strlen ("reboot"), + "", 0, + "", 0, + "", 0, + 0, BOOT_TIME, boot_time, 0, 0, 0); } # endif @@ -612,7 +596,7 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, # if defined __CYGWIN__ || defined _WIN32 if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0 && strcmp (file, UTMP_FILE) == 0 - && a.filled == 0) + && !have_boot_time (a)) { struct timespec boot_time; if (get_windows_boot_time (&boot_time) >= 0) -- 2.39.5