From e73e29d39cb7ab77fbe566e2b348b53d1d3fb0d3 Mon Sep 17 00:00:00 2001 From: John Higgins Date: Thu, 30 Jun 2022 21:23:29 -0700 Subject: [PATCH 1/6] Added rsamuelklatchko's changes --- src/pager.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/pager.rs b/src/pager.rs index 3473aa67da..b8a8b167cd 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -40,16 +40,24 @@ 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() - { + let file_stem = Path::new(bin).file_stem(); + + match file_stem.map(|s| s.to_string_lossy()).as_deref() { Some("bat") => PagerKind::Bat, Some("less") => PagerKind::Less, Some("more") => PagerKind::More, Some("most") => PagerKind::Most, - _ => PagerKind::Unknown, + _ => { + if env::args_os() + .nth(0) + .map(|arg0| Path::new(&arg0).file_stem() == file_stem) + .unwrap_or(false) + { + PagerKind::Bat + } else { + PagerKind::Unknown + } + } } } } From 6fc3f3cd61f6694f55def088baa2c1065bcfc46a Mon Sep 17 00:00:00 2001 From: John Higgins Date: Sat, 2 Jul 2022 01:55:11 -0700 Subject: [PATCH 2/6] Added some comments and deleted redundant code --- src/pager.rs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/pager.rs b/src/pager.rs index b8a8b167cd..8efd601728 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -39,24 +39,29 @@ pub(crate) enum PagerKind { impl PagerKind { fn from_bin(bin: &str) -> PagerKind { use std::path::Path; - - let file_stem = Path::new(bin).file_stem(); - - match file_stem.map(|s| s.to_string_lossy()).as_deref() { - Some("bat") => PagerKind::Bat, - Some("less") => PagerKind::Less, - Some("more") => PagerKind::More, - Some("most") => PagerKind::Most, - _ => { - if env::args_os() - .nth(0) - .map(|arg0| Path::new(&arg0).file_stem() == file_stem) - .unwrap_or(false) - { - PagerKind::Bat - } else { - PagerKind::Unknown - } + + // 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(|arg0| Path::new(&arg0).file_stem() == pager_bin) + .unwrap_or(false); + + if is_current_bin_pager { + PagerKind::Bat + } + else { + match pager_bin.map(|s| s.to_string_lossy()).as_deref() { + Some("less") => PagerKind::Less, + Some("more") => PagerKind::More, + Some("most") => PagerKind::Most, + _ => PagerKind::Unknown } } } From a1a6f53fc1917a3ce2ae3732edf007448e8ae9b4 Mon Sep 17 00:00:00 2001 From: John Higgins Date: Sat, 2 Jul 2022 01:57:28 -0700 Subject: [PATCH 3/6] Ran cargo fmt --- src/pager.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/pager.rs b/src/pager.rs index 8efd601728..e49c02399e 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -39,29 +39,27 @@ pub(crate) enum PagerKind { impl PagerKind { fn from_bin(bin: &str) -> PagerKind { use std::path::Path; - + // 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(); - + 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(|arg0| Path::new(&arg0).file_stem() == pager_bin) .unwrap_or(false); - + if is_current_bin_pager { PagerKind::Bat - } - else { + } else { match pager_bin.map(|s| s.to_string_lossy()).as_deref() { Some("less") => PagerKind::Less, Some("more") => PagerKind::More, Some("most") => PagerKind::Most, - _ => PagerKind::Unknown + _ => PagerKind::Unknown, } } } From 930aa125a6efd17f9e256890700e790d7f05b918 Mon Sep 17 00:00:00 2001 From: John Higgins <76265228+johnmatthiggins@users.noreply.github.com> Date: Sat, 2 Jul 2022 16:08:15 -0700 Subject: [PATCH 4/6] Update src/pager.rs Co-authored-by: Martin Nordholts --- src/pager.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/pager.rs b/src/pager.rs index e49c02399e..6cf8910b65 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -52,15 +52,13 @@ impl PagerKind { .map(|arg0| Path::new(&arg0).file_stem() == pager_bin) .unwrap_or(false); - if is_current_bin_pager { - PagerKind::Bat - } else { - match pager_bin.map(|s| s.to_string_lossy()).as_deref() { - Some("less") => PagerKind::Less, - Some("more") => PagerKind::More, - Some("most") => PagerKind::Most, - _ => PagerKind::Unknown, - } + 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, + } } } } From f899158d608dce1d35d1b1b4bb14de4ae8937925 Mon Sep 17 00:00:00 2001 From: John Higgins Date: Sat, 2 Jul 2022 16:08:48 -0700 Subject: [PATCH 5/6] Added bugfix to changelog --- CHANGELOG.md | 2 ++ src/pager.rs | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f250ce336..2926467301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/pager.rs b/src/pager.rs index 6cf8910b65..c6b6c3e46a 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -59,7 +59,6 @@ impl PagerKind { _ if is_current_bin_pager => PagerKind::Bat, _ => PagerKind::Unknown, } - } } } From d0eab78cc3cd02deb7678654c06d8f216fbc47d6 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Sun, 3 Jul 2022 14:44:51 +0200 Subject: [PATCH 6/6] src/pager.rs nitpick: arg0 -> s I forgot to comment on this name so I figured I'd just push a commit to take care of it. --- src/pager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pager.rs b/src/pager.rs index c6b6c3e46a..5b70777925 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -49,7 +49,7 @@ impl PagerKind { // Check if the current running binary is set to be our pager. let is_current_bin_pager = current_bin - .map(|arg0| Path::new(&arg0).file_stem() == pager_bin) + .map(|s| Path::new(&s).file_stem() == pager_bin) .unwrap_or(false); match pager_bin.map(|s| s.to_string_lossy()).as_deref() {