From 0bdbdbcc0a5919dace8fc0547c4f465ef26edb7e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 20 Jul 2026 21:11:44 +0100 Subject: [PATCH 1/5] Add safe fcntl --- rules/fcntl/tgt_refcount.rs | 31 +- tests/unit/out/refcount/fd_io.rs | 1129 +++++++++++++++++++++++++++++- tests/unit/out/unsafe/fd_io.rs | 333 ++++++++- 3 files changed, 1475 insertions(+), 18 deletions(-) diff --git a/rules/fcntl/tgt_refcount.rs b/rules/fcntl/tgt_refcount.rs index a347dbb3..70e67535 100644 --- a/rules/fcntl/tgt_refcount.rs +++ b/rules/fcntl/tgt_refcount.rs @@ -4,12 +4,31 @@ use libcc2rs::*; fn f1(a0: i32, a1: i32, va: &[VaArg]) -> i32 { - panic!( - "fcntl is not supported in the refcount model (fd={}, cmd={}, varargs={})", - a0, - a1, - va.len() - ) + let __res = match a1 { + ::libc::F_GETFL => FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&va[0])); + FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&va[0])); + FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } } fn f2(a0: Ptr, a1: i32, va: &[VaArg]) -> i32 { diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index a8be4c06..22b4982f 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -6,12 +6,9 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { +pub fn test_open_read_write_0() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_test.tmp", + b"/tmp/cpp2rust_fd_io_rw.tmp", ))); let fd: Value = Rc::new(RefCell::new({ let __mode = match &[(420).into()].first() { @@ -138,5 +135,1127 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); +} +pub fn test_pipe_1() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"ab") + .to_any() + .reinterpret_cast::() + .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ::default()).collect::>(), + )); + { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + }; + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 2_isize) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0_isize) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); +} +pub fn test_socket_listen_2() { + let s: Value = Rc::new(RefCell::new({ + let __family = match libc::AF_INET { + ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, + ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, + ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, + __d => panic!("socket: unsupported domain {__d}"), + }; + let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); + let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { + ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, + ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, + __t => panic!("socket: unsupported type {__t}"), + }; + let __proto = match 0 { + 0 => None, + ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), + ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), + __p => panic!("socket: unsupported protocol {__p}"), + }; + match nix::sys::socket::socket(__family, __ty, __flags, __proto) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*s.borrow()) >= 0) as i32) != 0)); + assert!( + (((match nix::sys::socket::Backlog::new(5) { + Ok(__b) => match FdRegistry::with_fd((*s.borrow()), |__fd| nix::sys::socket::listen( + &__fd, __b + )) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); +} +pub fn test_lseek_3() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_lseek.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello world") + .to_any() + .reinterpret_cast::() + .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 11_isize) as i32) + != 0) + ); + assert!( + ((({ + let __whence = match 2 { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + __w => panic!("lseek: unsupported whence {__w}"), + }; + match FdRegistry::with_fd((*fd.borrow()), |__fd| { + nix::unistd::lseek(__fd, 0_i64, __whence) + }) { + Ok(__off) => __off, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 11_i64) as i32) + != 0) + ); + assert!( + ((({ + let __whence = match 0 { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + __w => panic!("lseek: unsupported whence {__w}"), + }; + match FdRegistry::with_fd((*fd.borrow()), |__fd| { + nix::unistd::lseek(__fd, 6_i64, __whence) + }) { + Ok(__off) => __off, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 6_i64) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new( + (0..16).map(|_| ::default()).collect::>(), + )); + { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + }; + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| { + nix::unistd::read(__fd, __buf) + }) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 5_isize) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"world").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_ftruncate_4() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_trunc.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello world") + .to_any() + .reinterpret_cast::() + .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 11_isize) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::ftruncate(__fd, 5_i64)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let __whence = match 2 { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + __w => panic!("lseek: unsupported whence {__w}"), + }; + match FdRegistry::with_fd((*fd.borrow()), |__fd| { + nix::unistd::lseek(__fd, 0_i64, __whence) + }) { + Ok(__off) => __off, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 5_i64) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_fstat_5() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_fstat.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain( + ((::libc::O_WRONLY | ::libc::O_CREAT) | ::libc::O_TRUNC), + ), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + Ptr::from_string_literal(b"hello") + .to_any() + .reinterpret_cast::() + .with_slice(5_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 5_isize) as i32) + != 0) + ); + let st: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::stat::fstat(__fd)) { + Ok(__s) => { + (st.as_pointer()).with_mut(|__st| *__st = Stat::from_libc(&__s)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); + assert!((((((*(*st.borrow()).st_mode.borrow()) & 61440_u32) == 32768_u32) as i32) != 0)); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_isatty_6() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_tty.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::isatty(__fd)) { + Ok(__tty) => __tty as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_tcgetattr_7() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_fd_io_termios.tmp", + ))); + let fd: Value = Rc::new(RefCell::new({ + let __mode = match &[(420).into()].first() { + Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), + None => nix::sys::stat::Mode::empty(), + }; + match nix::fcntl::open( + (*path.borrow()).to_rust_string().as_str(), + nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= 0) as i32) != 0)); + let tio: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::termios::tcgetattr(__fd)) { + Ok(__t) => { + (tio.as_pointer()).with_mut(|__dst| *__dst = Termios::from_libc(&__t.into())); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == -1_i32) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_fcntl_8() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + let flags: Value = Rc::new(RefCell::new({ + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*flags.borrow()) >= 0) as i32) != 0)); + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) == 0) as i32) != 0)); + assert!( + ((({ + let __res = match 4 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + (*flags.borrow_mut()) = { + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }; + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) != 0) as i32) != 0)); + let b: Value = >::default(); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((b.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + ((({ + let __res = match 2 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn test_select_9() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + let rset: Value = Rc::new(RefCell::new(Default::default())); + (rset.as_pointer()).with_mut(|__s| __s.zero()); + (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); + let tv: Value = Rc::new(RefCell::new(Default::default())); + (*(*tv.borrow()).tv_sec.borrow_mut()) = 0_i64; + (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; + assert!( + ((({ + let __rp = (rset.as_pointer()).clone(); + let __wp = Ptr::::null().clone(); + let __ep = Ptr::::null().clone(); + let __tp = (tv.as_pointer()).clone(); + let __r_fds: Vec = match __rp.is_null() { + true => Vec::new(), + false => __rp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __w_fds: Vec = match __wp.is_null() { + true => Vec::new(), + false => __wp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __e_fds: Vec = match __ep.is_null() { + true => Vec::new(), + false => __ep.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let mut __wanted = Vec::new(); + __wanted.extend(&__r_fds); + __wanted.extend(&__w_fds); + __wanted.extend(&__e_fds); + FdRegistry::with_fds(&__wanted, |__b| { + let __rn = __r_fds.len(); + let __wn = __w_fds.len(); + let mut __rs = nix::sys::select::FdSet::new(); + let mut __ws = nix::sys::select::FdSet::new(); + let mut __es = nix::sys::select::FdSet::new(); + for __bfd in &__b[..__rn] { + __rs.insert(*__bfd); + } + for __bfd in &__b[__rn..__rn + __wn] { + __ws.insert(*__bfd); + } + for __bfd in &__b[__rn + __wn..] { + __es.insert(*__bfd); + } + let mut __tv = match __tp.is_null() { + true => None, + false => Some(__tp.with(|__t| { + nix::sys::time::TimeVal::new( + *__t.tv_sec.borrow() as _, + *__t.tv_usec.borrow() as _, + ) + })), + }; + match nix::sys::select::select( + ((*fds.borrow())[(0) as usize] + 1), + match __rp.is_null() { + true => None, + false => Some(&mut __rs), + }, + match __wp.is_null() { + true => None, + false => Some(&mut __ws), + }, + match __ep.is_null() { + true => None, + false => Some(&mut __es), + }, + __tv.as_mut(), + ) { + Ok(__n) => { + if !__rp.is_null() { + __rp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { + if __rs.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__wp.is_null() { + __wp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { + if __ws.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__ep.is_null() { + __ep.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { + if __es.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + match (__tp.is_null(), __tv.as_ref()) { + (false, Some(__t)) => __tp.with_mut(|__dst| { + *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; + *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; + }), + _ => {} + } + __n + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }) + } == 0) as i32) + != 0) + ); + assert!( + ((!(if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { + 1 + } else { + 0 + } != 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"x") + .to_any() + .reinterpret_cast::() + .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 1_isize) as i32) + != 0) + ); + (rset.as_pointer()).with_mut(|__s| __s.zero()); + (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); + (*(*tv.borrow()).tv_sec.borrow_mut()) = 1_i64; + (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; + assert!( + ((({ + let __rp = (rset.as_pointer()).clone(); + let __wp = Ptr::::null().clone(); + let __ep = Ptr::::null().clone(); + let __tp = (tv.as_pointer()).clone(); + let __r_fds: Vec = match __rp.is_null() { + true => Vec::new(), + false => __rp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __w_fds: Vec = match __wp.is_null() { + true => Vec::new(), + false => __wp.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let __e_fds: Vec = match __ep.is_null() { + true => Vec::new(), + false => __ep.with(|__s| { + (0..((*fds.borrow())[(0) as usize] + 1)) + .filter(|&__fd| __s.isset(__fd)) + .collect() + }), + }; + let mut __wanted = Vec::new(); + __wanted.extend(&__r_fds); + __wanted.extend(&__w_fds); + __wanted.extend(&__e_fds); + FdRegistry::with_fds(&__wanted, |__b| { + let __rn = __r_fds.len(); + let __wn = __w_fds.len(); + let mut __rs = nix::sys::select::FdSet::new(); + let mut __ws = nix::sys::select::FdSet::new(); + let mut __es = nix::sys::select::FdSet::new(); + for __bfd in &__b[..__rn] { + __rs.insert(*__bfd); + } + for __bfd in &__b[__rn..__rn + __wn] { + __ws.insert(*__bfd); + } + for __bfd in &__b[__rn + __wn..] { + __es.insert(*__bfd); + } + let mut __tv = match __tp.is_null() { + true => None, + false => Some(__tp.with(|__t| { + nix::sys::time::TimeVal::new( + *__t.tv_sec.borrow() as _, + *__t.tv_usec.borrow() as _, + ) + })), + }; + match nix::sys::select::select( + ((*fds.borrow())[(0) as usize] + 1), + match __rp.is_null() { + true => None, + false => Some(&mut __rs), + }, + match __wp.is_null() { + true => None, + false => Some(&mut __ws), + }, + match __ep.is_null() { + true => None, + false => Some(&mut __es), + }, + __tv.as_mut(), + ) { + Ok(__n) => { + if !__rp.is_null() { + __rp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { + if __rs.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__wp.is_null() { + __wp.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { + if __ws.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + if !__ep.is_null() { + __ep.with_mut(|__s| { + __s.zero(); + for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { + if __es.contains(*__bfd) { + __s.set(*__fd); + } + } + }); + } + match (__tp.is_null(), __tv.as_ref()) { + (false, Some(__t)) => __tp.with_mut(|__dst| { + *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; + *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; + }), + _ => {} + } + __n + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }) + } == 1) as i32) + != 0) + ); + assert!( + (if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { + 1 + } else { + 0 + } != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn test_poll_10() { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + Ptr::from_string_literal(b"x") + .to_any() + .reinterpret_cast::() + .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 1_isize) as i32) + != 0) + ); + let pfd: Value> = Rc::new(RefCell::new( + (0..2) + .map(|_| Default::default()) + .collect::>(), + )); + (*(*pfd.borrow())[(0) as usize].fd.borrow_mut()) = (*fds.borrow())[(0) as usize]; + (*(*pfd.borrow())[(0) as usize].events.borrow_mut()) = 1_i16; + (*(*pfd.borrow())[(0) as usize].revents.borrow_mut()) = 0_i16; + (*(*pfd.borrow())[(1) as usize].fd.borrow_mut()) = -1_i32; + (*(*pfd.borrow())[(1) as usize].events.borrow_mut()) = 1_i16; + (*(*pfd.borrow())[(1) as usize].revents.borrow_mut()) = 42_i16; + assert!( + ((({ + let __p = (pfd.as_pointer() as Ptr).clone(); + let __timeout = match nix::poll::PollTimeout::try_from(0) { + Ok(__t) => __t, + Err(_) => panic!("poll: unsupported timeout {}", 0), + }; + let mut __idx = Vec::new(); + let mut __wanted = Vec::new(); + let mut __events = Vec::new(); + for __i in 0..(2_u64 as usize) { + let (__fd, __ev) = __p + .offset(__i) + .with(|__e| (*__e.fd.borrow(), *__e.events.borrow())); + __p.offset(__i) + .with_mut(|__e| *__e.revents.borrow_mut() = 0); + if __fd >= 0 { + __idx.push(__i); + __wanted.push(__fd); + __events.push(__ev); + } + } + FdRegistry::with_fds(&__wanted, |__b| { + let mut __pfds: Vec = __b + .iter() + .zip(&__events) + .map(|(&__fd, &__ev)| { + nix::poll::PollFd::new(__fd, nix::poll::PollFlags::from_bits_truncate(__ev)) + }) + .collect(); + match nix::poll::poll(&mut __pfds, __timeout) { + Ok(__count) => { + for (__slot, &__i) in __pfds.iter().zip(&__idx) { + let __rev = match __slot.revents() { + Some(__r) => __r.bits(), + None => 0, + }; + __p.offset(__i) + .with_mut(|__e| *__e.revents.borrow_mut() = __rev); + } + __count + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }) + } == 1) as i32) + != 0) + ); + assert!( + ((((((*(*pfd.borrow())[(0) as usize].revents.borrow()) as i32) & 1) != 0) as i32) != 0) + ); + assert!((((((*(*pfd.borrow())[(1) as usize].revents.borrow()) as i32) == 0) as i32) != 0)); + let ch: Value = >::default(); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((ch.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 1_isize) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_open_read_write_0() }); + ({ test_pipe_1() }); + ({ test_socket_listen_2() }); + ({ test_lseek_3() }); + ({ test_ftruncate_4() }); + ({ test_fstat_5() }); + ({ test_isatty_6() }); + ({ test_tcgetattr_7() }); + ({ test_fcntl_8() }); + ({ test_select_9() }); + ({ test_poll_10() }); return 0; } diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index 29c8057d..1af3e9ce 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -6,14 +6,9 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { +pub unsafe fn test_open_read_write_0() { let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_test.tmp".as_ptr().cast_mut()).cast_const(); + (c"/tmp/cpp2rust_fd_io_rw.tmp".as_ptr().cast_mut()).cast_const(); let mut fd: i32 = (unsafe { libc::open( path as *const i8, @@ -66,5 +61,329 @@ unsafe fn main_0() -> i32 { ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_pipe_1() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 2_usize + )) == (2_isize)) as i32) + != 0) + ); + let mut buf: [libc::c_char; 4] = [(0 as libc::c_char); 4]; + { + let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!( + ((((libc::read( + fds[(0) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (2_isize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf.as_mut_ptr()).cast_const(), + (c"ab".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); + assert!( + ((((libc::read( + fds[(0) as usize], + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 4]>() + )) == (0_isize)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); +} +pub unsafe fn test_socket_listen_2() { + let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); + assert!(((((s) >= (0)) as i32) != 0)); + assert!(((((libc::listen(s, 5)) == (0)) as i32) != 0)); + assert!(((((libc::close(s)) == (0)) as i32) != 0)); +} +pub unsafe fn test_lseek_3() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_lseek.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 11_usize + )) == (11_isize)) as i32) + != 0) + ); + assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); + let mut buf: [libc::c_char; 16] = [(0 as libc::c_char); 16]; + { + let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[libc::c_char; 16]>() { + *byte_0.offset(offset as isize) = 0 as u8; + } + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!( + ((((libc::read( + fd, + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + ::std::mem::size_of::<[libc::c_char; 16]>() + )) == (5_isize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf.as_mut_ptr()).cast_const(), + (c"world".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_ftruncate_4() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_trunc.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 11_usize + )) == (11_isize)) as i32) + != 0) + ); + assert!(((((libc::ftruncate(fd, 5_i64)) == (0)) as i32) != 0)); + assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_fstat_5() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_fstat.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + (((::libc::O_WRONLY) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!( + ((((libc::write( + fd, + (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 5_usize + )) == (5_isize)) as i32) + != 0) + ); + let mut st: ::libc::stat = unsafe { std::mem::zeroed() }; + assert!(((((libc::fstat(fd, (&mut st as *mut ::libc::stat))) == (0)) as i32) != 0)); + assert!(((((st.st_size) == (5_i64)) as i32) != 0)); + assert!((((((st.st_mode) & (61440_u32)) == (32768_u32)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_isatty_6() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_tty.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + assert!(((((libc::isatty(fd)) == (0)) as i32) != 0)); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_tcgetattr_7() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_fd_io_termios.tmp".as_ptr().cast_mut()).cast_const(); + let mut fd: i32 = (unsafe { + libc::open( + path as *const i8, + ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, + (420), + ) + }); + assert!(((((fd) >= (0)) as i32) != 0)); + let mut tio: ::libc::termios = unsafe { std::mem::zeroed() }; + assert!( + ((((libc::tcgetattr(fd, (&mut tio as *mut ::libc::termios))) == (-1_i32)) as i32) != 0) + ); + assert!(((((libc::close(fd)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_fcntl_8() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + let mut flags: i32 = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!(((((flags) >= (0)) as i32) != 0)); + assert!((((((flags) & (::libc::O_NONBLOCK)) == (0)) as i32) != 0)); + assert!( + ((((unsafe { + libc::fcntl( + fds[(0) as usize] as i32, + 4 as i32, + ((flags) | (::libc::O_NONBLOCK)), + ) + }) == (0)) as i32) + != 0) + ); + flags = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!((((((flags) & (::libc::O_NONBLOCK)) != (0)) as i32) != 0)); + let mut b: libc::c_char = (0 as libc::c_char); + assert!( + ((((libc::read( + fds[(0) as usize], + ((&mut b as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), + 1_usize + )) == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + ((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); +} +pub unsafe fn test_select_9() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + let mut rset: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>(); + libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); + libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); + let mut tv: ::libc::timeval = unsafe { std::mem::zeroed() }; + tv.tv_sec = 0_i64; + tv.tv_usec = 0_i64; + assert!( + ((((libc::select( + ((fds[(0) as usize]) + (1)), + (&mut rset as *mut ::libc::fd_set), + std::ptr::null_mut(), + std::ptr::null_mut(), + (&mut tv as *mut ::libc::timeval) + )) == (0)) as i32) + != 0) + ); + assert!( + ((!(libc::FD_ISSET( + fds[(0) as usize], + (&mut rset as *mut ::libc::fd_set).cast_const() + ) as i32 + != 0) as i32) + != 0) + ); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize + )) == (1_isize)) as i32) + != 0) + ); + libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); + libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); + tv.tv_sec = 1_i64; + tv.tv_usec = 0_i64; + assert!( + ((((libc::select( + ((fds[(0) as usize]) + (1)), + (&mut rset as *mut ::libc::fd_set), + std::ptr::null_mut(), + std::ptr::null_mut(), + (&mut tv as *mut ::libc::timeval) + )) == (1)) as i32) + != 0) + ); + assert!( + (libc::FD_ISSET( + fds[(0) as usize], + (&mut rset as *mut ::libc::fd_set).cast_const() + ) as i32 + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); +} +pub unsafe fn test_poll_10() { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + assert!( + ((((libc::write( + fds[(1) as usize], + (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), + 1_usize + )) == (1_isize)) as i32) + != 0) + ); + let mut pfd: [::libc::pollfd; 2] = [unsafe { std::mem::zeroed() }; 2]; + pfd[(0) as usize].fd = fds[(0) as usize]; + pfd[(0) as usize].events = 1_i16; + pfd[(0) as usize].revents = 0_i16; + pfd[(1) as usize].fd = -1_i32; + pfd[(1) as usize].events = 1_i16; + pfd[(1) as usize].revents = 42_i16; + assert!(((((libc::poll(pfd.as_mut_ptr(), 2_u64 as ::libc::nfds_t, 0)) == (1)) as i32) != 0)); + assert!((((((pfd[(0) as usize].revents as i32) & (1)) != (0)) as i32) != 0)); + assert!(((((pfd[(1) as usize].revents as i32) == (0)) as i32) != 0)); + let mut ch: libc::c_char = (0 as libc::c_char); + assert!( + ((((libc::read( + fds[(0) as usize], + ((&mut ch as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), + 1_usize + )) == (1_isize)) as i32) + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_open_read_write_0() }); + (unsafe { test_pipe_1() }); + (unsafe { test_socket_listen_2() }); + (unsafe { test_lseek_3() }); + (unsafe { test_ftruncate_4() }); + (unsafe { test_fstat_5() }); + (unsafe { test_isatty_6() }); + (unsafe { test_tcgetattr_7() }); + (unsafe { test_fcntl_8() }); + (unsafe { test_select_9() }); + (unsafe { test_poll_10() }); return 0; } From 42dd53faafa278007ac29c83a9e14903f5a56af5 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 14:49:56 +0100 Subject: [PATCH 2/5] Add fcntl test --- tests/unit/fcntl.c | 20 + tests/unit/out/refcount/fcntl.rs | 171 +++++ tests/unit/out/refcount/fd_io.rs | 1129 +----------------------------- tests/unit/out/unsafe/fcntl.rs | 47 ++ tests/unit/out/unsafe/fd_io.rs | 333 +-------- 5 files changed, 250 insertions(+), 1450 deletions(-) create mode 100644 tests/unit/fcntl.c create mode 100644 tests/unit/out/refcount/fcntl.rs create mode 100644 tests/unit/out/unsafe/fcntl.rs diff --git a/tests/unit/fcntl.c b/tests/unit/fcntl.c new file mode 100644 index 00000000..b314d1de --- /dev/null +++ b/tests/unit/fcntl.c @@ -0,0 +1,20 @@ +#include +#include +#include + +int main(void) { + int fds[2]; + assert(pipe(fds) == 0); + int flags = fcntl(fds[0], F_GETFL, 0); + assert(flags >= 0); + assert((flags & O_NONBLOCK) == 0); + assert(fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) == 0); + flags = fcntl(fds[0], F_GETFL, 0); + assert((flags & O_NONBLOCK) != 0); + char b; + assert(read(fds[0], &b, 1) == -1); + assert(fcntl(fds[0], F_SETFD, FD_CLOEXEC) == 0); + assert(close(fds[0]) == 0); + assert(close(fds[1]) == 0); + return 0; +} diff --git a/tests/unit/out/refcount/fcntl.rs b/tests/unit/out/refcount/fcntl.rs new file mode 100644 index 00000000..377a2ce5 --- /dev/null +++ b/tests/unit/out/refcount/fcntl.rs @@ -0,0 +1,171 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + let flags: Value = Rc::new(RefCell::new({ + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*flags.borrow()) >= 0) as i32) != 0)); + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) == 0) as i32) != 0)); + assert!( + ((({ + let __res = match 4 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + (*flags.borrow_mut()) = { + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }; + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) != 0) as i32) != 0)); + let b: Value = >::default(); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((b.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + ((({ + let __res = match 2 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index 22b4982f..a8be4c06 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -6,9 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn test_open_read_write_0() { +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_rw.tmp", + b"/tmp/cpp2rust_fd_io_test.tmp", ))); let fd: Value = Rc::new(RefCell::new({ let __mode = match &[(420).into()].first() { @@ -135,1127 +138,5 @@ pub fn test_open_read_write_0() { } == 0) as i32) != 0) ); -} -pub fn test_pipe_1() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"ab") - .to_any() - .reinterpret_cast::() - .with_slice(2_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - let buf: Value> = Rc::new(RefCell::new( - (0..4).map(|_| ::default()).collect::>(), - )); - { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 2_isize) as i32) - != 0) - ); - assert!( - ((({ - let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); - let mut __it2 = Ptr::from_string_literal(b"ab").to_c_string_iterator(); - loop { - let __c1 = __it1.next(); - let __c2 = __it2.next(); - if __c1 != __c2 { - break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); - } - if __c1.is_none() { - break 0; - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 4]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0_isize) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); -} -pub fn test_socket_listen_2() { - let s: Value = Rc::new(RefCell::new({ - let __family = match libc::AF_INET { - ::libc::AF_INET => nix::sys::socket::AddressFamily::Inet, - ::libc::AF_INET6 => nix::sys::socket::AddressFamily::Inet6, - ::libc::AF_UNIX => nix::sys::socket::AddressFamily::Unix, - __d => panic!("socket: unsupported domain {__d}"), - }; - let __flags = nix::sys::socket::SockFlag::from_bits_truncate(libc::SOCK_STREAM); - let __ty = match libc::SOCK_STREAM & !nix::sys::socket::SockFlag::all().bits() { - ::libc::SOCK_STREAM => nix::sys::socket::SockType::Stream, - ::libc::SOCK_DGRAM => nix::sys::socket::SockType::Datagram, - __t => panic!("socket: unsupported type {__t}"), - }; - let __proto = match 0 { - 0 => None, - ::libc::IPPROTO_TCP => Some(nix::sys::socket::SockProtocol::Tcp), - ::libc::IPPROTO_UDP => Some(nix::sys::socket::SockProtocol::Udp), - __p => panic!("socket: unsupported protocol {__p}"), - }; - match nix::sys::socket::socket(__family, __ty, __flags, __proto) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*s.borrow()) >= 0) as i32) != 0)); - assert!( - (((match nix::sys::socket::Backlog::new(5) { - Ok(__b) => match FdRegistry::with_fd((*s.borrow()), |__fd| nix::sys::socket::listen( - &__fd, __b - )) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - }, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*s.borrow())) == 0) as i32) != 0)); -} -pub fn test_lseek_3() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_lseek.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - Ptr::from_string_literal(b"hello world") - .to_any() - .reinterpret_cast::() - .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 11_isize) as i32) - != 0) - ); - assert!( - ((({ - let __whence = match 2 { - 0 => nix::unistd::Whence::SeekSet, - 1 => nix::unistd::Whence::SeekCur, - 2 => nix::unistd::Whence::SeekEnd, - __w => panic!("lseek: unsupported whence {__w}"), - }; - match FdRegistry::with_fd((*fd.borrow()), |__fd| { - nix::unistd::lseek(__fd, 0_i64, __whence) - }) { - Ok(__off) => __off, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 11_i64) as i32) - != 0) - ); - assert!( - ((({ - let __whence = match 0 { - 0 => nix::unistd::Whence::SeekSet, - 1 => nix::unistd::Whence::SeekCur, - 2 => nix::unistd::Whence::SeekEnd, - __w => panic!("lseek: unsupported whence {__w}"), - }; - match FdRegistry::with_fd((*fd.borrow()), |__fd| { - nix::unistd::lseek(__fd, 6_i64, __whence) - }) { - Ok(__off) => __off, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 6_i64) as i32) - != 0) - ); - let buf: Value> = Rc::new(RefCell::new( - (0..16).map(|_| ::default()).collect::>(), - )); - { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize); - ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() - }; - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| { - nix::unistd::read(__fd, __buf) - }) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 5_isize) as i32) - != 0) - ); - assert!( - ((({ - let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); - let mut __it2 = Ptr::from_string_literal(b"world").to_c_string_iterator(); - loop { - let __c1 = __it1.next(); - let __c2 = __it2.next(); - if __c1 != __c2 { - break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); - } - if __c1.is_none() { - break 0; - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_ftruncate_4() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_trunc.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - Ptr::from_string_literal(b"hello world") - .to_any() - .reinterpret_cast::() - .with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 11_isize) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::ftruncate(__fd, 5_i64)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - ((({ - let __whence = match 2 { - 0 => nix::unistd::Whence::SeekSet, - 1 => nix::unistd::Whence::SeekCur, - 2 => nix::unistd::Whence::SeekEnd, - __w => panic!("lseek: unsupported whence {__w}"), - }; - match FdRegistry::with_fd((*fd.borrow()), |__fd| { - nix::unistd::lseek(__fd, 0_i64, __whence) - }) { - Ok(__off) => __off, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 5_i64) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_fstat_5() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_fstat.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain( - ((::libc::O_WRONLY | ::libc::O_CREAT) | ::libc::O_TRUNC), - ), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { - Ptr::from_string_literal(b"hello") - .to_any() - .reinterpret_cast::() - .with_slice(5_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 5_isize) as i32) - != 0) - ); - let st: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::stat::fstat(__fd)) { - Ok(__s) => { - (st.as_pointer()).with_mut(|__st| *__st = Stat::from_libc(&__s)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0)); - assert!((((((*(*st.borrow()).st_mode.borrow()) & 61440_u32) == 32768_u32) as i32) != 0)); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_isatty_6() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_tty.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::unistd::isatty(__fd)) { - Ok(__tty) => __tty as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - 0 - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_tcgetattr_7() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_fd_io_termios.tmp", - ))); - let fd: Value = Rc::new(RefCell::new({ - let __mode = match &[(420).into()].first() { - Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t), - None => nix::sys::stat::Mode::empty(), - }; - match nix::fcntl::open( - (*path.borrow()).to_rust_string().as_str(), - nix::fcntl::OFlag::from_bits_retain((::libc::O_RDONLY | ::libc::O_CREAT)), - __mode, - ) { - Ok(__ofd) => FdRegistry::register(__ofd), - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*fd.borrow()) >= 0) as i32) != 0)); - let tio: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::termios::tcgetattr(__fd)) { - Ok(__t) => { - (tio.as_pointer()).with_mut(|__dst| *__dst = Termios::from_libc(&__t.into())); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == -1_i32) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0)); - assert!( - (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); -} -pub fn test_fcntl_8() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - let flags: Value = Rc::new(RefCell::new({ - let __res = match 3 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*flags.borrow()) >= 0) as i32) != 0)); - assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) == 0) as i32) != 0)); - assert!( - ((({ - let __res = match 4 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get( - &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], - )); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get( - &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], - )); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 0) as i32) - != 0) - ); - (*flags.borrow_mut()) = { - let __res = match 3 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }; - assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) != 0) as i32) != 0)); - let b: Value = >::default(); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((b.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == (-1_i32 as isize)) as i32) - != 0) - ); - assert!( - ((({ - let __res = match 2 { - ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) - }), - ::libc::F_SETFL => { - let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(1).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) - }) - } - ::libc::F_SETFD => { - let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(1).into()][0])); - FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) - }) - } - __cmd => panic!("fcntl: unsupported cmd {}", __cmd), - }; - match __res { - Ok(__r) => __r, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 0) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); -} -pub fn test_select_9() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - let rset: Value = Rc::new(RefCell::new(Default::default())); - (rset.as_pointer()).with_mut(|__s| __s.zero()); - (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); - let tv: Value = Rc::new(RefCell::new(Default::default())); - (*(*tv.borrow()).tv_sec.borrow_mut()) = 0_i64; - (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; - assert!( - ((({ - let __rp = (rset.as_pointer()).clone(); - let __wp = Ptr::::null().clone(); - let __ep = Ptr::::null().clone(); - let __tp = (tv.as_pointer()).clone(); - let __r_fds: Vec = match __rp.is_null() { - true => Vec::new(), - false => __rp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __w_fds: Vec = match __wp.is_null() { - true => Vec::new(), - false => __wp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __e_fds: Vec = match __ep.is_null() { - true => Vec::new(), - false => __ep.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let mut __wanted = Vec::new(); - __wanted.extend(&__r_fds); - __wanted.extend(&__w_fds); - __wanted.extend(&__e_fds); - FdRegistry::with_fds(&__wanted, |__b| { - let __rn = __r_fds.len(); - let __wn = __w_fds.len(); - let mut __rs = nix::sys::select::FdSet::new(); - let mut __ws = nix::sys::select::FdSet::new(); - let mut __es = nix::sys::select::FdSet::new(); - for __bfd in &__b[..__rn] { - __rs.insert(*__bfd); - } - for __bfd in &__b[__rn..__rn + __wn] { - __ws.insert(*__bfd); - } - for __bfd in &__b[__rn + __wn..] { - __es.insert(*__bfd); - } - let mut __tv = match __tp.is_null() { - true => None, - false => Some(__tp.with(|__t| { - nix::sys::time::TimeVal::new( - *__t.tv_sec.borrow() as _, - *__t.tv_usec.borrow() as _, - ) - })), - }; - match nix::sys::select::select( - ((*fds.borrow())[(0) as usize] + 1), - match __rp.is_null() { - true => None, - false => Some(&mut __rs), - }, - match __wp.is_null() { - true => None, - false => Some(&mut __ws), - }, - match __ep.is_null() { - true => None, - false => Some(&mut __es), - }, - __tv.as_mut(), - ) { - Ok(__n) => { - if !__rp.is_null() { - __rp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { - if __rs.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__wp.is_null() { - __wp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { - if __ws.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__ep.is_null() { - __ep.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { - if __es.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - match (__tp.is_null(), __tv.as_ref()) { - (false, Some(__t)) => __tp.with_mut(|__dst| { - *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; - *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; - }), - _ => {} - } - __n - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }) - } == 0) as i32) - != 0) - ); - assert!( - ((!(if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { - 1 - } else { - 0 - } != 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"x") - .to_any() - .reinterpret_cast::() - .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 1_isize) as i32) - != 0) - ); - (rset.as_pointer()).with_mut(|__s| __s.zero()); - (rset.as_pointer()).with_mut(|__s| __s.set((*fds.borrow())[(0) as usize])); - (*(*tv.borrow()).tv_sec.borrow_mut()) = 1_i64; - (*(*tv.borrow()).tv_usec.borrow_mut()) = 0_i64; - assert!( - ((({ - let __rp = (rset.as_pointer()).clone(); - let __wp = Ptr::::null().clone(); - let __ep = Ptr::::null().clone(); - let __tp = (tv.as_pointer()).clone(); - let __r_fds: Vec = match __rp.is_null() { - true => Vec::new(), - false => __rp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __w_fds: Vec = match __wp.is_null() { - true => Vec::new(), - false => __wp.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let __e_fds: Vec = match __ep.is_null() { - true => Vec::new(), - false => __ep.with(|__s| { - (0..((*fds.borrow())[(0) as usize] + 1)) - .filter(|&__fd| __s.isset(__fd)) - .collect() - }), - }; - let mut __wanted = Vec::new(); - __wanted.extend(&__r_fds); - __wanted.extend(&__w_fds); - __wanted.extend(&__e_fds); - FdRegistry::with_fds(&__wanted, |__b| { - let __rn = __r_fds.len(); - let __wn = __w_fds.len(); - let mut __rs = nix::sys::select::FdSet::new(); - let mut __ws = nix::sys::select::FdSet::new(); - let mut __es = nix::sys::select::FdSet::new(); - for __bfd in &__b[..__rn] { - __rs.insert(*__bfd); - } - for __bfd in &__b[__rn..__rn + __wn] { - __ws.insert(*__bfd); - } - for __bfd in &__b[__rn + __wn..] { - __es.insert(*__bfd); - } - let mut __tv = match __tp.is_null() { - true => None, - false => Some(__tp.with(|__t| { - nix::sys::time::TimeVal::new( - *__t.tv_sec.borrow() as _, - *__t.tv_usec.borrow() as _, - ) - })), - }; - match nix::sys::select::select( - ((*fds.borrow())[(0) as usize] + 1), - match __rp.is_null() { - true => None, - false => Some(&mut __rs), - }, - match __wp.is_null() { - true => None, - false => Some(&mut __ws), - }, - match __ep.is_null() { - true => None, - false => Some(&mut __es), - }, - __tv.as_mut(), - ) { - Ok(__n) => { - if !__rp.is_null() { - __rp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __r_fds.iter().zip(&__b[..__rn]) { - if __rs.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__wp.is_null() { - __wp.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __w_fds.iter().zip(&__b[__rn..__rn + __wn]) { - if __ws.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - if !__ep.is_null() { - __ep.with_mut(|__s| { - __s.zero(); - for (__fd, __bfd) in __e_fds.iter().zip(&__b[__rn + __wn..]) { - if __es.contains(*__bfd) { - __s.set(*__fd); - } - } - }); - } - match (__tp.is_null(), __tv.as_ref()) { - (false, Some(__t)) => __tp.with_mut(|__dst| { - *__dst.tv_sec.borrow_mut() = __t.tv_sec() as i64; - *__dst.tv_usec.borrow_mut() = __t.tv_usec() as i64; - }), - _ => {} - } - __n - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }) - } == 1) as i32) - != 0) - ); - assert!( - (if (rset.as_pointer()).with(|__s| __s.isset((*fds.borrow())[(0) as usize])) { - 1 - } else { - 0 - } != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); -} -pub fn test_poll_10() { - let fds: Value> = Rc::new(RefCell::new( - (0..2).map(|_| ::default()).collect::>(), - )); - assert!( - (((match nix::unistd::pipe() { - Ok((__r, __w)) => { - let __fds = (fds.as_pointer() as Ptr).clone(); - __fds.write(FdRegistry::register(__r)); - __fds.offset(1).write(FdRegistry::register(__w)); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { - Ptr::from_string_literal(b"x") - .to_any() - .reinterpret_cast::() - .with_slice(1_usize, |__buf| nix::unistd::write(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 1_isize) as i32) - != 0) - ); - let pfd: Value> = Rc::new(RefCell::new( - (0..2) - .map(|_| Default::default()) - .collect::>(), - )); - (*(*pfd.borrow())[(0) as usize].fd.borrow_mut()) = (*fds.borrow())[(0) as usize]; - (*(*pfd.borrow())[(0) as usize].events.borrow_mut()) = 1_i16; - (*(*pfd.borrow())[(0) as usize].revents.borrow_mut()) = 0_i16; - (*(*pfd.borrow())[(1) as usize].fd.borrow_mut()) = -1_i32; - (*(*pfd.borrow())[(1) as usize].events.borrow_mut()) = 1_i16; - (*(*pfd.borrow())[(1) as usize].revents.borrow_mut()) = 42_i16; - assert!( - ((({ - let __p = (pfd.as_pointer() as Ptr).clone(); - let __timeout = match nix::poll::PollTimeout::try_from(0) { - Ok(__t) => __t, - Err(_) => panic!("poll: unsupported timeout {}", 0), - }; - let mut __idx = Vec::new(); - let mut __wanted = Vec::new(); - let mut __events = Vec::new(); - for __i in 0..(2_u64 as usize) { - let (__fd, __ev) = __p - .offset(__i) - .with(|__e| (*__e.fd.borrow(), *__e.events.borrow())); - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = 0); - if __fd >= 0 { - __idx.push(__i); - __wanted.push(__fd); - __events.push(__ev); - } - } - FdRegistry::with_fds(&__wanted, |__b| { - let mut __pfds: Vec = __b - .iter() - .zip(&__events) - .map(|(&__fd, &__ev)| { - nix::poll::PollFd::new(__fd, nix::poll::PollFlags::from_bits_truncate(__ev)) - }) - .collect(); - match nix::poll::poll(&mut __pfds, __timeout) { - Ok(__count) => { - for (__slot, &__i) in __pfds.iter().zip(&__idx) { - let __rev = match __slot.revents() { - Some(__r) => __r.bits(), - None => 0, - }; - __p.offset(__i) - .with_mut(|__e| *__e.revents.borrow_mut() = __rev); - } - __count - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - }) - } == 1) as i32) - != 0) - ); - assert!( - ((((((*(*pfd.borrow())[(0) as usize].revents.borrow()) as i32) & 1) != 0) as i32) != 0) - ); - assert!((((((*(*pfd.borrow())[(1) as usize].revents.borrow()) as i32) == 0) as i32) != 0)); - let ch: Value = >::default(); - assert!( - (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { - ((ch.as_pointer()) as Ptr) - .to_any() - .reinterpret_cast::() - .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) - }) { - Ok(__n) => __n as isize, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 1_isize) as i32) - != 0) - ); - assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); - assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); -} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - ({ test_open_read_write_0() }); - ({ test_pipe_1() }); - ({ test_socket_listen_2() }); - ({ test_lseek_3() }); - ({ test_ftruncate_4() }); - ({ test_fstat_5() }); - ({ test_isatty_6() }); - ({ test_tcgetattr_7() }); - ({ test_fcntl_8() }); - ({ test_select_9() }); - ({ test_poll_10() }); return 0; } diff --git a/tests/unit/out/unsafe/fcntl.rs b/tests/unit/out/unsafe/fcntl.rs new file mode 100644 index 00000000..08e188da --- /dev/null +++ b/tests/unit/out/unsafe/fcntl.rs @@ -0,0 +1,47 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + let mut flags: i32 = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!(((((flags) >= (0)) as i32) != 0)); + assert!((((((flags) & (::libc::O_NONBLOCK)) == (0)) as i32) != 0)); + assert!( + ((((unsafe { + libc::fcntl( + fds[(0) as usize] as i32, + 4 as i32, + ((flags) | (::libc::O_NONBLOCK)), + ) + }) == (0)) as i32) + != 0) + ); + flags = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!((((((flags) & (::libc::O_NONBLOCK)) != (0)) as i32) != 0)); + let mut b: libc::c_char = (0 as libc::c_char); + assert!( + ((((libc::read( + fds[(0) as usize], + ((&mut b as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), + 1_usize + )) == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + ((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/fd_io.rs b/tests/unit/out/unsafe/fd_io.rs index 1af3e9ce..29c8057d 100644 --- a/tests/unit/out/unsafe/fd_io.rs +++ b/tests/unit/out/unsafe/fd_io.rs @@ -6,9 +6,14 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn test_open_read_write_0() { +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_rw.tmp".as_ptr().cast_mut()).cast_const(); + (c"/tmp/cpp2rust_fd_io_test.tmp".as_ptr().cast_mut()).cast_const(); let mut fd: i32 = (unsafe { libc::open( path as *const i8, @@ -61,329 +66,5 @@ pub unsafe fn test_open_read_write_0() { ); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_pipe_1() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"ab".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 2_usize - )) == (2_isize)) as i32) - != 0) - ); - let mut buf: [libc::c_char; 4] = [(0 as libc::c_char); 4]; - { - let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::<[libc::c_char; 4]>() { - *byte_0.offset(offset as isize) = 0 as u8; - } - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fds[(0) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (2_isize)) as i32) - != 0) - ); - assert!( - ((((libc::strcmp( - (buf.as_mut_ptr()).cast_const(), - (c"ab".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); - assert!( - ((((libc::read( - fds[(0) as usize], - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 4]>() - )) == (0_isize)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); -} -pub unsafe fn test_socket_listen_2() { - let mut s: i32 = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0); - assert!(((((s) >= (0)) as i32) != 0)); - assert!(((((libc::listen(s, 5)) == (0)) as i32) != 0)); - assert!(((((libc::close(s)) == (0)) as i32) != 0)); -} -pub unsafe fn test_lseek_3() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_lseek.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!( - ((((libc::write( - fd, - (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 11_usize - )) == (11_isize)) as i32) - != 0) - ); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); - assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); - let mut buf: [libc::c_char; 16] = [(0 as libc::c_char); 16]; - { - let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8; - for offset in 0..::std::mem::size_of::<[libc::c_char; 16]>() { - *byte_0.offset(offset as isize) = 0 as u8; - } - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) - }; - assert!( - ((((libc::read( - fd, - (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), - ::std::mem::size_of::<[libc::c_char; 16]>() - )) == (5_isize)) as i32) - != 0) - ); - assert!( - ((((libc::strcmp( - (buf.as_mut_ptr()).cast_const(), - (c"world".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_ftruncate_4() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_trunc.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!( - ((((libc::write( - fd, - (c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 11_usize - )) == (11_isize)) as i32) - != 0) - ); - assert!(((((libc::ftruncate(fd, 5_i64)) == (0)) as i32) != 0)); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_fstat_5() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_fstat.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - (((::libc::O_WRONLY) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!( - ((((libc::write( - fd, - (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 5_usize - )) == (5_isize)) as i32) - != 0) - ); - let mut st: ::libc::stat = unsafe { std::mem::zeroed() }; - assert!(((((libc::fstat(fd, (&mut st as *mut ::libc::stat))) == (0)) as i32) != 0)); - assert!(((((st.st_size) == (5_i64)) as i32) != 0)); - assert!((((((st.st_mode) & (61440_u32)) == (32768_u32)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_isatty_6() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_tty.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - assert!(((((libc::isatty(fd)) == (0)) as i32) != 0)); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_tcgetattr_7() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_fd_io_termios.tmp".as_ptr().cast_mut()).cast_const(); - let mut fd: i32 = (unsafe { - libc::open( - path as *const i8, - ((::libc::O_RDONLY) | (::libc::O_CREAT)) as i32, - (420), - ) - }); - assert!(((((fd) >= (0)) as i32) != 0)); - let mut tio: ::libc::termios = unsafe { std::mem::zeroed() }; - assert!( - ((((libc::tcgetattr(fd, (&mut tio as *mut ::libc::termios))) == (-1_i32)) as i32) != 0) - ); - assert!(((((libc::close(fd)) == (0)) as i32) != 0)); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub unsafe fn test_fcntl_8() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - let mut flags: i32 = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); - assert!(((((flags) >= (0)) as i32) != 0)); - assert!((((((flags) & (::libc::O_NONBLOCK)) == (0)) as i32) != 0)); - assert!( - ((((unsafe { - libc::fcntl( - fds[(0) as usize] as i32, - 4 as i32, - ((flags) | (::libc::O_NONBLOCK)), - ) - }) == (0)) as i32) - != 0) - ); - flags = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); - assert!((((((flags) & (::libc::O_NONBLOCK)) != (0)) as i32) != 0)); - let mut b: libc::c_char = (0 as libc::c_char); - assert!( - ((((libc::read( - fds[(0) as usize], - ((&mut b as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), - 1_usize - )) == (-1_i32 as isize)) as i32) - != 0) - ); - assert!( - ((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); -} -pub unsafe fn test_select_9() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - let mut rset: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>(); - libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); - libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); - let mut tv: ::libc::timeval = unsafe { std::mem::zeroed() }; - tv.tv_sec = 0_i64; - tv.tv_usec = 0_i64; - assert!( - ((((libc::select( - ((fds[(0) as usize]) + (1)), - (&mut rset as *mut ::libc::fd_set), - std::ptr::null_mut(), - std::ptr::null_mut(), - (&mut tv as *mut ::libc::timeval) - )) == (0)) as i32) - != 0) - ); - assert!( - ((!(libc::FD_ISSET( - fds[(0) as usize], - (&mut rset as *mut ::libc::fd_set).cast_const() - ) as i32 - != 0) as i32) - != 0) - ); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize - )) == (1_isize)) as i32) - != 0) - ); - libc::FD_ZERO((&mut rset as *mut ::libc::fd_set)); - libc::FD_SET(fds[(0) as usize], (&mut rset as *mut ::libc::fd_set)); - tv.tv_sec = 1_i64; - tv.tv_usec = 0_i64; - assert!( - ((((libc::select( - ((fds[(0) as usize]) + (1)), - (&mut rset as *mut ::libc::fd_set), - std::ptr::null_mut(), - std::ptr::null_mut(), - (&mut tv as *mut ::libc::timeval) - )) == (1)) as i32) - != 0) - ); - assert!( - (libc::FD_ISSET( - fds[(0) as usize], - (&mut rset as *mut ::libc::fd_set).cast_const() - ) as i32 - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); -} -pub unsafe fn test_poll_10() { - let mut fds: [i32; 2] = [0_i32; 2]; - assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); - assert!( - ((((libc::write( - fds[(1) as usize], - (c"x".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void), - 1_usize - )) == (1_isize)) as i32) - != 0) - ); - let mut pfd: [::libc::pollfd; 2] = [unsafe { std::mem::zeroed() }; 2]; - pfd[(0) as usize].fd = fds[(0) as usize]; - pfd[(0) as usize].events = 1_i16; - pfd[(0) as usize].revents = 0_i16; - pfd[(1) as usize].fd = -1_i32; - pfd[(1) as usize].events = 1_i16; - pfd[(1) as usize].revents = 42_i16; - assert!(((((libc::poll(pfd.as_mut_ptr(), 2_u64 as ::libc::nfds_t, 0)) == (1)) as i32) != 0)); - assert!((((((pfd[(0) as usize].revents as i32) & (1)) != (0)) as i32) != 0)); - assert!(((((pfd[(1) as usize].revents as i32) == (0)) as i32) != 0)); - let mut ch: libc::c_char = (0 as libc::c_char); - assert!( - ((((libc::read( - fds[(0) as usize], - ((&mut ch as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), - 1_usize - )) == (1_isize)) as i32) - != 0) - ); - assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); - assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_open_read_write_0() }); - (unsafe { test_pipe_1() }); - (unsafe { test_socket_listen_2() }); - (unsafe { test_lseek_3() }); - (unsafe { test_ftruncate_4() }); - (unsafe { test_fstat_5() }); - (unsafe { test_isatty_6() }); - (unsafe { test_tcgetattr_7() }); - (unsafe { test_fcntl_8() }); - (unsafe { test_select_9() }); - (unsafe { test_poll_10() }); return 0; } From 9ce16d676e196ae797731e866bf45a3167997384 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 21 Jul 2026 21:44:21 +0100 Subject: [PATCH 3/5] Add GETFD in fcntl --- rules/fcntl/tgt_refcount.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rules/fcntl/tgt_refcount.rs b/rules/fcntl/tgt_refcount.rs index 70e67535..51ff7206 100644 --- a/rules/fcntl/tgt_refcount.rs +++ b/rules/fcntl/tgt_refcount.rs @@ -14,6 +14,9 @@ fn f1(a0: i32, a1: i32, va: &[VaArg]) -> i32 { nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) }) } + ::libc::F_GETFD => FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), ::libc::F_SETFD => { let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&va[0])); FdRegistry::with_fd(a0, |__fd| { From a9b03a9eae0a4af1a6ce4469ddd3073cef27c630 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 14:53:53 +0100 Subject: [PATCH 4/5] Update tests --- tests/unit/fcntl.c | 2 ++ tests/unit/out/refcount/fcntl.rs | 62 ++++++++++++++++++++++++++++++++ tests/unit/out/unsafe/fcntl.rs | 10 ++++++ 3 files changed, 74 insertions(+) diff --git a/tests/unit/fcntl.c b/tests/unit/fcntl.c index b314d1de..a9869b49 100644 --- a/tests/unit/fcntl.c +++ b/tests/unit/fcntl.c @@ -13,7 +13,9 @@ int main(void) { assert((flags & O_NONBLOCK) != 0); char b; assert(read(fds[0], &b, 1) == -1); + assert((fcntl(fds[0], F_GETFD, 0) & FD_CLOEXEC) == 0); assert(fcntl(fds[0], F_SETFD, FD_CLOEXEC) == 0); + assert((fcntl(fds[0], F_GETFD, 0) & FD_CLOEXEC) != 0); assert(close(fds[0]) == 0); assert(close(fds[1]) == 0); return 0; diff --git a/tests/unit/out/refcount/fcntl.rs b/tests/unit/out/refcount/fcntl.rs index 377a2ce5..fe25d964 100644 --- a/tests/unit/out/refcount/fcntl.rs +++ b/tests/unit/out/refcount/fcntl.rs @@ -135,6 +135,37 @@ fn main_0() -> i32 { } == (-1_i32 as isize)) as i32) != 0) ); + assert!( + (((({ + let __res = match 1 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } & 1) + == 0) as i32) + != 0) + ); assert!( ((({ let __res = match 2 { @@ -165,6 +196,37 @@ fn main_0() -> i32 { } == 0) as i32) != 0) ); + assert!( + (((({ + let __res = match 1 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } & 1) + != 0) as i32) + != 0) + ); assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); return 0; diff --git a/tests/unit/out/unsafe/fcntl.rs b/tests/unit/out/unsafe/fcntl.rs index 08e188da..d40f4c94 100644 --- a/tests/unit/out/unsafe/fcntl.rs +++ b/tests/unit/out/unsafe/fcntl.rs @@ -38,9 +38,19 @@ unsafe fn main_0() -> i32 { )) == (-1_i32 as isize)) as i32) != 0) ); + assert!( + (((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 1 as i32, (0),) }) & (1)) == (0)) + as i32) + != 0) + ); assert!( ((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0) ); + assert!( + (((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 1 as i32, (0),) }) & (1)) != (0)) + as i32) + != 0) + ); assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); return 0; From 241a5184012c746d834c87a7e9f7427cdbc7de2f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 23 Jul 2026 15:09:29 +0100 Subject: [PATCH 5/5] Update tests --- tests/unit/out/refcount/fcntl.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/unit/out/refcount/fcntl.rs b/tests/unit/out/refcount/fcntl.rs index fe25d964..7b8bf8ad 100644 --- a/tests/unit/out/refcount/fcntl.rs +++ b/tests/unit/out/refcount/fcntl.rs @@ -39,6 +39,9 @@ fn main_0() -> i32 { nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) }) } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), ::libc::F_SETFD => { let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { @@ -71,6 +74,9 @@ fn main_0() -> i32 { nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) }) } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), ::libc::F_SETFD => { let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get( &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], @@ -102,6 +108,9 @@ fn main_0() -> i32 { nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) }) } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), ::libc::F_SETFD => { let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { @@ -147,6 +156,9 @@ fn main_0() -> i32 { nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) }) } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), ::libc::F_SETFD => { let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { @@ -178,6 +190,9 @@ fn main_0() -> i32 { nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) }) } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), ::libc::F_SETFD => { let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(1).into()][0])); FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { @@ -208,6 +223,9 @@ fn main_0() -> i32 { nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) }) } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), ::libc::F_SETFD => { let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| {