Skip to content

Commit 6d01535

Browse files
sylvestrecakebaker
authored andcommitted
pwd: ignore non-option arguments, drop getcwd disabling from pwd-long test
1 parent 785860e commit 6d01535

6 files changed

Lines changed: 56 additions & 3 deletions

File tree

src/uu/pwd/locales/en-US.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ pwd-usage = pwd [OPTION]...
55
pwd-help-logical = use PWD from environment, even if it contains symlinks
66
pwd-help-physical = avoid all symlinks
77
8+
# Warning messages
9+
pwd-ignoring-non-option-arguments = ignoring non-option arguments
10+
811
# Error messages
912
pwd-error-failed-to-get-current-directory = failed to get current directory
1013
pwd-error-failed-to-print-current-directory = failed to print current directory

src/uu/pwd/locales/fr-FR.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ pwd-usage = pwd [OPTION]...
55
pwd-help-logical = utiliser PWD de l'environnement, même s'il contient des liens symboliques
66
pwd-help-physical = éviter tous les liens symboliques
77
8+
# Messages d'avertissement
9+
pwd-ignoring-non-option-arguments = arguments non-option ignorés
10+
811
# Messages d'erreur
912
pwd-error-failed-to-get-current-directory = échec de l'obtention du répertoire actuel
1013
pwd-error-failed-to-print-current-directory = échec de l'affichage du répertoire actuel

src/uu/pwd/src/pwd.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ use uucore::format_usage;
1212

1313
use uucore::display::println_verbatim;
1414
use uucore::error::{FromIo, UResult};
15+
use uucore::show_error;
1516

1617
use uucore::translate;
1718
const OPT_LOGICAL: &str = "logical";
1819
const OPT_PHYSICAL: &str = "physical";
20+
const ARG_OPERANDS: &str = "operands";
1921

2022
fn physical_path() -> io::Result<PathBuf> {
2123
// std::env::current_dir() is a thin wrapper around libc::getcwd().
@@ -110,6 +112,12 @@ fn logical_path() -> io::Result<PathBuf> {
110112
#[uucore::main(no_signals)]
111113
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
112114
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
115+
116+
// GNU pwd ignores any non-option operands but warns about them.
117+
if matches.contains_id(ARG_OPERANDS) {
118+
show_error!("{}", translate!("pwd-ignoring-non-option-arguments"));
119+
}
120+
113121
// if POSIXLY_CORRECT is set, we want to a logical resolution.
114122
// This produces a different output when doing mkdir -p a/b && ln -s a/b c && cd c && pwd
115123
// We should get c in this case instead of a/b at the end of the path
@@ -160,4 +168,10 @@ pub fn uu_app() -> Command {
160168
.help(translate!("pwd-help-physical"))
161169
.action(ArgAction::SetTrue),
162170
)
171+
.arg(
172+
Arg::new(ARG_OPERANDS)
173+
.action(ArgAction::Append)
174+
.num_args(1..)
175+
.hide(true),
176+
)
163177
}

tests/by-util/test_pwd.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ fn test_default() {
2222
}
2323

2424
#[test]
25-
fn test_failed() {
26-
let (_at, mut ucmd) = at_and_ucmd!();
27-
ucmd.arg("will-fail").fails();
25+
fn test_ignores_non_option_arguments() {
26+
// GNU pwd ignores non-option operands, warning on stderr but exiting 0.
27+
let (at, mut ucmd) = at_and_ucmd!();
28+
ucmd.arg("will-fail")
29+
.succeeds()
30+
.stdout_is(at.root_dir_resolved() + "\n")
31+
.stderr_is("pwd: ignoring non-option arguments\n");
2832
}
2933

3034
#[cfg(unix)]

util/gnu-patches/series

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ tests_du_move_dir_while_traversing.patch
1111
test_mkdir_restorecon.patch
1212
error_msg_uniq.diff
1313
tests_numfmt.patch
14+
tests_pwd-long.patch
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Index: gnu/tests/pwd/pwd-long.sh
2+
===================================================================
3+
--- gnu.orig/tests/pwd/pwd-long.sh
4+
+++ gnu/tests/pwd/pwd-long.sh
5+
@@ -19,7 +19,6 @@
6+
7+
. "${srcdir=.}/tests/init.sh";
8+
print_ver_ pwd
9+
-uses_strace_
10+
11+
require_readable_root_
12+
require_perl_
13+
@@ -27,11 +26,10 @@
14+
ARGV_0=$0
15+
export ARGV_0
16+
17+
-# Disable the getcwd syscall if possible, so more of our code is exercised.
18+
-no_sys_getcwd() {
19+
- strace -f -o /dev/null -e 'getcwd' -e fault=all:error=ENOSYS "$@"
20+
-}
21+
-no_sys_getcwd true || no_sys_getcwd() { "$@"; }
22+
+# uutils: our pwd has no userspace getcwd reimplementation like GNU's; it
23+
+# relies on the kernel getcwd(), which already handles paths longer than
24+
+# PATH_MAX. So run pwd directly instead of disabling the getcwd syscall.
25+
+no_sys_getcwd() { "$@"; }
26+
27+
# Don't use CuTmpdir here, since File::Temp's use of rmtree can't
28+
# remove the deep tree we create.

0 commit comments

Comments
 (0)