diff options
author | Alejandro Colomar <alx@kernel.org> | 2023-03-13 00:59:22 +0100 |
---|---|---|
committer | Iker Pedrosa <ikerpedrosam@gmail.com> | 2023-03-28 13:00:38 +0200 |
commit | 1d7d94ed7dc6736b17581d625066114a93252ff5 (patch) | |
tree | 173e93422f500002c31f1a6af9f16ac9894041c1 | |
parent | e27ca530912c9386f2a60656b4a2b713521f82b1 (diff) |
Simplify is_my_tty()
This commit will serve to document why we shouldn't worry about the
truncation in the call to strlcpy(3). Since we have one more byte in
tmptty than in full_tty, truncation will produce a string that is at
least one byte longer than full_tty. Such a string could never compare
equal, so we're actually handling the truncation in a clever way. Maybe
too clever, but that's why I'm documenting it here.
Now, about the simplification itself:
Since we made sure that both full_tty and tmptty are null-terminated, we
can call strcmp(3) instead of strncmp(3). We can also simplify the
return logic avoiding one branch.
Cc: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
-rw-r--r-- | libmisc/utmp.c | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/libmisc/utmp.c b/libmisc/utmp.c index b35382ef..6e074c27 100644 --- a/libmisc/utmp.c +++ b/libmisc/utmp.c @@ -32,7 +32,7 @@ static bool is_my_tty (const char tty[UT_LINESIZE]) { char full_tty[STRLEN("/dev/") + UT_LINESIZE + 1]; /* tmptty shall be bigger than full_tty */ - static char tmptty[sizeof (full_tty)+1]; + static char tmptty[sizeof(full_tty) + 1]; full_tty[0] = '\0'; if (tty[0] != '/') @@ -42,17 +42,15 @@ static bool is_my_tty (const char tty[UT_LINESIZE]) if ('\0' == tmptty[0]) { const char *tname = ttyname (STDIN_FILENO); if (NULL != tname) - (void) strlcpy (tmptty, tname, sizeof tmptty); + (void) strlcpy (tmptty, tname, sizeof(tmptty)); } if ('\0' == tmptty[0]) { (void) puts (_("Unable to determine your tty name.")); exit (EXIT_FAILURE); - } else if (strncmp (full_tty, tmptty, sizeof (tmptty)) != 0) { - return false; - } else { - return true; } + + return strcmp (full_tty, tmptty) == 0; } /* |