]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use strncmp(3) instead of explicit byte comparisons
authorAlejandro Colomar <alx@kernel.org>
Thu, 4 Jul 2024 16:47:58 +0000 (18:47 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 18 Oct 2025 19:08:47 +0000 (14:08 -0500)
This is simpler to read, IMO.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/utmp.c
src/login.c
src/logoutd.c

index 4aa2a9096be0f8ce51cb0ea098135ec570034b43..43cd6aac1f6bec31bac8c5548fff2f52e7aaf736 100644 (file)
@@ -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;
index 7b567aae8ce53462e9a61194ec1d6c63f999e608..aff812e2e10c3d984a766161a5c33f3630158479 100644 (file)
@@ -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);
                        }
index 3efcd8468c815b345ff47ad03957abf052569265..fc08fa1fd50b34b0a596867f48659f783084558b 100644 (file)
@@ -13,6 +13,7 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <utmpx.h>
@@ -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;
                        }