| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
use clap::ArgAction; |
| 7 |
use clap::{Arg, Command}; |
| 8 |
use std::env; |
| 9 |
use std::io; |
| 10 |
use std::path::PathBuf; |
| 11 |
use uucore::format_usage; |
| 12 |
|
| 13 |
use uucore::display::println_verbatim; |
| 14 |
use uucore::error::{FromIo, UResult}; |
| 15 |
use uucore::show_error; |
| 16 |
|
| 17 |
use uucore::translate; |
| 18 |
const OPT_LOGICAL: &str = "logical"; |
| 19 |
const OPT_PHYSICAL: &str = "physical"; |
| 20 |
const ARG_OPERANDS: &str = "operands"; |
| 21 |
|
| 22 |
fn physical_path() -> io::Result<PathBuf> { |
| 23 |
// std::env::current_dir() is a thin wrapper around libc::getcwd(). |
| 24 |
let path = env::current_dir()?; |
| 25 |
|
| 26 |
// On Unix, getcwd() must return the physical path: |
| 27 |
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html |
| 28 |
#[cfg(unix)] |
| 29 |
{ |
| 30 |
Ok(path) |
| 31 |
} |
| 32 |
|
| 33 |
// On Windows we have to resolve it. |
| 34 |
// On other systems we also resolve it, just in case. |
| 35 |
#[cfg(not(unix))] |
| 36 |
{ |
| 37 |
path.canonicalize() |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
fn logical_path() -> io::Result<PathBuf> { |
| 42 |
// getcwd() on Windows seems to include symlinks, so this is easy. |
| 43 |
#[cfg(windows)] |
| 44 |
{ |
| 45 |
env::current_dir() |
| 46 |
} |
| 47 |
|
| 48 |
// If we're not on Windows we do things Unix-style. |
| 49 |
// |
| 50 |
// Typical Unix-like kernels don't actually keep track of the logical working |
| 51 |
// directory. They know the precise directory a process is in, and the getcwd() |
| 52 |
// syscall reconstructs a path from that. |
| 53 |
// |
| 54 |
// The logical working directory is maintained by the shell, in the $PWD |
| 55 |
// environment variable. So we check carefully if that variable looks |
| 56 |
// reasonable, and if not then we fall back to the physical path. |
| 57 |
// |
| 58 |
// POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pwd.html |
| 59 |
#[cfg(not(windows))] |
| 60 |
{ |
| 61 |
use std::path::Path; |
| 62 |
fn looks_reasonable(path: &Path) -> bool { |
| 63 |
// First, check if it's an absolute path. |
| 64 |
if !path.has_root() { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
// Then, make sure there are no . or .. components |
| 69 |
// Path::components() isn't useful here, it normalizes those out. |
| 70 |
// Use as_encoded_bytes since . and .. are valid unicode |
| 71 |
|
| 72 |
if path |
| 73 |
.as_os_str() |
| 74 |
.as_encoded_bytes() |
| 75 |
.split(|&b| std::path::is_separator(b as char)) |
| 76 |
.any(|p| p == b"." || p == b"..") |
| 77 |
{ |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
// Finally, check if it matches the directory we're in. |
| 82 |
#[cfg(unix)] |
| 83 |
{ |
| 84 |
use std::fs::metadata; |
| 85 |
use std::os::unix::fs::MetadataExt; |
| 86 |
match (metadata(path), metadata(".")) { |
| 87 |
(Ok(info1), Ok(info2)) => { |
| 88 |
info1.dev() == info2.dev() && info1.ino() == info2.ino() |
| 89 |
} |
| 90 |
_ => false, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[cfg(not(unix))] |
| 95 |
{ |
| 96 |
use std::fs::canonicalize; |
| 97 |
match (canonicalize(path), canonicalize(".")) { |
| 98 |
(Ok(path1), Ok(path2)) => path1 == path2, |
| 99 |
_ => false, |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
match env::var_os("PWD").map(PathBuf::from) { |
| 105 |
Some(value) if looks_reasonable(&value) => Ok(value), |
| 106 |
_ => env::current_dir(), |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[uucore::main(no_signals)] |
| 112 |
pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
| 113 |
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?; |
| 114 |
|
| 115 |
// GNU pwd ignores any non-option operands but warns about them. |
| 116 |
if matches.contains_id(ARG_OPERANDS) { |
| 117 |
show_error!("{}", translate!("pwd-ignoring-non-option-arguments")); |
| 118 |
} |
| 119 |
|
| 120 |
// if POSIXLY_CORRECT is set, we want to a logical resolution. |
| 121 |
// This produces a different output when doing mkdir -p a/b && ln -s a/b c && cd c && pwd |
| 122 |
// We should get c in this case instead of a/b at the end of the path |
| 123 |
let cwd = if matches.get_flag(OPT_PHYSICAL) { |
| 124 |
physical_path() |
| 125 |
} else if matches.get_flag(OPT_LOGICAL) || env::var("POSIXLY_CORRECT").is_ok() { |
| 126 |
logical_path() |
| 127 |
} else { |
| 128 |
physical_path() |
| 129 |
} |
| 130 |
.map_err_context(|| translate!("pwd-error-failed-to-get-current-directory"))?; |
| 131 |
|
| 132 |
// \\?\ is a prefix Windows gives to paths under certain circumstances, |
| 133 |
// including when canonicalizing them. |
| 134 |
// With the right extension trait we can remove it non-lossily, but |
| 135 |
// we print it lossily anyway, so no reason to bother. |
| 136 |
#[cfg(windows)] |
| 137 |
let cwd = cwd |
| 138 |
.to_string_lossy() |
| 139 |
.strip_prefix(r"\\?\") |
| 140 |
.map(Into::into) |
| 141 |
.unwrap_or(cwd); |
| 142 |
|
| 143 |
println_verbatim(cwd) |
| 144 |
.map_err_context(|| translate!("pwd-error-failed-to-print-current-directory"))?; |
| 145 |
Ok(()) |
| 146 |
} |
| 147 |
|
| 148 |
pub fn uu_app() -> Command { |
| 149 |
Command::new("pwd") |
| 150 |
.version(uucore::crate_version!()) |
| 151 |
.help_template(uucore::localized_help_template("pwd")) |
| 152 |
.about(translate!("pwd-about")) |
| 153 |
.override_usage(format_usage(&translate!("pwd-usage"))) |
| 154 |
.infer_long_args(true) |
| 155 |
.arg( |
| 156 |
Arg::new(OPT_LOGICAL) |
| 157 |
.short('L') |
| 158 |
.long(OPT_LOGICAL) |
| 159 |
.help(translate!("pwd-help-logical")) |
| 160 |
.action(ArgAction::SetTrue), |
| 161 |
) |
| 162 |
.arg( |
| 163 |
Arg::new(OPT_PHYSICAL) |
| 164 |
.short('P') |
| 165 |
.long(OPT_PHYSICAL) |
| 166 |
.overrides_with(OPT_LOGICAL) |
| 167 |
.help(translate!("pwd-help-physical")) |
| 168 |
.action(ArgAction::SetTrue), |
| 169 |
) |
| 170 |
.arg( |
| 171 |
Arg::new(ARG_OPERANDS) |
| 172 |
.action(ArgAction::Append) |
| 173 |
.num_args(1..) |
| 174 |
.hide(true), |
| 175 |
) |
| 176 |
} |