git: 491737689db5 - stable/15 - pwd: Error out if writing to stdout failed

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sun, 15 Feb 2026 08:58:03 UTC
The branch stable/15 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=491737689db5df5f35baa8261c620d6af52b2d00

commit 491737689db5df5f35baa8261c620d6af52b2d00
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-11 02:06:41 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-02-15 08:57:46 +0000

    pwd: Error out if writing to stdout failed
    
    POSIX requires us to print a diagnostic and return a non-zero exit
    code if writing to stdout failed.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D55227
    
    (cherry picked from commit 5b398611607b0dab2f2550ef73f62d41dab6fac5)
---
 bin/pwd/pwd.c             |  2 ++
 bin/pwd/tests/pwd_test.sh | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/bin/pwd/pwd.c b/bin/pwd/pwd.c
index f626a2236c02..2bbf5457ec17 100644
--- a/bin/pwd/pwd.c
+++ b/bin/pwd/pwd.c
@@ -112,5 +112,7 @@ main(int argc, char *argv[])
 		printf("%s\n", pwd);
 	else
 		err(1, ".");
+	if (fflush(stdout) != 0)
+		err(1, "stdout");
 	exit(0);
 }
diff --git a/bin/pwd/tests/pwd_test.sh b/bin/pwd/tests/pwd_test.sh
index e418e56a89fa..a8805582cc4d 100644
--- a/bin/pwd/tests/pwd_test.sh
+++ b/bin/pwd/tests/pwd_test.sh
@@ -66,8 +66,29 @@ physical_body()
 	atf_check -o inline:"$root/phy/baz\n" pwd -L
 }
 
+atf_test_case stdout
+stdout_head()
+{
+	atf_set descr "error writing to stdout"
+}
+stdout_body()
+{
+	pwd=$(which pwd)
+	[ -f "$pwd" ] || atf_skip "unable to distinguish binary from builtin"
+	(
+		trap "" PIPE
+		# Give true(1) some time to exit.
+		sleep 1
+		$pwd 2>stderr
+		echo $? >result
+	) | true
+	atf_check -o inline:"1\n" cat result
+	atf_check -o match:"stdout" cat stderr
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case logical
 	atf_add_test_case physical
+	atf_add_test_case stdout
 }