From b8bb1c78ae85498cf4ed987721670612437ada2e Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 15 May 2024 22:16:36 +0200 Subject: [PATCH] Rewrite slack_get_is_localtime with ifstreams --- slack-timedate.cpp | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/slack-timedate.cpp b/slack-timedate.cpp index 607736f..3f8b2d8 100644 --- a/slack-timedate.cpp +++ b/slack-timedate.cpp @@ -16,7 +16,9 @@ * */ +#include #include +#include #include #include #include @@ -215,24 +217,26 @@ gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) return clock_settime (CLOCK_REALTIME, &ts) == 0; } -gboolean slack_get_is_localtime () { - FILE *fh; - char time_str[10]; // "localtime" is longer than "UTC" and has 9 letters + \0 - gboolean is_localtime; +gboolean slack_get_is_localtime() +{ + std::ifstream fh{ "/etc/hardwareclock", std::ios::in }; + if (!fh.is_open()) + { + return false; + } + std::array time_str; // "localtime" is longer than "UTC" and has 9 letters + \0 - is_localtime = FALSE; + while (fh.good()) + { + fh.getline(time_str.data(), time_str.size()); + fh.clear(fh.rdstate() & (~std::ios_base::failbit)); - fh = fopen ("/etc/hardwareclock", "r"); - if (fh == NULL) return FALSE; - while (fgets (time_str, 10, fh)) { - if (!g_strcmp0 (time_str, "localtime")) { - is_localtime = TRUE; - break; - } - } - - fclose (fh); - return is_localtime; + if (std::strncmp(time_str.data(), "localtime", time_str.size()) == 0) + { + return true; + } + } + return false; } gboolean slack_get_ntp()