Skip to content

Commit b801b0a

Browse files
committed
libc first
1 parent 98166d2 commit b801b0a

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed

crates/common/src/fileutils.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,22 +460,16 @@ pub fn fopen(path: &std::path::Path, mode: &str) -> std::io::Result<*mut libc::F
460460
{
461461
use std::os::windows::io::IntoRawHandle;
462462

463-
// Declare Windows CRT functions
464-
unsafe extern "C" {
465-
fn _open_osfhandle(handle: isize, flags: libc::c_int) -> libc::c_int;
466-
fn _fdopen(fd: libc::c_int, mode: *const libc::c_char) -> *mut libc::FILE;
467-
}
468-
469463
// Convert File handle to CRT file descriptor
470464
let handle = file.into_raw_handle();
471-
let fd = unsafe { _open_osfhandle(handle as isize, libc::O_RDONLY) };
465+
let fd = unsafe { libc::open_osfhandle(handle as isize, libc::O_RDONLY) };
472466
if fd == -1 {
473467
return Err(std::io::Error::last_os_error());
474468
}
475469

476470
// Convert fd to FILE*
477471
let mode_cstr = CString::new(mode).unwrap();
478-
let fp = unsafe { _fdopen(fd, mode_cstr.as_ptr()) };
472+
let fp = unsafe { libc::fdopen(fd, mode_cstr.as_ptr()) };
479473
if fp.is_null() {
480474
unsafe { libc::close(fd) };
481475
return Err(std::io::Error::last_os_error());

crates/vm/src/stdlib/msvcrt.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ mod msvcrt {
8888
}
8989
}
9090

91-
unsafe extern "C" {
92-
fn _open_osfhandle(osfhandle: isize, flags: i32) -> i32;
93-
}
94-
9591
#[pyfunction]
9692
fn open_osfhandle(handle: isize, flags: i32, vm: &VirtualMachine) -> PyResult<i32> {
97-
let ret = unsafe { suppress_iph!(_open_osfhandle(handle, flags)) };
93+
let ret = unsafe { suppress_iph!(libc::open_osfhandle(handle, flags)) };
9894
if ret == -1 {
9995
Err(errno_err(vm))
10096
} else {

crates/vm/src/stdlib/nt.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,6 @@ pub(crate) mod module {
833833

834834
unsafe extern "C" {
835835
fn _umask(mask: i32) -> i32;
836-
fn _dup(fd: i32) -> i32;
837-
fn _dup2(fd: i32, fd2: i32) -> i32;
838-
fn _open_osfhandle(osfhandle: intptr_t, flags: i32) -> i32;
839836
}
840837

841838
/// Close fd and convert error to PyException (PEP 446 cleanup)
@@ -877,8 +874,8 @@ pub(crate) mod module {
877874
// Convert handles to file descriptors
878875
// O_NOINHERIT = 0x80 (MSVC CRT)
879876
const O_NOINHERIT: i32 = 0x80;
880-
let read_fd = unsafe { _open_osfhandle(read_handle, O_NOINHERIT) };
881-
let write_fd = unsafe { _open_osfhandle(write_handle, libc::O_WRONLY | O_NOINHERIT) };
877+
let read_fd = unsafe { libc::open_osfhandle(read_handle, O_NOINHERIT) };
878+
let write_fd = unsafe { libc::open_osfhandle(write_handle, libc::O_WRONLY | O_NOINHERIT) };
882879

883880
if read_fd == -1 || write_fd == -1 {
884881
unsafe {
@@ -964,7 +961,7 @@ pub(crate) mod module {
964961

965962
#[pyfunction]
966963
fn dup(fd: i32, vm: &VirtualMachine) -> PyResult<i32> {
967-
let fd2 = unsafe { suppress_iph!(_dup(fd)) };
964+
let fd2 = unsafe { suppress_iph!(libc::dup(fd)) };
968965
if fd2 < 0 {
969966
return Err(errno_err(vm));
970967
}
@@ -987,7 +984,7 @@ pub(crate) mod module {
987984

988985
#[pyfunction]
989986
fn dup2(args: Dup2Args, vm: &VirtualMachine) -> PyResult<i32> {
990-
let result = unsafe { suppress_iph!(_dup2(args.fd, args.fd2)) };
987+
let result = unsafe { suppress_iph!(libc::dup2(args.fd, args.fd2)) };
991988
if result < 0 {
992989
return Err(errno_err(vm));
993990
}

0 commit comments

Comments
 (0)