diff --git a/libcc2rs/src/fd.rs b/libcc2rs/src/fd.rs index 64bfb795..119cc87b 100644 --- a/libcc2rs/src/fd.rs +++ b/libcc2rs/src/fd.rs @@ -9,7 +9,13 @@ pub struct FdRegistry { } thread_local! { - static FD_REGISTRY: RefCell = const { RefCell::new(FdRegistry { fds: Vec::new() }) }; + static FD_REGISTRY: RefCell = RefCell::new(FdRegistry { + fds: vec![ + std::io::stdin().as_fd().try_clone_to_owned().ok(), + std::io::stdout().as_fd().try_clone_to_owned().ok(), + std::io::stderr().as_fd().try_clone_to_owned().ok(), + ], + }); } impl FdRegistry { diff --git a/libcc2rs/src/io.rs b/libcc2rs/src/io.rs index fef060ce..70a98e28 100644 --- a/libcc2rs/src/io.rs +++ b/libcc2rs/src/io.rs @@ -1,7 +1,7 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::{AnyPtr, AsPointer, Ptr, Value}; +use crate::{AnyPtr, AsPointer, CFile, Ptr, Value}; use std::cell::{RefCell, UnsafeCell}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; @@ -48,6 +48,24 @@ thread_local! { }; } +thread_local! { + static C_STDIN: Value = Rc::new(RefCell::new(CFile::new(0))); + static C_STDOUT: Value = Rc::new(RefCell::new(CFile::new(1))); + static C_STDERR: Value = Rc::new(RefCell::new(CFile::new(2))); +} + +pub fn c_stdin() -> Ptr { + C_STDIN.with(AsPointer::as_pointer) +} + +pub fn c_stdout() -> Ptr { + C_STDOUT.with(AsPointer::as_pointer) +} + +pub fn c_stderr() -> Ptr { + C_STDERR.with(AsPointer::as_pointer) +} + pub fn cin() -> Ptr { SAFE_STDIN.with(AsPointer::as_pointer) } @@ -84,79 +102,24 @@ pub unsafe fn cerr_unsafe() -> *mut std::fs::File { UNSAFE_STDERR.with(UnsafeCell::get) } -pub fn fread_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +pub fn fread_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let total = a1.saturating_mul(a2); - let mut dst = a0.reinterpret_cast::(); - - let f = (*a3.upgrade().deref()) - .try_clone() - .expect("try_clone failed"); - let mut reader = std::io::BufReader::with_capacity(64 * 1024, f); - - let mut read_bytes: usize = 0; - let mut buffer: [u8; 8192] = [0; 8192]; - - while read_bytes < total { - let remaining = total - read_bytes; - let to_read = std::cmp::min(buffer.len(), remaining); - - let n = match std::io::Read::read(&mut reader, &mut buffer[..to_read]) { - Ok(0) => break, - Ok(n) => n, - Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue, - Err(e) => panic!("Unhandled error in fread: {e}"), - }; - - for &byte in &buffer[..n] { - dst.write(byte); - dst += 1; - } - - read_bytes += n; + if total == 0 { + return 0; } - + let dst = a0.reinterpret_cast::(); + let read_bytes = dst.with_slice_mut(total, |buf| a3.with_mut(|f| f.read(buf))); read_bytes / a1 } -pub fn fwrite_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +pub fn fwrite_refcount(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let total = a1.saturating_mul(a2); - let mut src = a0.reinterpret_cast::(); - - let f = (*a3.upgrade().deref()) - .try_clone() - .expect("try_clone failed"); - let mut writer = std::io::BufWriter::with_capacity(64 * 1024, f); - - let mut written_bytes: usize = 0; - let mut buffer: [u8; 8192] = [0; 8192]; - - while written_bytes < total { - let remaining = total - written_bytes; - let to_fill = std::cmp::min(buffer.len(), remaining); - - for b in buffer.iter_mut().take(to_fill) { - *b = src.read(); - src += 1; - } - - let mut off = 0; - while off < to_fill { - match std::io::Write::write(&mut writer, &buffer[off..to_fill]) { - Ok(0) => break, - Ok(n) => off += n, - Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue, - Err(e) => panic!("Unhandled error in fwrite: {e}"), - } - } - - if off == 0 { - break; - } - - written_bytes += off; + if total == 0 { + return 0; } - - written_bytes / a1 + let src = a0.reinterpret_cast::(); + let written = src.with_slice(total, |bytes| a3.with_mut(|f| f.write(bytes))); + written / a1 } unsafe extern "C" { diff --git a/libcc2rs/src/libc_shims/cfile.rs b/libcc2rs/src/libc_shims/cfile.rs new file mode 100644 index 00000000..914f3bdb --- /dev/null +++ b/libcc2rs/src/libc_shims/cfile.rs @@ -0,0 +1,122 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use crate::FdRegistry; + +pub struct CFile { + pub fd: i32, + pub eof: bool, + pub err: bool, +} + +impl CFile { + pub fn new(fd: i32) -> Self { + CFile { + fd, + eof: false, + err: false, + } + } + + pub fn open(path: &str, mode: &str) -> Option { + let flags = match mode { + "rb" => nix::fcntl::OFlag::O_RDONLY, + "wb" => nix::fcntl::OFlag::O_WRONLY + .union(nix::fcntl::OFlag::O_CREAT) + .union(nix::fcntl::OFlag::O_TRUNC), + m => panic!("fopen: unsupported mode {:?}", m), + }; + match nix::fcntl::open(path, flags, nix::sys::stat::Mode::from_bits_truncate(0o666)) { + Ok(ofd) => Some(CFile::new(FdRegistry::register(ofd))), + Err(e) => { + crate::cpp2rust_errno().write(e as i32); + None + } + } + } + + pub fn read(&mut self, buf: &mut [u8]) -> usize { + let mut n = 0; + while n < buf.len() { + match FdRegistry::with_fd(self.fd, |b| nix::unistd::read(b, &mut buf[n..])) { + Ok(0) => { + self.eof = true; + break; + } + Ok(k) => n += k, + Err(nix::errno::Errno::EINTR) => {} + Err(e) => { + self.err = true; + crate::cpp2rust_errno().write(e as i32); + break; + } + } + } + n + } + + pub fn write(&mut self, buf: &[u8]) -> usize { + let mut n = 0; + while n < buf.len() { + match FdRegistry::with_fd(self.fd, |b| nix::unistd::write(b, &buf[n..])) { + Ok(0) => { + self.err = true; + break; + } + Ok(k) => n += k, + Err(nix::errno::Errno::EINTR) => {} + Err(e) => { + self.err = true; + crate::cpp2rust_errno().write(e as i32); + break; + } + } + } + n + } + + pub fn seek(&mut self, offset: i64, whence: i32) -> i64 { + let w = match whence { + 0 => nix::unistd::Whence::SeekSet, + 1 => nix::unistd::Whence::SeekCur, + 2 => nix::unistd::Whence::SeekEnd, + other => panic!("fseek: unsupported whence {}", other), + }; + match FdRegistry::with_fd(self.fd, |b| nix::unistd::lseek(b, offset, w)) { + Ok(off) => { + self.eof = false; + off + } + Err(e) => { + crate::cpp2rust_errno().write(e as i32); + -1 + } + } + } + + pub fn tell(&self) -> i64 { + match FdRegistry::with_fd(self.fd, |b| { + nix::unistd::lseek(b, 0, nix::unistd::Whence::SeekCur) + }) { + Ok(off) => off, + Err(e) => { + crate::cpp2rust_errno().write(e as i32); + -1 + } + } + } + + pub fn getc(&mut self) -> i32 { + let mut b = [0u8; 1]; + match self.read(&mut b) { + 1 => b[0] as i32, + _ => -1, + } + } + + pub fn close(&self) -> i32 { + FdRegistry::close(self.fd) + } +} + +impl crate::ByteRepr for CFile {} diff --git a/libcc2rs/src/libc_shims/mod.rs b/libcc2rs/src/libc_shims/mod.rs index be806985..016ab7f9 100644 --- a/libcc2rs/src/libc_shims/mod.rs +++ b/libcc2rs/src/libc_shims/mod.rs @@ -1,6 +1,7 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. +mod cfile; mod dirent; mod fdset; mod ifaddrs; @@ -13,6 +14,7 @@ mod stat; mod termios; mod time; +pub use cfile::*; pub use dirent::*; pub use fdset::*; pub use ifaddrs::*; diff --git a/rules/stdio/src.cpp b/rules/stdio/src.cpp index 21822c9a..271233d2 100644 --- a/rules/stdio/src.cpp +++ b/rules/stdio/src.cpp @@ -71,3 +71,9 @@ int f23(FILE *stream) { return getc(stream); } int f24(FILE *stream, char *buf, int mode, size_t size) { return setvbuf(stream, buf, mode, size); } + +int f25(void) { return SEEK_SET; } + +int f26(void) { return SEEK_CUR; } + +int f27(void) { return SEEK_END; } diff --git a/rules/stdio/tgt_refcount.rs b/rules/stdio/tgt_refcount.rs index 94a1a879..e275508d 100644 --- a/rules/stdio/tgt_refcount.rs +++ b/rules/stdio/tgt_refcount.rs @@ -2,59 +2,36 @@ // Distributed under the MIT license that can be found in the LICENSE file. use libcc2rs::*; -use std::io::prelude::*; -use std::os::fd::AsFd; -fn t1() -> Ptr<::std::fs::File> { +fn t1() -> Ptr { Ptr::null() } -fn f1(a0: Ptr, a1: Ptr) -> Ptr<::std::fs::File> { - match a1.to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(a0.to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(a0.to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), +fn f1(a0: Ptr, a1: Ptr) -> Ptr { + match CFile::open(&a0.to_rust_string(), &a1.to_rust_string()) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), } } -fn f2(a0: Ptr<::std::fs::File>) -> i32 { +fn f2(a0: Ptr) -> i32 { + let __r = a0.with(|__f| __f.close()); a0.delete(); - 0 + __r } -fn f3(a0: Ptr<::std::fs::File>) -> i64 { - a0.with_mut(|v| match v.stream_position() { - Ok(pos) => pos as i64, - Err(_) => -1, - }) +fn f3(a0: Ptr) -> i64 { + a0.with(|__f| __f.tell()) } -fn f4(a0: &mut ::std::fs::File, a1: i64, a2: i32) -> i32 { - if (match a2 { - 0 => a0.seek(std::io::SeekFrom::Start(a1 as u64)), - 1 => a0.seek(std::io::SeekFrom::Current(a1)), - 2 => a0.seek(std::io::SeekFrom::End(a1)), - _ => Err(std::io::Error::other("unsupported whence for fseek.")), - }) - .is_ok() - { - 0 - } else { - -1 +fn f4(a0: &mut CFile, a1: i64, a2: i32) -> i32 { + match a0.seek(a1, a2) { + -1 => -1, + _ => 0, } } -fn f5(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +fn f5(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let __a0 = a0; let __a1 = a1; let __a2 = a2; @@ -62,7 +39,7 @@ fn f5(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) } -fn f6(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { +fn f6(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr) -> usize { let __a0 = a0; let __a1 = a1; let __a2 = a2; @@ -70,99 +47,83 @@ fn f6(a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>) -> usize { libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3) } -fn f7(a0: Ptr<::std::fs::File>) -> i32 { - if !a0.is_null() { - match a0.with_mut(|v| v.sync_all()) { - Ok(_) => 0, - Err(_) => -1, - } - } else { - ::std::io::stdout().flush().unwrap(); - ::std::io::stderr().flush().unwrap(); - 0 - } +fn f7(a0: Ptr) -> i32 { + 0 } -fn f8() -> Ptr<::std::fs::File> { - libcc2rs::cout() +fn f8() -> Ptr { + libcc2rs::c_stdout() } -fn f9() -> Ptr<::std::fs::File> { - libcc2rs::cerr() +fn f9() -> Ptr { + libcc2rs::c_stderr() } -fn f10() -> Ptr<::std::fs::File> { - libcc2rs::cin() +fn f10() -> Ptr { + libcc2rs::c_stdin() } -fn f11(a0: i32, a1: Ptr<::std::fs::File>) -> i32 { +fn f11(a0: i32, a1: Ptr) -> i32 { let __c = a0 as u8; - match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &[__c])) { - Ok(()) => __c as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match a1.with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, } } -fn f12(a0: Ptr, a1: Ptr<::std::fs::File>) -> i32 { +fn f12(a0: Ptr, a1: Ptr) -> i32 { let __bytes: Vec = a0.to_c_string_iterator().collect(); - match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match a1.with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } fn f13(a0: Ptr) -> i32 { - let __bytes: Vec = a0.to_c_string_iterator().collect(); - match libcc2rs::cout().with_mut(|__f| { - ::std::io::Write::write_all(__f, &__bytes) - .and_then(|_| ::std::io::Write::write_all(__f, b"\n")) - }) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + let mut __bytes: Vec = a0.to_c_string_iterator().collect(); + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } -fn f17(a0: Ptr, a1: i32, a2: Ptr<::std::fs::File>) -> Ptr { +fn f14(a0: Ptr) -> i32 { + a0.with(|__f| __f.fd) +} + +fn f15(a0: Ptr) -> i32 { + a0.with(|__f| __f.err) as i32 +} + +fn f16(a0: Ptr) -> i32 { + a0.with(|__f| __f.eof) as i32 +} + +fn f17(a0: Ptr, a1: i32, a2: Ptr) -> Ptr { let __buf = a0.clone(); let __n = a1; - let __stream = a2.clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = a2.with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -172,47 +133,35 @@ fn f17(a0: Ptr, a1: i32, a2: Ptr<::std::fs::File>) -> Ptr { } } -fn f18(a0: Ptr, a1: Ptr, a2: Ptr<::std::fs::File>) -> Ptr<::std::fs::File> { +fn f18(a0: Ptr, a1: Ptr, a2: Ptr) -> Ptr { let __stream = a2.clone(); - let __new = match a1.to_rust_string().as_str() { - "rb" => ::std::fs::OpenOptions::new() - .read(true) - .open(a0.to_rust_string()), - "wb" => ::std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(a0.to_rust_string()), - _ => panic!("unsupported mode"), - }; - match __new { - Ok(__f) => { + let __old = __stream.with(|__f| __f.fd); + match __old { + 0..=2 => {} + __fd => { + FdRegistry::close(__fd); + } + } + match CFile::open(&a0.to_rust_string(), &a1.to_rust_string()) { + Some(__f) => { __stream.write(__f); __stream } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - Ptr::null() - } + None => Ptr::null(), } } -fn f19(a0: Ptr<::std::fs::File>, a1: i64, a2: i32) -> i32 { - let __r = a0.with_mut(|__f| match a2 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(a1 as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(a1)), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(a1)), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } +fn f19(a0: Ptr, a1: i64, a2: i32) -> i32 { + match a0.with_mut(|__f| __f.seek(a1, a2)) { + -1 => -1, + _ => 0, } } +fn f20(a0: i32, a1: Ptr) -> Ptr { + Ptr::alloc(CFile::new(a0)) +} + fn f21(a0: Ptr, a1: usize, a2: Ptr, va: &[VaArg]) -> i32 { let __s = libcc2rs::format_c(&a2.to_rust_string(), va); let __b = __s.as_bytes(); @@ -236,19 +185,10 @@ fn f22(a0: Ptr, a1: Ptr) -> i32 { } } -fn f23(a0: Ptr<::std::fs::File>) -> i32 { - let mut __b: [u8; 1] = [0]; - match a0.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } +fn f23(a0: Ptr) -> i32 { + a0.with_mut(|__f| __f.getc()) } -fn f24(a0: Ptr<::std::fs::File>, a1: Ptr, a2: i32, a3: usize) -> i32 { - /* std::fs::File is unbuffered */ +fn f24(a0: Ptr, a1: Ptr, a2: i32, a3: usize) -> i32 { 0 } diff --git a/rules/stdio/tgt_unsafe.rs b/rules/stdio/tgt_unsafe.rs index bbd1d0a0..e2dafd93 100644 --- a/rules/stdio/tgt_unsafe.rs +++ b/rules/stdio/tgt_unsafe.rs @@ -104,3 +104,15 @@ unsafe fn f23(a0: *mut ::libc::FILE) -> i32 { unsafe fn f24(a0: *mut ::libc::FILE, a1: *mut libc::c_char, a2: i32, a3: usize) -> i32 { libc::setvbuf(a0, a1, a2, a3) } + +unsafe fn f25() -> i32 { + ::libc::SEEK_SET +} + +unsafe fn f26() -> i32 { + ::libc::SEEK_CUR +} + +unsafe fn f27() -> i32 { + ::libc::SEEK_END +} diff --git a/tests/ub/file_use_after_fclose.c b/tests/ub/file_use_after_fclose.c new file mode 100644 index 00000000..53114d5d --- /dev/null +++ b/tests/ub/file_use_after_fclose.c @@ -0,0 +1,11 @@ +// panic-ub: refcount +// nondet-result: unsafe +#include +#include + +int main(void) { + FILE *fp = fopen("/tmp/cpp2rust_uafc_test.tmp", "wb"); + assert(fp); + fclose(fp); + return fputc('x', fp) == 'x' ? 1 : 0; +} diff --git a/tests/ub/out/refcount/file_use_after_fclose.rs b/tests/ub/out/refcount/file_use_after_fclose.rs new file mode 100644 index 00000000..73c4f73e --- /dev/null +++ b/tests/ub/out/refcount/file_use_after_fclose.rs @@ -0,0 +1,41 @@ +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 fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/tmp/cpp2rust_uafc_test.tmp").to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!(!(*fp.borrow()).is_null()); + { + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + }; + return if ((({ + let __c = ('x' as i32) as u8; + match (*fp.borrow()).with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + } == ('x' as i32)) as i32) + != 0) + { + 1 + } else { + 0 + }; +} diff --git a/tests/ub/out/unsafe/file_use_after_fclose.rs b/tests/ub/out/unsafe/file_use_after_fclose.rs new file mode 100644 index 00000000..c576dd9d --- /dev/null +++ b/tests/ub/out/unsafe/file_use_after_fclose.rs @@ -0,0 +1,26 @@ +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 fp: *mut ::libc::FILE = libc::fopen( + (c"/tmp/cpp2rust_uafc_test.tmp".as_ptr().cast_mut()).cast_const(), + (c"wb".as_ptr().cast_mut()).cast_const(), + ); + assert!(!(fp).is_null()); + libc::fclose(fp); + return if ((((libc::fputc(('x' as i32), fp)) == ('x' as i32)) as i32) != 0) { + 1 + } else { + 0 + }; +} diff --git a/tests/unit/errno.c b/tests/unit/errno.c index 9f9f0e3f..ec58a397 100644 --- a/tests/unit/errno.c +++ b/tests/unit/errno.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include diff --git a/tests/unit/out/refcount/errno.rs b/tests/unit/out/refcount/errno.rs index a326cca0..2cf9c8bc 100644 --- a/tests/unit/out/refcount/errno.rs +++ b/tests/unit/out/refcount/errno.rs @@ -28,21 +28,9 @@ pub fn test_errno_preserved_across_strdup_1() { pub fn test_errno_from_fseek_2() { libcc2rs::cpp2rust_errno().write(0); let r: Value = Rc::new(RefCell::new( - if (match 0 { - 0 => libcc2rs::cin().with_mut(|__v: &mut ::std::fs::File| { - __v.seek(std::io::SeekFrom::Start(0_i64 as u64)) - }), - 1 => libcc2rs::cin() - .with_mut(|__v: &mut ::std::fs::File| __v.seek(std::io::SeekFrom::Current(0_i64))), - 2 => libcc2rs::cin() - .with_mut(|__v: &mut ::std::fs::File| __v.seek(std::io::SeekFrom::End(0_i64))), - _ => Err(std::io::Error::other("unsupported whence for fseek.")), - }) - .is_ok() - { - 0 - } else { - -1 + match libcc2rs::c_stdin().with_mut(|__v: &mut CFile| __v.seek(0_i64, ::libc::SEEK_SET)) { + -1 => -1, + _ => 0, }, )); assert!(((((*r.borrow()) == -1_i32) as i32) != 0)); diff --git a/tests/unit/out/refcount/fflush_null.rs b/tests/unit/out/refcount/fflush_null.rs index 61c6c9c9..b9314f49 100644 --- a/tests/unit/out/refcount/fflush_null.rs +++ b/tests/unit/out/refcount/fflush_null.rs @@ -10,15 +10,6 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let file_ptr: Value> = Rc::new(RefCell::new(Ptr::null())); - return if !(*file_ptr.borrow()).is_null() { - match (*file_ptr.borrow()).with_mut(|v| v.sync_all()) { - Ok(_) => 0, - Err(_) => -1, - } - } else { - ::std::io::stdout().flush().unwrap(); - ::std::io::stderr().flush().unwrap(); - 0 - }; + let file_ptr: Value> = Rc::new(RefCell::new(Ptr::null())); + return 0; } diff --git a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs index 96ed54c2..b809e187 100644 --- a/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs +++ b/tests/unit/out/refcount/fn_ptr_stdlib_compare.rs @@ -24,45 +24,37 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fn1: Value) -> usize>> = + let fn1: Value) -> usize>> = Rc::new(RefCell::new(FnPtr::< - fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, + fn(AnyPtr, usize, usize, Ptr) -> usize, >::new(libcc2rs::fread_refcount))); assert!({ let _lhs = (*fn1.borrow()).clone(); - _lhs == FnPtr::) -> usize>::new( + _lhs == FnPtr::) -> usize>::new( libcc2rs::fread_refcount, ) }); assert!(!((*fn1.borrow()).is_null())); let fn2: Value, usize, usize, AnyPtr) -> usize>> = Rc::new(RefCell::new( - FnPtr::) -> usize>::new( - libcc2rs::fread_refcount, - ) - .cast::, usize, usize, AnyPtr) -> usize>(Some( + FnPtr::) -> usize>::new(libcc2rs::fread_refcount) + .cast::, usize, usize, AnyPtr) -> usize>(Some( (|a0: Ptr, a1: usize, a2: usize, a3: AnyPtr| -> usize { - libcc2rs::fread_refcount( - a0.to_any(), - a1, - a2, - a3.reinterpret_cast::<::std::fs::File>(), - ) + libcc2rs::fread_refcount(a0.to_any(), a1, a2, a3.reinterpret_cast::()) }) as fn(Ptr, usize, usize, AnyPtr) -> usize, )), )); assert!({ let _lhs = (*fn1.borrow()).clone(); - _lhs == ((*fn2.borrow()) - .cast::) -> usize>(None)) - .clone() + _lhs == ((*fn2.borrow()).cast::) -> usize>(None)) + .clone() }); - let f3: Value) -> usize>> = + let f3: Value) -> usize>> = Rc::new(RefCell::new( FnPtr::, usize, usize, AnyPtr) -> usize>::new(my_alternative_fread_0) - .cast::) -> usize>(Some( - (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>| -> usize { + .cast::) -> usize>(Some( + (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr| -> usize { my_alternative_fread_0(a0.reinterpret_cast::(), a1, a2, a3.to_any()) - }) as fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, + }) as fn(AnyPtr, usize, usize, Ptr) -> usize, )), )); assert!( @@ -71,21 +63,13 @@ fn main_0() -> i32 { let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/zero").to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -118,28 +102,21 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/zero").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/zero").to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -175,72 +152,56 @@ fn main_0() -> i32 { (*i.borrow_mut()).prefix_inc(); } { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } - let gn1: Value) -> usize>> = + let gn1: Value) -> usize>> = Rc::new(RefCell::new(FnPtr::< - fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, + fn(AnyPtr, usize, usize, Ptr) -> usize, >::new(libcc2rs::fwrite_refcount))); assert!({ let _lhs = (*gn1.borrow()).clone(); - _lhs == FnPtr::) -> usize>::new( + _lhs == FnPtr::) -> usize>::new( libcc2rs::fwrite_refcount, ) }); assert!(!((*gn1.borrow()).is_null())); let gn2: Value, usize, usize, AnyPtr) -> usize>> = Rc::new(RefCell::new( - FnPtr::) -> usize>::new( - libcc2rs::fwrite_refcount, - ) - .cast::, usize, usize, AnyPtr) -> usize>(Some( + FnPtr::) -> usize>::new(libcc2rs::fwrite_refcount) + .cast::, usize, usize, AnyPtr) -> usize>(Some( (|a0: Ptr, a1: usize, a2: usize, a3: AnyPtr| -> usize { - libcc2rs::fwrite_refcount( - a0.to_any(), - a1, - a2, - a3.reinterpret_cast::<::std::fs::File>(), - ) + libcc2rs::fwrite_refcount(a0.to_any(), a1, a2, a3.reinterpret_cast::()) }) as fn(Ptr, usize, usize, AnyPtr) -> usize, )), )); assert!({ let _lhs = (*gn1.borrow()).clone(); - _lhs == ((*gn2.borrow()) - .cast::) -> usize>(None)) - .clone() + _lhs == ((*gn2.borrow()).cast::) -> usize>(None)) + .clone() }); - let g3: Value) -> usize>> = - Rc::new(RefCell::new( - FnPtr::, usize, usize, AnyPtr) -> usize>::new(my_alternative_fwrite_1) - .cast::) -> usize>(Some( - (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr<::std::fs::File>| -> usize { - my_alternative_fwrite_1(a0.reinterpret_cast::(), a1, a2, a3.to_any()) - }) as fn(AnyPtr, usize, usize, Ptr<::std::fs::File>) -> usize, - )), - )); + let g3: Value) -> usize>> = Rc::new(RefCell::new( + FnPtr::, usize, usize, AnyPtr) -> usize>::new(my_alternative_fwrite_1) + .cast::) -> usize>(Some( + (|a0: AnyPtr, a1: usize, a2: usize, a3: Ptr| -> usize { + my_alternative_fwrite_1(a0.reinterpret_cast::(), a1, a2, a3.to_any()) + }) as fn(AnyPtr, usize, usize, Ptr) -> usize, + )), + )); assert!( (({ (*(*g3.borrow()))(AnyPtr::default(), 0_usize, 0_usize, Ptr::null(),) }) == 33_usize) ); let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/null").to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -263,28 +224,21 @@ fn main_0() -> i32 { })); assert!(((*n.borrow()) == 10_usize)); { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } let mut __do_while = true; 'loop_: while __do_while || (0 != 0) { __do_while = false; - let stream: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(Ptr::from_string_literal(b"/dev/null").to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let stream: Value> = Rc::new(RefCell::new( + match CFile::open( + &Ptr::from_string_literal(b"/dev/null").to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!(!((*stream.borrow()).is_null())); @@ -310,8 +264,9 @@ fn main_0() -> i32 { )); assert!(((*n.borrow()) == 10_usize)); { + let __r = (*stream.borrow()).with(|__f| __f.close()); (*stream.borrow()).delete(); - 0 + __r }; } return 0; diff --git a/tests/unit/out/refcount/fopen.rs b/tests/unit/out/refcount/fopen.rs index 1bf595ef..d8c30f4f 100644 --- a/tests/unit/out/refcount/fopen.rs +++ b/tests/unit/out/refcount/fopen.rs @@ -12,21 +12,14 @@ pub fn main() { fn main_0() -> i32 { let fname: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"testfile.txt"))); let mode: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"rb"))); - let file_ptr: Value> = - Rc::new(RefCell::new(match (*mode.borrow()).to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*fname.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*fname.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), - })); + let file_ptr: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*fname.borrow()).to_rust_string(), + &(*mode.borrow()).to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); return 0; } diff --git a/tests/unit/out/refcount/global_without_initializer.rs b/tests/unit/out/refcount/global_without_initializer.rs index f1dd9e2a..331bc5ef 100644 --- a/tests/unit/out/refcount/global_without_initializer.rs +++ b/tests/unit/out/refcount/global_without_initializer.rs @@ -35,7 +35,7 @@ thread_local!( pub static s_0: Value> = Rc::new(RefCell::new(Ptr::::null())); ); thread_local!( - pub static file_1: Value> = Rc::new(RefCell::new(Ptr::null())); + pub static file_1: Value> = Rc::new(RefCell::new(Ptr::null())); ); thread_local!( pub static size_2: Value = Rc::new(RefCell::new(0_usize)); diff --git a/tests/unit/out/refcount/goto_aggregate_default.rs b/tests/unit/out/refcount/goto_aggregate_default.rs index ff5b7fa2..c12724a6 100644 --- a/tests/unit/out/refcount/goto_aggregate_default.rs +++ b/tests/unit/out/refcount/goto_aggregate_default.rs @@ -51,7 +51,7 @@ pub fn agg_0(n: i32) -> i32 { let p: Value = >::default(); let ptr: Value> = Rc::new(RefCell::new(Ptr::::null())); let fp: Value i32>> = Rc::new(RefCell::new(FnPtr::null())); - let file: Value> = Rc::new(RefCell::new(Ptr::null())); + let file: Value> = Rc::new(RefCell::new(Ptr::null())); let total: Value = >::default(); goto_block!({ '__entry: { diff --git a/tests/unit/out/refcount/lseek_ftruncate.rs b/tests/unit/out/refcount/lseek_ftruncate.rs index 732fafa1..5da75c3c 100644 --- a/tests/unit/out/refcount/lseek_ftruncate.rs +++ b/tests/unit/out/refcount/lseek_ftruncate.rs @@ -50,7 +50,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let __whence = match 2 { + let __whence = match ::libc::SEEK_END { 0 => nix::unistd::Whence::SeekSet, 1 => nix::unistd::Whence::SeekCur, 2 => nix::unistd::Whence::SeekEnd, @@ -70,7 +70,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let __whence = match 0 { + let __whence = match ::libc::SEEK_SET { 0 => nix::unistd::Whence::SeekSet, 1 => nix::unistd::Whence::SeekCur, 2 => nix::unistd::Whence::SeekEnd, @@ -143,7 +143,7 @@ fn main_0() -> i32 { ); assert!( ((({ - let __whence = match 2 { + let __whence = match ::libc::SEEK_END { 0 => nix::unistd::Whence::SeekSet, 1 => nix::unistd::Whence::SeekCur, 2 => nix::unistd::Whence::SeekEnd, diff --git a/tests/unit/out/refcount/printfs.rs b/tests/unit/out/refcount/printfs.rs index c6148b19..704eb21d 100644 --- a/tests/unit/out/refcount/printfs.rs +++ b/tests/unit/out/refcount/printfs.rs @@ -26,7 +26,7 @@ fn main_0() -> i32 { println!("{}", Ptr::from_string_literal(b"fprintf stdout")); println!("{} {} {}", 1, 2_u32, 3_i64); print!("hello world"); - let in_: Value> = Rc::new(RefCell::new((libcc2rs::cin()).clone())); + let in_: Value> = Rc::new(RefCell::new((libcc2rs::c_stdin()).clone())); assert!(!((*in_.borrow()).is_null())); println!("{}", Ptr::from_string_literal(b"printf")); print!("hello world"); diff --git a/tests/unit/out/refcount/ptr_to_incomplete_struct.rs b/tests/unit/out/refcount/ptr_to_incomplete_struct.rs index 8d999eac..342a9937 100644 --- a/tests/unit/out/refcount/ptr_to_incomplete_struct.rs +++ b/tests/unit/out/refcount/ptr_to_incomplete_struct.rs @@ -10,11 +10,9 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fp: Value> = Rc::new(RefCell::new((libcc2rs::cout()).clone())); + let fp: Value> = Rc::new(RefCell::new((libcc2rs::c_stdout()).clone())); let p: Value = Rc::new(RefCell::new((*fp.borrow()).clone().to_any())); - let fp2: Value> = Rc::new(RefCell::new( - (*p.borrow()).reinterpret_cast::<::std::fs::File>(), - )); + let fp2: Value> = Rc::new(RefCell::new((*p.borrow()).reinterpret_cast::())); assert!( ((({ let _lhs = (*fp.borrow()).clone(); diff --git a/tests/unit/out/refcount/stdio.rs b/tests/unit/out/refcount/stdio.rs new file mode 100644 index 00000000..33c19da5 --- /dev/null +++ b/tests/unit/out/refcount/stdio.rs @@ -0,0 +1,151 @@ +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 test_fputc_0() { + { + let __c = ('H' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + { + let __c = ('i' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; +} +pub fn test_fputs_1() { + { + let __bytes: Vec = Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .collect(); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"from variable"))); + { + let __bytes: Vec = (*s.borrow()).to_c_string_iterator().collect(); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('b' as i32) as u8), + (('u' as i32) as u8), + (('f' as i32) as u8), + (('\0' as i32) as u8), + ]))); + { + let __bytes: Vec = (buf.as_pointer() as Ptr) + .to_c_string_iterator() + .collect(); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + { + let __c = ('\n' as i32) as u8; + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, + } + }; +} +pub fn test_puts_2() { + { + let mut __bytes: Vec = Ptr::from_string_literal(b"puts hello") + .to_c_string_iterator() + .collect(); + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"puts variable"))); + { + let mut __bytes: Vec = (*s.borrow()).to_c_string_iterator().collect(); + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; +} +pub fn test_fileno_3() { + assert!((((libcc2rs::c_stdin().with(|__f| __f.fd) == 0) as i32) != 0)); + assert!((((libcc2rs::c_stdout().with(|__f| __f.fd) == 1) as i32) != 0)); + assert!((((libcc2rs::c_stderr().with(|__f| __f.fd) == 2) as i32) != 0)); + let file: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"cpp2rust_fileno_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*file.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!(((((*fp.borrow()).with(|__f| __f.fd) > 2) as i32) != 0)); + { + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + }; + assert!( + (((match nix::unistd::unlink((*file.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_fputc_0() }); + ({ test_fputs_1() }); + ({ test_puts_2() }); + ({ test_fileno_3() }); + return 0; +} diff --git a/tests/unit/out/refcount/stdio_nofd.rs b/tests/unit/out/refcount/stdio_nofd.rs index 0914e8a5..e459d55b 100644 --- a/tests/unit/out/refcount/stdio_nofd.rs +++ b/tests/unit/out/refcount/stdio_nofd.rs @@ -10,33 +10,22 @@ pub fn test_fputc_fputs_0() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( b"cpp2rust_stdio_nofd_puts.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); assert!( ((({ let __c = ('A' as i32) as u8; - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &[__c])) { - Ok(()) => __c as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&[__c])) { + 1 => __c as i32, + _ => -1, } } == ('A' as i32)) as i32) != 0) @@ -46,37 +35,27 @@ pub fn test_fputc_fputs_0() { let __bytes: Vec = Ptr::from_string_literal(b"BCD\n") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); let buf: Value> = Rc::new(RefCell::new(Box::new([ @@ -116,8 +95,9 @@ pub fn test_fputc_fputs_0() { ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -135,18 +115,13 @@ pub fn test_fputc_fputs_0() { pub fn test_puts_1() { assert!( ((({ - let __bytes: Vec = Ptr::from_string_literal(b"hello from puts") + let mut __bytes: Vec = Ptr::from_string_literal(b"hello from puts") .to_c_string_iterator() .collect(); - match libcc2rs::cout().with_mut(|__f| { - ::std::io::Write::write_all(__f, &__bytes) - .and_then(|_| ::std::io::Write::write_all(__f, b"\n")) - }) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + __bytes.push(b'\n'); + match libcc2rs::c_stdout().with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) @@ -156,21 +131,13 @@ pub fn test_fgets_getc_2() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( b"cpp2rust_stdio_nofd_gets.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -179,37 +146,27 @@ pub fn test_fgets_getc_2() { let __bytes: Vec = Ptr::from_string_literal(b"line1\nline2\n") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); let buf: Value> = Rc::new(RefCell::new( @@ -219,36 +176,27 @@ pub fn test_fgets_getc_2() { (((!(({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 8; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -267,54 +215,32 @@ pub fn test_fgets_getc_2() { == 0) as i32) != 0) ); - assert!( - ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == ('l' as i32)) as i32) - != 0) - ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('l' as i32)) as i32) != 0)); assert!( (((!(({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 4; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -337,36 +263,27 @@ pub fn test_fgets_getc_2() { (((!(({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 8; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -389,36 +306,27 @@ pub fn test_fgets_getc_2() { (((({ let __buf = (buf.as_pointer() as Ptr).clone(); let __n = 8; - let __stream = (*fp.borrow()).clone(); if __n <= 0 { Ptr::null() } else { let __max = (__n - 1) as usize; let mut __dst = __buf.clone(); let mut __count: usize = 0; - let mut __failed = false; - while __count < __max { - let mut __b: [u8; 1] = [0]; - match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => break, - Ok(_) => { - __dst.write(__b[0]); - __dst += 1; - __count += 1; - if __b[0] == b'\n' { - break; - } + let __failed = (*fp.borrow()).with_mut(|__f| { + while __count < __max { + let __c = __f.getc(); + if __c < 0 { + break; } - Err(__e) => { - if __e.kind() != ::std::io::ErrorKind::Interrupted { - libcc2rs::cpp2rust_errno() - .write(__e.raw_os_error().unwrap_or(::libc::EIO)); - __failed = true; - break; - } + __dst.write(__c as u8); + __dst += 1; + __count += 1; + if __c as u8 == b'\n' { + break; } } - } + __f.err + }); if __failed || __count == 0 { Ptr::null() } else { @@ -430,24 +338,12 @@ pub fn test_fgets_getc_2() { .is_null()) as i32) != 0) ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == (-1_i32)) as i32) != 0)); assert!( ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == (-1_i32)) as i32) - != 0) - ); - assert!( - ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -466,21 +362,13 @@ pub fn test_freopen_3() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( b"cpp2rust_stdio_nofd_reopen.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -489,38 +377,31 @@ pub fn test_freopen_3() { let __bytes: Vec = Ptr::from_string_literal(b"hello") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); - let fp2: Value> = Rc::new(RefCell::new({ + let fp2: Value> = Rc::new(RefCell::new({ let __stream = (*fp.borrow()).clone(); - let __new = match Ptr::from_string_literal(b"rb").to_rust_string().as_str() { - "rb" => ::std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()), - "wb" => ::std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()), - _ => panic!("unsupported mode"), - }; - match __new { - Ok(__f) => { + let __old = __stream.with(|__f| __f.fd); + match __old { + 0..=2 => {} + __fd => { + FdRegistry::close(__fd); + } + } + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => { __stream.write(__f); __stream } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - Ptr::null() - } + None => Ptr::null(), } })); assert!((((!((*fp2.borrow()).is_null())) as i32) != 0)); @@ -553,8 +434,9 @@ pub fn test_freopen_3() { ); assert!( ((({ + let __r = (*fp2.borrow()).with(|__f| __f.close()); (*fp2.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -573,21 +455,13 @@ pub fn test_fseeko_4() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( b"cpp2rust_stdio_nofd_seek.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -596,54 +470,33 @@ pub fn test_fseeko_4() { let __bytes: Vec = Ptr::from_string_literal(b"hello world") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); assert!( - ((({ - let __r = (*fp.borrow()).with_mut(|__f| match 0 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(6_i64 as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(6_i64)), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(6_i64)), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } - } + (((match (*fp.borrow()).with_mut(|__f| __f.seek(6_i64, ::libc::SEEK_SET)) { + -1 => -1, + _ => 0, } == 0) as i32) != 0) ); @@ -675,73 +528,26 @@ pub fn test_fseeko_4() { != 0) ); assert!( - ((({ - let __r = (*fp.borrow()).with_mut(|__f| match 2 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start((-5_i32 as i64) as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current((-5_i32 as i64))), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End((-5_i32 as i64))), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } - } + (((match (*fp.borrow()).with_mut(|__f| __f.seek((-5_i32 as i64), ::libc::SEEK_END)) { + -1 => -1, + _ => 0, } == 0) as i32) != 0) ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('w' as i32)) as i32) != 0)); assert!( - ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == ('w' as i32)) as i32) - != 0) - ); - assert!( - ((({ - let __r = (*fp.borrow()).with_mut(|__f| match 1 { - 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(1_i64 as u64)), - 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(1_i64)), - 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(1_i64)), - _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), - }); - match __r { - Ok(_) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); - -1 - } - } + (((match (*fp.borrow()).with_mut(|__f| __f.seek(1_i64, ::libc::SEEK_CUR)) { + -1 => -1, + _ => 0, } == 0) as i32) != 0) ); + assert!(((((*fp.borrow()).with_mut(|__f| __f.getc()) == ('r' as i32)) as i32) != 0)); assert!( ((({ - let mut __b: [u8; 1] = [0]; - match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { - Ok(0) => -1, - Ok(_) => __b[0] as i32, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } - } - } == ('r' as i32)) as i32) - != 0) - ); - assert!( - ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -763,21 +569,13 @@ pub fn test_rename_5() { let to: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( b"cpp2rust_stdio_nofd_to.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*from.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); @@ -786,20 +584,18 @@ pub fn test_rename_5() { let __bytes: Vec = Ptr::from_string_literal(b"data") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -817,44 +613,29 @@ pub fn test_rename_5() { != 0) ); assert!( - ((((match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*from.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + ((((match CFile::open( + &(*from.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string() + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }) .is_null()) as i32) != 0) ); - (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*to.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*to.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + (*fp.borrow_mut()) = match CFile::open( + &(*to.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }; assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); @@ -886,44 +667,34 @@ pub fn test_setvbuf_6() { let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( b"cpp2rust_stdio_nofd_vbuf.tmp", ))); - let fp: Value> = Rc::new(RefCell::new( - match Ptr::from_string_literal(b"wb").to_rust_string() { - v if v == "rb" => std::fs::OpenOptions::new() - .read(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - v if v == "wb" => std::fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open((*path.borrow()).to_rust_string()) - .ok() - .map_or(Ptr::null(), |f| Ptr::alloc(f)), - _ => panic!("unsupported mode"), + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), }, )); assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); - assert!((((/* std::fs::File is unbuffered */0 == 0) as i32) != 0)); + assert!((((0 == 0) as i32) != 0)); assert!( ((({ let __bytes: Vec = Ptr::from_string_literal(b"x") .to_c_string_iterator() .collect(); - match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); - -1 - } + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, } } >= 0) as i32) != 0) ); assert!( ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); (*fp.borrow()).delete(); - 0 + __r } == 0) as i32) != 0) ); diff --git a/tests/unit/out/refcount/sys_stat.rs b/tests/unit/out/refcount/sys_stat.rs new file mode 100644 index 00000000..6ca8fe0c --- /dev/null +++ b/tests/unit/out/refcount/sys_stat.rs @@ -0,0 +1,128 @@ +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 test_stat_0() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"cpp2rust_stat_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + let st: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match nix::sys::stat::stat((*path.borrow()).to_rust_string().as_str()) { + 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_mtime.borrow()) > 0_i64) as i32) != 0)); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_fstat_1() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"cpp2rust_fstat_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + 0; + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + 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()) == 11_i64) as i32) != 0)); + assert!(((((*(*st.borrow()).st_mtime.borrow()) > 0_i64) as i32) != 0)); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_stat_0() }); + ({ test_fstat_1() }); + return 0; +} diff --git a/tests/unit/out/refcount/unistd.rs b/tests/unit/out/refcount/unistd.rs new file mode 100644 index 00000000..0164ef74 --- /dev/null +++ b/tests/unit/out/refcount/unistd.rs @@ -0,0 +1,630 @@ +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 test_close_0() { + 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!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + let buf: Value> = Rc::new(RefCell::new( + (0..1).map(|_| ::default()).collect::>(), + )); + 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(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!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); +} +pub fn test_lseek_1() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_lseek_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + 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(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(5_usize, |__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!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"world").to_any(), 5_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_read_2() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_read_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + (((match FdRegistry::with_fd((*fd.borrow()), |__fd| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 11_isize) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"hello world").to_any(), 11_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_unlink_3() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_unlink_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 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) + ); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == -1_i32) as i32) + != 0) + ); +} +pub fn test_pipe_4() { + 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 msg: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"world"))); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(1) as usize], |__fd| { + ((*msg.borrow()).clone() as Ptr) + .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 buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + 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(8_usize, |__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!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&((*msg.borrow()).clone() as Ptr::).to_any(), 5_usize) + == 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(8_usize, |__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_ftruncate_5() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_ftruncate_test.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + { + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| __f.write(&__bytes)) == __bytes.len() { + true => 0, + false => -1, + } + }; + 0; + let fd: Value = Rc::new(RefCell::new((*fp.borrow()).with(|__f| __f.fd))); + 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 __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match CFile::open( + &(*path.borrow()).to_rust_string(), + &Ptr::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + (*fd.borrow_mut()) = (*fp.borrow()).with(|__f| __f.fd); + 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!( + ((({ + let __r = (*fp.borrow()).with(|__f| __f.close()); + (*fp.borrow()).delete(); + __r + } == 0) as i32) + != 0) + ); + match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + }; +} +pub fn test_open_6() { + 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( + Ptr::from_string_literal(b"/dev/null") + .to_rust_string() + .as_str(), + nix::fcntl::OFlag::from_bits_retain(0), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*fd.borrow()) >= -1_i32) as i32) != 0)); + if ((((*fd.borrow()) >= 0) as i32) != 0) { + FdRegistry::close((*fd.borrow())); + } + (*fd.borrow_mut()) = { + let __mode = match &[].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( + Ptr::from_string_literal(b"/dev/null") + .to_rust_string() + .as_str(), + nix::fcntl::OFlag::from_bits_retain(0), + __mode, + ) { + Ok(__ofd) => FdRegistry::register(__ofd), + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }; + assert!(((((*fd.borrow()) >= -1_i32) as i32) != 0)); + if ((((*fd.borrow()) >= 0) as i32) != 0) { + FdRegistry::close((*fd.borrow())); + } +} +pub fn test_fcntl_7() { + assert!( + (((panic!( + "fcntl is not supported in the refcount model (fd={}, cmd={}, varargs={})", + 0, + 1, + &[].len() + ) >= -1_i32) as i32) + != 0) + ); + let duped: Value = Rc::new(RefCell::new(panic!( + "fcntl is not supported in the refcount model (fd={}, cmd={}, varargs={})", + 0, + 0, + &[(100).into(),].len() + ))); + assert!(((((*duped.borrow()) >= -1_i32) as i32) != 0)); + if ((((*duped.borrow()) >= 0) as i32) != 0) { + FdRegistry::close((*duped.borrow())); + } +} +pub fn test_ioctl_8() { + let arg: Value = Rc::new(RefCell::new(0)); + assert!( + (((panic!( + "ioctl is not supported in the refcount model (fd={}, request={}, varargs={})", + 0, + 0_u64, + &[(arg.as_pointer()).into(),].len() + ) >= -1_i32) as i32) + != 0) + ); +} +pub fn test_isatty_9() { + println!( + "{}", + match FdRegistry::with_fd(0, |__fd| nix::unistd::isatty(__fd)) { + Ok(__tty) => __tty as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } + ); +} +pub fn test_geteuid_10() { + println!("{}", nix::unistd::geteuid().as_raw()); +} +pub fn test_gethostname_11() { + let name: Value> = Rc::new(RefCell::new( + (0..256).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::gethostname() { + Ok(__name) => { + let __bytes = __name.as_encoded_bytes(); + let __n = __bytes + .len() + .min(::std::mem::size_of::<[u8; 256]>().saturating_sub(1)); + if ::std::mem::size_of::<[u8; 256]>() > 0 { + (name.as_pointer() as Ptr).with_slice_mut(__n + 1, |__s| { + __s[..__n].copy_from_slice(&__bytes[..__n]); + __s[__n] = 0; + }); + } + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + println!("{}", (name.as_pointer() as Ptr::)); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_close_0() }); + ({ test_lseek_1() }); + ({ test_read_2() }); + ({ test_unlink_3() }); + ({ test_pipe_4() }); + ({ test_ftruncate_5() }); + ({ test_open_6() }); + ({ test_fcntl_7() }); + ({ test_ioctl_8() }); + ({ test_isatty_9() }); + ({ test_geteuid_10() }); + ({ test_gethostname_11() }); + return 0; +} diff --git a/tests/unit/out/refcount/user_defined_same_as_libc.rs b/tests/unit/out/refcount/user_defined_same_as_libc.rs index f52ec539..04bd1c0c 100644 --- a/tests/unit/out/refcount/user_defined_same_as_libc.rs +++ b/tests/unit/out/refcount/user_defined_same_as_libc.rs @@ -6,7 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn fopen_0(path: Ptr, mode: Ptr) -> Ptr<::std::fs::File> { +pub fn fopen_0(path: Ptr, mode: Ptr) -> Ptr { let path: Value> = Rc::new(RefCell::new(path)); let mode: Value> = Rc::new(RefCell::new(mode)); (*path.borrow()).clone(); @@ -17,10 +17,10 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let fp: Value> = Rc::new(RefCell::new( + let fp: Value> = Rc::new(RefCell::new( ({ fopen_0( - Ptr::from_string_literal(b"/tmp/irrelevant-file"), + Ptr::from_string_literal(b"irrelevant-file"), Ptr::from_string_literal(b"r"), ) }), diff --git a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs index 7813aa0d..beb9a941 100644 --- a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs +++ b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs @@ -79,8 +79,8 @@ pub fn dispatch_0(option: i32, __args: &[VaArg]) -> i32 { break 'switch; } __v if __v == (opt::OPT_FILE as i32) => { - let f: Value> = Rc::new(RefCell::new( - ((*ap.borrow_mut()).arg::>()).clone(), + let f: Value> = Rc::new(RefCell::new( + ((*ap.borrow_mut()).arg::>()).clone(), )); (*result.borrow_mut()) = ((!((*f.borrow()).is_null())) as i32).clone(); break 'switch; @@ -120,7 +120,7 @@ fn main_0() -> i32 { (((({ dispatch_0( (opt::OPT_FILE as i32), - &[((libcc2rs::cout()).clone()).into()], + &[((libcc2rs::c_stdout()).clone()).into()], ) }) == 1) as i32) != 0) @@ -129,7 +129,7 @@ fn main_0() -> i32 { (((({ dispatch_0( (opt::OPT_FILE as i32), - &[((AnyPtr::default()).reinterpret_cast::<::std::fs::File>()).into()], + &[((AnyPtr::default()).reinterpret_cast::()).into()], ) }) == 0) as i32) != 0) diff --git a/tests/unit/out/unsafe/errno.rs b/tests/unit/out/unsafe/errno.rs index 41eda988..36749fca 100644 --- a/tests/unit/out/unsafe/errno.rs +++ b/tests/unit/out/unsafe/errno.rs @@ -26,7 +26,11 @@ pub unsafe fn test_errno_preserved_across_strdup_1() { } pub unsafe fn test_errno_from_fseek_2() { (*libcc2rs::cpp2rust_errno_unsafe()) = 0; - let mut r: i32 = libc::fseek(libcc2rs::stdin_unsafe(), 0_i64 as ::libc::c_long, 0); + let mut r: i32 = libc::fseek( + libcc2rs::stdin_unsafe(), + 0_i64 as ::libc::c_long, + ::libc::SEEK_SET, + ); assert!(((((r) == (-1_i32)) as i32) != 0)); assert!(((((*libcc2rs::cpp2rust_errno_unsafe()) == (libc::ESPIPE)) as i32) != 0)); (*libcc2rs::cpp2rust_errno_unsafe()) = 0; diff --git a/tests/unit/out/unsafe/lseek_ftruncate.rs b/tests/unit/out/unsafe/lseek_ftruncate.rs index 49be7fda..9bbf1c69 100644 --- a/tests/unit/out/unsafe/lseek_ftruncate.rs +++ b/tests/unit/out/unsafe/lseek_ftruncate.rs @@ -30,8 +30,8 @@ unsafe fn main_0() -> i32 { )) == (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)); + assert!(((((libc::lseek(fd, 0_i64, ::libc::SEEK_END)) == (11_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 6_i64, ::libc::SEEK_SET)) == (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; @@ -56,7 +56,7 @@ unsafe fn main_0() -> 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::lseek(fd, 0_i64, ::libc::SEEK_END)) == (5_i64)) as i32) != 0)); assert!(((((libc::close(fd)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); return 0; diff --git a/tests/unit/out/unsafe/stdio_nofd.rs b/tests/unit/out/unsafe/stdio_nofd.rs index 15ecb674..0382ace1 100644 --- a/tests/unit/out/unsafe/stdio_nofd.rs +++ b/tests/unit/out/unsafe/stdio_nofd.rs @@ -231,7 +231,7 @@ pub unsafe fn test_fseeko_4() { assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((((!((fp).is_null())) as i32) != 0)); - assert!(((((libc::fseeko(fp, 6_i64 as ::libc::off_t, 0)) == (0)) as i32) != 0)); + assert!(((((libc::fseeko(fp, 6_i64 as ::libc::off_t, ::libc::SEEK_SET)) == (0)) as i32) != 0)); let mut buf: [libc::c_char; 8] = [ (0 as libc::c_char), (0 as libc::c_char), @@ -273,9 +273,12 @@ pub unsafe fn test_fseeko_4() { }) == (0)) as i32) != 0) ); - assert!(((((libc::fseeko(fp, (-5_i32 as i64) as ::libc::off_t, 2)) == (0)) as i32) != 0)); + assert!( + ((((libc::fseeko(fp, (-5_i32 as i64) as ::libc::off_t, ::libc::SEEK_END)) == (0)) as i32) + != 0) + ); assert!(((((libc::fgetc(fp)) == ('w' as i32)) as i32) != 0)); - assert!(((((libc::fseeko(fp, 1_i64 as ::libc::off_t, 1)) == (0)) as i32) != 0)); + assert!(((((libc::fseeko(fp, 1_i64 as ::libc::off_t, ::libc::SEEK_CUR)) == (0)) as i32) != 0)); assert!(((((libc::fgetc(fp)) == ('r' as i32)) as i32) != 0)); assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); diff --git a/tests/unit/out/unsafe/unistd.rs b/tests/unit/out/unsafe/unistd.rs index edfeb8d0..4d15e652 100644 --- a/tests/unit/out/unsafe/unistd.rs +++ b/tests/unit/out/unsafe/unistd.rs @@ -31,8 +31,8 @@ pub unsafe fn test_lseek_1() { fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((((!((fp).is_null())) as i32) != 0)); let mut fd: i32 = libc::fileno(fp); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (11_i64)) as i32) != 0)); - assert!(((((libc::lseek(fd, 6_i64, 0)) == (6_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 0_i64, ::libc::SEEK_END)) == (11_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 6_i64, ::libc::SEEK_SET)) == (6_i64)) as i32) != 0)); let mut buf: [libc::c_char; 8] = [ (0 as libc::c_char), (0 as libc::c_char), @@ -221,7 +221,7 @@ pub unsafe fn test_ftruncate_5() { fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((((!((fp).is_null())) as i32) != 0)); fd = (libc::fileno(fp)).clone(); - assert!(((((libc::lseek(fd, 0_i64, 2)) == (5_i64)) as i32) != 0)); + assert!(((((libc::lseek(fd, 0_i64, ::libc::SEEK_END)) == (5_i64)) as i32) != 0)); assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); libc::unlink(path); } diff --git a/tests/unit/out/unsafe/user_defined_same_as_libc.rs b/tests/unit/out/unsafe/user_defined_same_as_libc.rs index 3fd2350f..6d552900 100644 --- a/tests/unit/out/unsafe/user_defined_same_as_libc.rs +++ b/tests/unit/out/unsafe/user_defined_same_as_libc.rs @@ -22,7 +22,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut fp: *mut ::libc::FILE = (unsafe { fopen_0( - (c"/tmp/irrelevant-file".as_ptr().cast_mut()).cast_const(), + (c"irrelevant-file".as_ptr().cast_mut()).cast_const(), (c"r".as_ptr().cast_mut()).cast_const(), ) }); diff --git a/tests/unit/stdio.c b/tests/unit/stdio.c index 89a382df..8936431c 100644 --- a/tests/unit/stdio.c +++ b/tests/unit/stdio.c @@ -1,4 +1,3 @@ -// no-compile: refcount #include #include #include diff --git a/tests/unit/sys_stat.c b/tests/unit/sys_stat.c index d1261c4b..433665ee 100644 --- a/tests/unit/sys_stat.c +++ b/tests/unit/sys_stat.c @@ -1,4 +1,3 @@ -// no-compile: refcount #include #include #include diff --git a/tests/unit/user_defined_same_as_libc.c b/tests/unit/user_defined_same_as_libc.c index 57863b8a..eeb5c0ef 100644 --- a/tests/unit/user_defined_same_as_libc.c +++ b/tests/unit/user_defined_same_as_libc.c @@ -8,7 +8,7 @@ FILE *fopen(const char *path, const char *mode) { } int main() { - FILE *fp = fopen("/tmp/irrelevant-file", "r"); + FILE *fp = fopen("irrelevant-file", "r"); assert(fp == NULL); return 0; }