mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-04-19 09:02:15 +02:00
Be more conservative with stdin.
If no paths are given to ripgrep, only read from stdin if it's a file or a FIFO. In particular, if something like `rg foo < /dev/null` is used, then don't try to read from stdin. Fixes #35, #81
This commit is contained in:
parent
2015c56e8d
commit
ab0d1c1c79
@ -279,7 +279,8 @@ impl RawArgs {
|
|||||||
if self.arg_path.is_empty() {
|
if self.arg_path.is_empty() {
|
||||||
if atty::on_stdin()
|
if atty::on_stdin()
|
||||||
|| self.flag_files
|
|| self.flag_files
|
||||||
|| self.flag_type_list {
|
|| self.flag_type_list
|
||||||
|
|| !atty::stdin_is_readable() {
|
||||||
vec![Path::new("./").to_path_buf()]
|
vec![Path::new("./").to_path_buf()]
|
||||||
} else {
|
} else {
|
||||||
vec![Path::new("-").to_path_buf()]
|
vec![Path::new("-").to_path_buf()]
|
||||||
|
23
src/atty.rs
23
src/atty.rs
@ -4,6 +4,29 @@ from (or to) a terminal. Windows and Unix do this differently, so implement
|
|||||||
both here.
|
both here.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn stdin_is_readable() -> bool {
|
||||||
|
use std::fs::File;
|
||||||
|
use std::os::unix::fs::FileTypeExt;
|
||||||
|
use std::os::unix::io::{FromRawFd, IntoRawFd};
|
||||||
|
use libc;
|
||||||
|
|
||||||
|
let file = unsafe { File::from_raw_fd(libc::STDIN_FILENO) };
|
||||||
|
let md = file.metadata();
|
||||||
|
let _ = file.into_raw_fd();
|
||||||
|
let ft = match md {
|
||||||
|
Err(_) => return false,
|
||||||
|
Ok(md) => md.file_type(),
|
||||||
|
};
|
||||||
|
ft.is_file() || ft.is_fifo()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub fn stdin_is_readable() -> bool {
|
||||||
|
// ???
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub fn on_stdin() -> bool {
|
pub fn on_stdin() -> bool {
|
||||||
use libc;
|
use libc;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user