Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/uu/pwd/src/pwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ fn logical_path() -> io::Result<PathBuf> {
}
}

#[cfg(windows)]
fn strip_windows_verbatim_prefix(path: &str) -> Option<PathBuf> {
if let Some(rest) = path.strip_prefix(r"\\?\UNC\") {
Some(PathBuf::from(format!(r"\\{rest}")))
} else {
path.strip_prefix(r"\\?\").map(PathBuf::from)
}
}

#[uucore::main(no_signals)]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
Expand All @@ -135,11 +144,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// With the right extension trait we can remove it non-lossily, but
// we print it lossily anyway, so no reason to bother.
#[cfg(windows)]
let cwd = cwd
.to_string_lossy()
.strip_prefix(r"\\?\")
.map(Into::into)
.unwrap_or(cwd);
let cwd = {
let path = cwd.to_string_lossy();
strip_windows_verbatim_prefix(&path).unwrap_or(cwd)
};

println_verbatim(cwd)
.map_err_context(|| translate!("pwd-error-failed-to-print-current-directory"))?;
Expand Down
15 changes: 15 additions & 0 deletions src/uu/readlink/src/readlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,22 @@ pub fn uu_app() -> Command {
)
}

#[cfg(windows)]
fn strip_windows_verbatim_prefix(path: &str) -> Option<PathBuf> {
if let Some(rest) = path.strip_prefix(r"\\?\UNC\") {
Some(PathBuf::from(format!(r"\\{rest}")))
} else {
path.strip_prefix(r"\\?\").map(PathBuf::from)
}
}

fn show(path: &Path, line_ending: Option<LineEnding>) -> std::io::Result<()> {
#[cfg(windows)]
let path = {
let path_str = path.to_string_lossy();
strip_windows_verbatim_prefix(&path_str).unwrap_or_else(|| path.to_path_buf())
};

uucore::display::print_verbatim(path)?;
if let Some(line_ending) = line_ending {
write!(stdout(), "{line_ending}")?;
Expand Down