* doc/coreutils.texi (pwd invocation): Describe the new quoting.
* src/pwd.c (main): Enable quoting, depending on tty and $QUOTING_STYLE.
* tests/misc/tty-quoting.sh: Add a pwd test case.
* NEWS: Mention the new feature.
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-4L2T78XW.
---
NEWS | 4 ++++
doc/coreutils.texi | 6 ++++++
src/pwd.c | 29 ++++++++++++++++++++++++++---
tests/misc/tty-quoting.sh | 16 +++++++++++++++-
4 files changed, 51 insertions(+), 4 deletions(-)
diff --git a/NEWS b/NEWS
index 779436b5f..5a239b1fa 100644
--- a/NEWS
+++ b/NEWS
@@ -528,6 +528,10 @@ GNU coreutils NEWS -*-
outline -*-
'od --strings' exits promptly upon receiving a write error,
which is significant when processing many or large input files.
+ 'pwd' when outputting to tty, uses shell quoting when required, to more
+ robustly escape presented directory names, honoring $QUOTING_STYLE to
+ adjust or disable quoting.
+
'tail' ensures prompt output when inotify is not being used, and when
initial reads finish, but subsequent reads might block, which was seen
e.g. with fifos on NetBSD 10.
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 477d8f96b..e15921a0e 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -14843,6 +14843,12 @@ precedence. If neither option is given, then this
implementation uses
@option{-P} as the default unless the @env{POSIXLY_CORRECT}
environment variable is set.
+When standard output is a terminal,
+the directory name is quoted using the @samp{shell-escape} style.
+The environment variable @env{QUOTING_STYLE} specifies the quoting style.
+Valid quoting styles are:
+@quotingStyles
+
@mayConflictWithShellBuiltIn{pwd}
@exitstatus
diff --git a/src/pwd.c b/src/pwd.c
index aa75b3d71..949c523ce 100644
--- a/src/pwd.c
+++ b/src/pwd.c
@@ -19,6 +19,7 @@
#include <stdio.h>
#include <sys/types.h>
+#include "argmatch.h" /* argmatch($QUOTING_STYLE). */
#include "system.h"
#include "quote.h"
#include "root-dev-ino.h"
@@ -36,6 +37,9 @@ struct file_name
char *start;
};
+/* Quote the output, using the default quoting style. */
+static bool quote_output;
+
static struct option const longopts[] =
{
{"logical", no_argument, NULL, 'L'},
@@ -315,6 +319,13 @@ logical_getcwd (void)
return NULL;
}
+/* Output the directory name WD, quoting it if appropriate. */
+static void
+print_wd (char const *wd)
+{
+ puts (quote_output ? quoteN (wd) : wd);
+}
+
int
main (int argc, char **argv)
@@ -358,12 +369,24 @@ main (int argc, char **argv)
if (optind < argc)
error (0, 0, _("ignoring non-option arguments"));
+ if (isatty (STDOUT_FILENO))
+ {
+ int qs = getenv_quoting_style ();
+ if (qs < 0)
+ qs = shell_escape_quoting_style;
+ if (qs != literal_quoting_style)
+ {
+ set_quoting_style (NULL, qs);
+ quote_output = true;
+ }
+ }
+
if (logical)
{
char const *wd = logical_getcwd ();
if (wd)
{
- puts (wd);
+ print_wd (wd);
return EXIT_SUCCESS;
}
}
@@ -371,14 +394,14 @@ main (int argc, char **argv)
char *wd = xgetcwd ();
if (wd != NULL)
{
- puts (wd);
+ print_wd (wd);
free (wd);
}
else
{
struct file_name *file_name = file_name_init ();
robust_getcwd (file_name);
- puts (file_name->start);
+ print_wd (file_name->start);
file_name_free (file_name);
}
diff --git a/tests/misc/tty-quoting.sh b/tests/misc/tty-quoting.sh
index b4eee247c..f67a70c8e 100755
--- a/tests/misc/tty-quoting.sh
+++ b/tests/misc/tty-quoting.sh
@@ -17,7 +17,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
-print_ver_ basename cksum dirname du ls readlink realpath printf test
+print_ver_ basename cksum dirname du ls pwd readlink realpath printf test
require_strace_ ioctl
touch 'b ar' || framework_failure_
@@ -81,4 +81,18 @@ for cmd in 'cksum -a sha1 --untagged' 'cksum -a sha1'; do
compare full_quoted full_unquoted && fail=1
done
+# pwd has no operands, so enter a problematic directory through a symlink
+# so that the logical and physical names are both quoted, and differ.
+mkdir 'd ir' &&
+ln -s 'd ir' 's ym' &&
+cd 's ym' || framework_failure_
+for opt in -L -P; do
+ run_tty_ pwd $opt >quoted || fail=1
+ QUOTING_STYLE=literal run_tty_ pwd $opt >unquoted || fail=1
+
+ env printf '%q\n' "$(cat unquoted)" >printf_quoted || framework_failure_
+
+ compare quoted printf_quoted || fail=1
+done
+
Exit $fail
--
2.56.0