aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2026-02-11 02:06:41 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2026-02-11 02:06:56 +0000
commit5b398611607b0dab2f2550ef73f62d41dab6fac5 (patch)
tree504c28fc655b1ce036eead4bc2c93a8533515c3f
parentb02def7c4a10fa3e2b05ebec379c5c634b87484d (diff)
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
-rw-r--r--bin/pwd/pwd.c2
-rw-r--r--bin/pwd/tests/pwd_test.sh21
2 files changed, 23 insertions, 0 deletions
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
}