Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent fork nightmare with PAGER=batcat #2235

Merged
merged 6 commits into from
Aug 12, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

## Bugfixes

- Prevent fork nightmare with `PAGER=batcat`. See #2235 (@johnmatthiggins)

## Other

- Relaxed glibc requirements on amd64, see #2106 and #2194 (@sharkdp)
Expand Down
20 changes: 14 additions & 6 deletions src/pager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ impl PagerKind {
fn from_bin(bin: &str) -> PagerKind {
use std::path::Path;

match Path::new(bin)
.file_stem()
.map(|s| s.to_string_lossy())
.as_deref()
{
Some("bat") => PagerKind::Bat,
// Set to `less` by default on most Linux distros.
let pager_bin = Path::new(bin).file_stem();

// The name of the current running binary. Normally `bat` but sometimes
// `batcat` for compatibility reasons.
let current_bin = env::args_os().next();

// Check if the current running binary is set to be our pager.
let is_current_bin_pager = current_bin
.map(|s| Path::new(&s).file_stem() == pager_bin)
.unwrap_or(false);

match pager_bin.map(|s| s.to_string_lossy()).as_deref() {
Some("less") => PagerKind::Less,
Some("more") => PagerKind::More,
Some("most") => PagerKind::Most,
_ if is_current_bin_pager => PagerKind::Bat,
_ => PagerKind::Unknown,
}
}
Expand Down