|
8 | 8 | * file that was distributed with this source code.
|
9 | 9 | */
|
10 | 10 |
|
11 |
| -// spell-checker:ignore (ToDO) stdlib,ctermid |
12 |
| -// spell-checker:ignore (options) GETFD EPERM ENOSYS ISCHR,ISREG,ISFIFO |
| 11 | +// spell-checker:ignore (ToDO) stdlib |
| 12 | +// spell-checker:ignore (options) GETFD EPERM ENOSYS |
13 | 13 |
|
14 | 14 | use std::io::{stdin, Error};
|
15 | 15 |
|
16 | 16 | use std::os::unix::prelude::AsRawFd;
|
17 | 17 |
|
18 |
| -use libc::{S_IFIFO, S_IFREG}; |
19 | 18 | use nix::sys::stat::fstat;
|
| 19 | + |
| 20 | +use libc::{S_IFIFO, S_IFSOCK}; |
| 21 | + |
20 | 22 | pub type Pid = libc::pid_t;
|
21 | 23 |
|
22 | 24 | pub struct ProcessChecker {
|
@@ -49,20 +51,17 @@ fn get_errno() -> i32 {
|
49 | 51 | }
|
50 | 52 |
|
51 | 53 | pub fn stdin_is_pipe_or_fifo() -> bool {
|
52 |
| - // courtesy of Stack Overflow community https://stackoverflow.com/a/7601564 (08/07/2022). |
53 |
| - // cmd\method ctermid open isatty fstat |
54 |
| - // ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― |
55 |
| - // ./test /dev/tty OK YES S_ISCHR |
56 |
| - // ./test < test.cc /dev/tty OK NO S_ISREG |
57 |
| - // cat test.cc | ./test /dev/tty OK NO S_ISFIFO |
58 |
| - // echo ./test | at now /dev/tty FAIL NO S_ISREG |
59 |
| - |
60 |
| - match fstat(libc::STDIN_FILENO) { |
61 |
| - Ok(m) => { |
62 |
| - let mode= m.st_mode as libc::mode_t; |
63 |
| - (mode & S_IFIFO != 0) || (mode & S_IFREG != 0) }, |
64 |
| - Err(err) => panic!("{}",err) |
65 |
| - } |
| 54 | + let fd = libc::STDIN_FILENO; |
| 55 | + // GNU tail checks fd >= 0 |
| 56 | + fd >= 0 |
| 57 | + && match fstat(fd) { |
| 58 | + Ok(stat) => { |
| 59 | + let mode = stat.st_mode as libc::mode_t; |
| 60 | + // NOTE: This is probably not the most correct way to check this |
| 61 | + (mode & S_IFIFO != 0) || (mode & S_IFSOCK != 0) |
| 62 | + } |
| 63 | + Err(err) => panic!("{}", err), |
| 64 | + } |
66 | 65 | }
|
67 | 66 |
|
68 | 67 | // FIXME: Detect a closed file descriptor, e.g.: `tail <&-`
|
|
0 commit comments