From c9a21ec3173b93de4839e5ff9eddadb020431656 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Sat, 31 Dec 2022 17:03:39 +0000
Subject: [PATCH] stty: fix off by one column wrapping on output

* src/stty.c (wrapf): Adjust the comparison by 1,
to account for the space we're adding.
* tests/misc/stty.sh: Add a test case.
* NEWS: Mention the fix.
Reported in https://bugs.debian.org/1027442

Refernece:https://github.com/coreutils/coreutils/commit/c9a21ec3173b93de4839e5ff9eddadb020431656
Conflict:NEWS Context adapation

---
 NEWS               | 4 ++++
 src/stty.c         | 2 +-
 tests/misc/stty.sh | 6 ++++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 805f012..9d3f253 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,10 @@ GNU coreutils NEWS
 * Noteworthy changes in release 9.0 (2021-09-24) [stable]

 ** Bug fixes
+  stty now wraps output appropriately for the terminal width.
+  Previously it may have output 1 character too wide for certain widths.
+  [bug introduced in coreutils-5.3]
+
   stty ispeed and ospeed options no longer accept and silently ignore
   invalid speed arguments.  Now they're validated against both the
   general accepted set, and the system supported set of valid speeds.
diff --git a/src/stty.c b/src/stty.c
index b4c2cbecd..f3c7915e1 100644
--- a/src/stty.c
+++ b/src/stty.c
@@ -519,7 +519,7 @@ wrapf (char const *message,...)
 
   if (0 < current_col)
     {
-      if (max_col - current_col < buflen)
+      if (max_col - current_col <= buflen)
         {
           putchar ('\n');
           current_col = 0;
diff --git a/tests/misc/stty.sh b/tests/misc/stty.sh
index bcdc80e87..7abcec5af 100755
--- a/tests/misc/stty.sh
+++ b/tests/misc/stty.sh
@@ -89,4 +89,10 @@ returns_ 1 strace -o log2 -e ioctl stty -blahblah || fail=1
 n_ioctl2=$(wc -l < log2) || framework_failure_
 test "$n_ioctl1" = "$n_ioctl2" || fail=1
 
+# Ensure we wrap output appropriately
+for W in $(seq 80 90); do
+  output_width=$(COLUMNS="$W" stty -a | wc -L)
+  test "$output_width" -le "$W" || fail=1
+done
+
 Exit $fail
-- 
2.27.0

