Skip to content

Commit 904ccb5

Browse files
authored
Merge pull request #475 from rustcoreutils/copilot/fix-unsafe-libc-calls
Replace unsafe libc::isatty with safe Rust IsTerminal trait
2 parents 966c3d0 + c38c6f0 commit 904ccb5

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

misc/test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//
99

1010
use std::ffi::CString;
11+
use std::io::IsTerminal;
12+
use std::os::fd::BorrowedFd;
1113
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
1214
use std::path::Path;
1315

@@ -156,17 +158,15 @@ fn eval_unary_path(op: &UnaryOp, s: &str) -> bool {
156158
}
157159

158160
fn eval_terminal(s: &str) -> bool {
159-
let fd = match s.parse::<u32>() {
161+
let fd = match s.parse::<i32>() {
160162
Ok(f) => f,
161163
Err(_) => {
162164
return false;
163165
}
164166
};
165167

166-
// Normally, posixutils would use the atty crate.
167-
// Passing an arbitrary fd requires unsafe isatty in this case.
168-
169-
unsafe { libc::isatty(fd as i32) == 1 }
168+
// Use safe Rust IsTerminal trait with BorrowedFd
169+
unsafe { BorrowedFd::borrow_raw(fd).is_terminal() }
170170
}
171171

172172
fn eval_unary(op_str: &str, s: &str) -> bool {

0 commit comments

Comments
 (0)