Skip to content

Commit f20c50c

Browse files
committed
fixup! Rework tail plateform module to tackle uutils#2873
1 parent 731e237 commit f20c50c

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/uu/tail/src/platform/unix.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
* file that was distributed with this source code.
99
*/
1010

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
1313

1414
use std::io::{stdin, Error};
1515

1616
use std::os::unix::prelude::AsRawFd;
1717

18-
use libc::{S_IFIFO, S_IFREG};
1918
use nix::sys::stat::fstat;
19+
20+
use libc::{S_IFIFO, S_IFSOCK};
21+
2022
pub type Pid = libc::pid_t;
2123

2224
pub struct ProcessChecker {
@@ -49,20 +51,17 @@ fn get_errno() -> i32 {
4951
}
5052

5153
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+
}
6665
}
6766

6867
// FIXME: Detect a closed file descriptor, e.g.: `tail <&-`

0 commit comments

Comments
 (0)