From 49a1995049546ffd943953c320c8409082e3d167 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 4 Jul 2024 18:47:58 +0200 Subject: [PATCH] lib/, src/: Use strncmp(3) instead of explicit byte comparisons This is simpler to read, IMO. Signed-off-by: Alejandro Colomar --- lib/utmp.c | 12 ++++++------ src/login.c | 2 +- src/logoutd.c | 5 +++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/utmp.c b/lib/utmp.c index 4aa2a9096..43cd6aac1 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -210,7 +210,7 @@ get_session_host(char **out, pid_t main_pid) ut = get_current_utmp(main_pid); #if defined(HAVE_STRUCT_UTMPX_UT_HOST) - if ((ut != NULL) && (ut->ut_host[0] != '\0')) { + if ((ut != NULL) && strncmp(ut->ut_host, "", countof(ut->ut_host)) != 0) { *out = XSTRNDUP(ut->ut_host); } else { *out = NULL; @@ -281,7 +281,7 @@ prepare_utmp(const char *name, const char *line, const char *host, if (NULL != host && !streq(host, "")) hostname = xstrdup(host); #if defined(HAVE_STRUCT_UTMPX_UT_HOST) - else if (NULL != ut && '\0' != ut->ut_host[0]) + else if (NULL != ut && strncmp(ut->ut_host, "", countof(ut->ut_host)) != 0) hostname = XSTRNDUP(ut->ut_host); #endif @@ -436,12 +436,12 @@ active_sessions_count(const char *name, unsigned long limit) if (USER_PROCESS != ut->ut_type) { continue; } - if ('\0' == ut->ut_user[0]) { + if (strncmp(ut->ut_user, "", countof(ut->ut_user)) == 0) continue; - } - if (strncmp (name, ut->ut_user, sizeof (ut->ut_user)) != 0) { + + if (strncmp(ut->ut_user, name, countof(ut->ut_user)) != 0) continue; - } + count++; if (count > limit) { break; diff --git a/src/login.c b/src/login.c index 7b567aae8..aff812e2e 100644 --- a/src/login.c +++ b/src/login.c @@ -1200,7 +1200,7 @@ int main (int argc, char **argv) printf (_("Last login: %s on %s"), ptime, ll.ll_line); #ifdef HAVE_LL_HOST /* __linux__ || SUN4 */ - if ('\0' != ll.ll_host[0]) { + if (strncmp(ll.ll_host, "", countof(ll.ll_host)) != 0) { printf (_(" from %.*s"), (int) sizeof ll.ll_host, ll.ll_host); } diff --git a/src/logoutd.c b/src/logoutd.c index 3efcd8468..fc08fa1fd 100644 --- a/src/logoutd.c +++ b/src/logoutd.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -178,9 +179,9 @@ main(int argc, char **argv) if (ut->ut_type != USER_PROCESS) { continue; } - if (ut->ut_user[0] == '\0') { + if (strncmp(ut->ut_user, "", countof(ut->ut_user)) == 0) continue; - } + if (check_login (ut)) { continue; } -- 2.47.3