From 0d09850a45efad6e94f0409d98bbafb8403d6106 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 15:15:59 +0100 Subject: [PATCH 1/4] Add unistd safe rules --- tests/unit/out/refcount/stdcopy_ostream.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/unit/out/refcount/stdcopy_ostream.rs b/tests/unit/out/refcount/stdcopy_ostream.rs index 9ae7dd16..3470dda5 100644 --- a/tests/unit/out/refcount/stdcopy_ostream.rs +++ b/tests/unit/out/refcount/stdcopy_ostream.rs @@ -29,7 +29,15 @@ fn main_0() -> i32 { ); (*ofs.borrow_mut()).try_clone().unwrap() }; - match nix::unistd::unlink((file.as_pointer() as Ptr).to_rust_string().as_str()) { + match nix::unistd::unlink( + ::std::ffi::CString::new( + (file.as_pointer() as Ptr) + .to_c_string_iterator() + .collect::>(), + ) + .unwrap() + .as_c_str(), + ) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); From a68ab2f7f3c34521af9453a008b0942eda305e1b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 15:20:43 +0100 Subject: [PATCH 2/4] Use to_rust_string in unlink --- tests/unit/out/refcount/stdcopy_ostream.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tests/unit/out/refcount/stdcopy_ostream.rs b/tests/unit/out/refcount/stdcopy_ostream.rs index 3470dda5..9ae7dd16 100644 --- a/tests/unit/out/refcount/stdcopy_ostream.rs +++ b/tests/unit/out/refcount/stdcopy_ostream.rs @@ -29,15 +29,7 @@ fn main_0() -> i32 { ); (*ofs.borrow_mut()).try_clone().unwrap() }; - match nix::unistd::unlink( - ::std::ffi::CString::new( - (file.as_pointer() as Ptr) - .to_c_string_iterator() - .collect::>(), - ) - .unwrap() - .as_c_str(), - ) { + match nix::unistd::unlink((file.as_pointer() as Ptr).to_rust_string().as_str()) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); From d06dc9e16488c11e696369cdec1e8aa7b2b662a2 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 20:33:31 +0100 Subject: [PATCH 3/4] Add safe rules for time --- libc-dep/Cargo.toml | 1 + rule-preprocessor/src/semantic.rs | 9 +- rules/Cargo.toml | 1 + rules/time/tgt_refcount.rs | 148 ++++++++ tests/lit/lit/formats/Cpp2RustTest.py | 6 + tests/unit/out/refcount/sys_time.rs | 499 ++++++++++++++++++++++++++ tests/unit/out/unsafe/sys_time.rs | 186 ++++++++++ tests/unit/sys_time.c | 90 +++++ 8 files changed, 939 insertions(+), 1 deletion(-) create mode 100644 tests/unit/out/refcount/sys_time.rs create mode 100644 tests/unit/out/unsafe/sys_time.rs create mode 100644 tests/unit/sys_time.c diff --git a/libc-dep/Cargo.toml b/libc-dep/Cargo.toml index fefb8b74..06d6bd3f 100644 --- a/libc-dep/Cargo.toml +++ b/libc-dep/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] libc = "0.2" nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } +jiff = "0.2" diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index f9030457..2f475a95 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -46,7 +46,14 @@ fn build_rustc_args(crate_root: &Path) -> Vec { args.push("-L".to_string()); args.push(format!("dependency={}", deps.display())); - for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi", "nix"] { + for dep in &[ + "libcc2rs", + "libc", + "brotli_sys", + "rustls_ffi", + "nix", + "jiff", + ] { if let Some(rlib) = find_rlib(deps.as_path(), dep) { args.push("--extern".to_string()); args.push(format!("{}={}", dep, rlib.display())); diff --git a/rules/Cargo.toml b/rules/Cargo.toml index d9a5e1be..0c8d3841 100644 --- a/rules/Cargo.toml +++ b/rules/Cargo.toml @@ -14,3 +14,4 @@ libcc2rs = { version = "0.1.0", path = "../libcc2rs" } brotli-sys = "0.3" rustls-ffi = { version = "0.15.3", default-features = false } nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } +jiff = "0.2" diff --git a/rules/time/tgt_refcount.rs b/rules/time/tgt_refcount.rs index d5bd7893..1305e223 100644 --- a/rules/time/tgt_refcount.rs +++ b/rules/time/tgt_refcount.rs @@ -14,3 +14,151 @@ fn t2() -> libcc2rs::Timeval { fn t3() -> libcc2rs::Timespec { Default::default() } + +fn f1(a0: Ptr<::libc::time_t>) -> ::libc::time_t { + let __out = a0; + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + let __s = __ts.tv_sec(); + if !__out.is_null() { + __out.write(__s); + } + __s + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f2(a0: ::libc::clockid_t, a1: Ptr) -> i32 { + match nix::time::clock_gettime(nix::time::ClockId::from_raw(a0)) { + Ok(__ts) => { + a1.with_mut(|__t| { + *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f4(a0: Ptr<::libc::time_t>, a1: Ptr) -> Ptr { + let __res = a1; + match jiff::Timestamp::from_second(a0.read()) { + Ok(__ts) => { + let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); + __res.with_mut(|__tm| { + *__tm.tm_sec.borrow_mut() = __dt.second() as i32; + *__tm.tm_min.borrow_mut() = __dt.minute() as i32; + *__tm.tm_hour.borrow_mut() = __dt.hour() as i32; + *__tm.tm_mday.borrow_mut() = __dt.day() as i32; + *__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1; + *__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900; + *__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32; + *__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1; + *__tm.tm_isdst.borrow_mut() = 0; + *__tm.tm_gmtoff.borrow_mut() = 0; + *__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT"); + }); + __res + } + Err(_) => { + libcc2rs::cpp2rust_errno().write(::libc::EOVERFLOW); + Ptr::null() + } + } +} + +fn f6(a0: Ptr, a1: usize, a2: Ptr, a3: Ptr) -> usize { + let __dt = a3.with(|__tm| { + jiff::civil::DateTime::new( + (*__tm.tm_year.borrow() + 1900) as i16, + (*__tm.tm_mon.borrow() + 1) as i8, + *__tm.tm_mday.borrow() as i8, + *__tm.tm_hour.borrow() as i8, + *__tm.tm_min.borrow() as i8, + *__tm.tm_sec.borrow() as i8, + 0, + ) + }); + let __text = match __dt { + Ok(__d) => { + jiff::fmt::strtime::format(a2.to_rust_string().as_str(), __d).unwrap_or_default() + } + Err(_) => String::new(), + }; + if __text.is_empty() || __text.len() + 1 > a1 { + 0 + } else { + let mut __dst = a0.clone(); + for __b in __text.as_bytes() { + __dst.write(*__b); + __dst += 1; + } + __dst.write(0); + __text.len() + } +} + +fn f7(a0: Ptr, a1: Ptr) -> i32 { + let __times = a1; + let __at = __times.with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + let __mt = __times.offset(1).with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + match nix::sys::stat::utimes(a0.to_rust_string().as_str(), &__at, &__mt) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +#[cfg(target_os = "linux")] +fn f8(a0: Ptr, a1: Ptr<::libc::timezone>) -> i32 { + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + a0.with_mut(|__tv| { + *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +#[cfg(target_os = "macos")] +fn f8(a0: Ptr, a1: AnyPtr) -> i32 { + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + a0.with_mut(|__tv| { + *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} diff --git a/tests/lit/lit/formats/Cpp2RustTest.py b/tests/lit/lit/formats/Cpp2RustTest.py index 89465616..006bc2a1 100644 --- a/tests/lit/lit/formats/Cpp2RustTest.py +++ b/tests/lit/lit/formats/Cpp2RustTest.py @@ -212,6 +212,10 @@ def build_rust(self): libc_dep_deps.glob("libnix-*.rlib"), key=lambda p: p.stat().st_mtime, ) + jiff_rlib = max( + libc_dep_deps.glob("libjiff-*.rlib"), + key=lambda p: p.stat().st_mtime, + ) cmd = [ "rustc", "+" + read_rust_version(), @@ -240,6 +244,8 @@ def build_rust(self): f"libc={libc_rlib}", "--extern", f"nix={nix_rlib}", + "--extern", + f"jiff={jiff_rlib}", ] _, err, returncode = lit.util.executeCommand(cmd, str(self.tmp_dir)) if exp.should_not_compile: diff --git a/tests/unit/out/refcount/sys_time.rs b/tests/unit/out/refcount/sys_time.rs new file mode 100644 index 00000000..295c2000 --- /dev/null +++ b/tests/unit/out/refcount/sys_time.rs @@ -0,0 +1,499 @@ +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_time_0() { + let t1: Value = Rc::new(RefCell::new({ + let __out = Ptr::::null(); + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + let __s = __ts.tv_sec(); + if !__out.is_null() { + __out.write(__s); + } + __s + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + let t2: Value = Rc::new(RefCell::new(0_i64)); + let t3: Value = Rc::new(RefCell::new({ + let __out = (t2.as_pointer()); + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + let __s = __ts.tv_sec(); + if !__out.is_null() { + __out.write(__s); + } + __s + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*t1.borrow()) > 1500000000_i64) as i32) != 0)); + assert!(((((*t2.borrow()) == (*t3.borrow())) as i32) != 0)); + assert!(((((*t3.borrow()) >= (*t1.borrow())) as i32) != 0)); +} +pub fn test_clock_gettime_1() { + let ts: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match nix::time::clock_gettime(nix::time::ClockId::from_raw(0)) { + Ok(__ts) => { + (ts.as_pointer()).with_mut(|__t| { + *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!(((((*(*ts.borrow()).tv_sec.borrow()) > 1500000000_i64) as i32) != 0)); + assert!( + (((((((*(*ts.borrow()).tv_nsec.borrow()) >= 0_i64) as i32) != 0) + && ((((*(*ts.borrow()).tv_nsec.borrow()) < 1000000000_i64) as i32) != 0)) + as i32) + != 0) + ); +} +pub fn print_tm_2(t: i64) { + let t: Value = Rc::new(RefCell::new(t)); + let tm: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((!(({ + let __res = (tm.as_pointer()); + match jiff::Timestamp::from_second((t.as_pointer()).read()) { + Ok(__ts) => { + let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); + __res.with_mut(|__tm| { + *__tm.tm_sec.borrow_mut() = __dt.second() as i32; + *__tm.tm_min.borrow_mut() = __dt.minute() as i32; + *__tm.tm_hour.borrow_mut() = __dt.hour() as i32; + *__tm.tm_mday.borrow_mut() = __dt.day() as i32; + *__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1; + *__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900; + *__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32; + *__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1; + *__tm.tm_isdst.borrow_mut() = 0; + *__tm.tm_gmtoff.borrow_mut() = 0; + *__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT"); + }); + __res + } + Err(_) => { + libcc2rs::cpp2rust_errno().write(::libc::EOVERFLOW); + Ptr::null() + } + } + }) + .is_null())) as i32) + != 0) + ); + println!( + "{}-{}-{} {}:{}:{} wday={} yday={}", + (*(*tm.borrow()).tm_year.borrow()), + (*(*tm.borrow()).tm_mon.borrow()), + (*(*tm.borrow()).tm_mday.borrow()), + (*(*tm.borrow()).tm_hour.borrow()), + (*(*tm.borrow()).tm_min.borrow()), + (*(*tm.borrow()).tm_sec.borrow()), + (*(*tm.borrow()).tm_wday.borrow()), + (*(*tm.borrow()).tm_yday.borrow()) + ); +} +pub fn test_gmtime_r_3() { + ({ print_tm_2(0_i64) }); + ({ print_tm_2(1_i64) }); + ({ print_tm_2(86399_i64) }); + ({ print_tm_2(86400_i64) }); + ({ print_tm_2(951782400_i64) }); + ({ print_tm_2(951868799_i64) }); + ({ print_tm_2(1704067199_i64) }); + ({ print_tm_2(1704067200_i64) }); + ({ print_tm_2(1721126096_i64) }); + ({ print_tm_2(4102444800_i64) }); +} +pub fn test_strftime_4() { + let t: Value = Rc::new(RefCell::new(1721126096_i64)); + let tm: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((!(({ + let __res = (tm.as_pointer()); + match jiff::Timestamp::from_second((t.as_pointer()).read()) { + Ok(__ts) => { + let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); + __res.with_mut(|__tm| { + *__tm.tm_sec.borrow_mut() = __dt.second() as i32; + *__tm.tm_min.borrow_mut() = __dt.minute() as i32; + *__tm.tm_hour.borrow_mut() = __dt.hour() as i32; + *__tm.tm_mday.borrow_mut() = __dt.day() as i32; + *__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1; + *__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900; + *__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32; + *__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1; + *__tm.tm_isdst.borrow_mut() = 0; + *__tm.tm_gmtoff.borrow_mut() = 0; + *__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT"); + }); + __res + } + Err(_) => { + libcc2rs::cpp2rust_errno().write(::libc::EOVERFLOW); + Ptr::null() + } + } + }) + .is_null())) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new( + (0..64).map(|_| ::default()).collect::>(), + )); + assert!( + ((({ + let __dt = (tm.as_pointer()).with(|__tm| { + jiff::civil::DateTime::new( + (*__tm.tm_year.borrow() + 1900) as i16, + (*__tm.tm_mon.borrow() + 1) as i8, + *__tm.tm_mday.borrow() as i8, + *__tm.tm_hour.borrow() as i8, + *__tm.tm_min.borrow() as i8, + *__tm.tm_sec.borrow() as i8, + 0, + ) + }); + let __text = match __dt { + Ok(__d) => jiff::fmt::strtime::format( + Ptr::from_string_literal(b"%Y-%m-%d %H:%M:%S") + .to_rust_string() + .as_str(), + __d, + ) + .unwrap_or_default(), + Err(_) => String::new(), + }; + if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { + 0 + } else { + let mut __dst = (buf.as_pointer() as Ptr).clone(); + for __b in __text.as_bytes() { + __dst.write(*__b); + __dst += 1; + } + __dst.write(0); + __text.len() + } + } > 0_usize) as i32) + != 0) + ); + println!("{}", (buf.as_pointer() as Ptr::)); + assert!( + ((({ + let __dt = (tm.as_pointer()).with(|__tm| { + jiff::civil::DateTime::new( + (*__tm.tm_year.borrow() + 1900) as i16, + (*__tm.tm_mon.borrow() + 1) as i8, + *__tm.tm_mday.borrow() as i8, + *__tm.tm_hour.borrow() as i8, + *__tm.tm_min.borrow() as i8, + *__tm.tm_sec.borrow() as i8, + 0, + ) + }); + let __text = match __dt { + Ok(__d) => jiff::fmt::strtime::format( + Ptr::from_string_literal(b"%a, %d %b %Y %T") + .to_rust_string() + .as_str(), + __d, + ) + .unwrap_or_default(), + Err(_) => String::new(), + }; + if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { + 0 + } else { + let mut __dst = (buf.as_pointer() as Ptr).clone(); + for __b in __text.as_bytes() { + __dst.write(*__b); + __dst += 1; + } + __dst.write(0); + __text.len() + } + } > 0_usize) as i32) + != 0) + ); + println!("{}", (buf.as_pointer() as Ptr::)); + assert!( + ((({ + let __dt = (tm.as_pointer()).with(|__tm| { + jiff::civil::DateTime::new( + (*__tm.tm_year.borrow() + 1900) as i16, + (*__tm.tm_mon.borrow() + 1) as i8, + *__tm.tm_mday.borrow() as i8, + *__tm.tm_hour.borrow() as i8, + *__tm.tm_min.borrow() as i8, + *__tm.tm_sec.borrow() as i8, + 0, + ) + }); + let __text = match __dt { + Ok(__d) => jiff::fmt::strtime::format( + Ptr::from_string_literal(b"day %j 100%%") + .to_rust_string() + .as_str(), + __d, + ) + .unwrap_or_default(), + Err(_) => String::new(), + }; + if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { + 0 + } else { + let mut __dst = (buf.as_pointer() as Ptr).clone(); + for __b in __text.as_bytes() { + __dst.write(*__b); + __dst += 1; + } + __dst.write(0); + __text.len() + } + } > 0_usize) as i32) + != 0) + ); + println!("{}", (buf.as_pointer() as Ptr::)); + assert!( + ((({ + let __dt = (tm.as_pointer()).with(|__tm| { + jiff::civil::DateTime::new( + (*__tm.tm_year.borrow() + 1900) as i16, + (*__tm.tm_mon.borrow() + 1) as i8, + *__tm.tm_mday.borrow() as i8, + *__tm.tm_hour.borrow() as i8, + *__tm.tm_min.borrow() as i8, + *__tm.tm_sec.borrow() as i8, + 0, + ) + }); + let __text = match __dt { + Ok(__d) => jiff::fmt::strtime::format( + Ptr::from_string_literal(b"%e").to_rust_string().as_str(), + __d, + ) + .unwrap_or_default(), + Err(_) => String::new(), + }; + if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { + 0 + } else { + let mut __dst = (buf.as_pointer() as Ptr).clone(); + for __b in __text.as_bytes() { + __dst.write(*__b); + __dst += 1; + } + __dst.write(0); + __text.len() + } + } > 0_usize) as i32) + != 0) + ); + println!("{}", (buf.as_pointer() as Ptr::)); + let small: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ::default()).collect::>(), + )); + assert!( + ((({ + let __dt = (tm.as_pointer()).with(|__tm| { + jiff::civil::DateTime::new( + (*__tm.tm_year.borrow() + 1900) as i16, + (*__tm.tm_mon.borrow() + 1) as i8, + *__tm.tm_mday.borrow() as i8, + *__tm.tm_hour.borrow() as i8, + *__tm.tm_min.borrow() as i8, + *__tm.tm_sec.borrow() as i8, + 0, + ) + }); + let __text = match __dt { + Ok(__d) => jiff::fmt::strtime::format( + Ptr::from_string_literal(b"%Y-%m-%d") + .to_rust_string() + .as_str(), + __d, + ) + .unwrap_or_default(), + Err(_) => String::new(), + }; + if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 4]>() { + 0 + } else { + let mut __dst = (small.as_pointer() as Ptr).clone(); + for __b in __text.as_bytes() { + __dst.write(*__b); + __dst += 1; + } + __dst.write(0); + __text.len() + } + } == 0_usize) as i32) + != 0) + ); +} +pub fn test_gettimeofday_5() { + let tv: Value = Rc::new(RefCell::new(Default::default())); + assert!( + (((match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + (tv.as_pointer()).with_mut(|__tv| { + *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + assert!(((((*(*tv.borrow()).tv_sec.borrow()) > 1500000000_i64) as i32) != 0)); + assert!( + (((((((*(*tv.borrow()).tv_usec.borrow()) >= 0_i64) as i32) != 0) + && ((((*(*tv.borrow()).tv_usec.borrow()) < 1000000_i64) as i32) != 0)) + as i32) + != 0) + ); +} +pub fn test_utimes_6() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_utimes_test.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"), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + let times: Value> = Rc::new(RefCell::new( + (0..2) + .map(|_| Default::default()) + .collect::>(), + )); + (*(*times.borrow())[(0) as usize].tv_sec.borrow_mut()) = 1000000000_i64; + (*(*times.borrow())[(0) as usize].tv_usec.borrow_mut()) = 0_i64; + (*(*times.borrow())[(1) as usize].tv_sec.borrow_mut()) = 1000000001_i64; + (*(*times.borrow())[(1) as usize].tv_usec.borrow_mut()) = 0_i64; + assert!( + ((({ + let __times = (times.as_pointer() as Ptr); + let __at = __times.with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + let __mt = __times.offset(1).with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + match nix::sys::stat::utimes((*path.borrow()).to_rust_string().as_str(), &__at, &__mt) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let __times = (times.as_pointer() as Ptr); + let __at = __times.with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + let __mt = __times.offset(1).with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + match nix::sys::stat::utimes( + Ptr::from_string_literal(b"/tmp/cpp2rust_utimes_test_missing.tmp") + .to_rust_string() + .as_str(), + &__at, + &__mt, + ) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == -1_i32) 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 main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_time_0() }); + ({ test_clock_gettime_1() }); + ({ test_gmtime_r_3() }); + ({ test_strftime_4() }); + ({ test_gettimeofday_5() }); + ({ test_utimes_6() }); + return 0; +} diff --git a/tests/unit/out/unsafe/sys_time.rs b/tests/unit/out/unsafe/sys_time.rs new file mode 100644 index 00000000..9e181bac --- /dev/null +++ b/tests/unit/out/unsafe/sys_time.rs @@ -0,0 +1,186 @@ +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 unsafe fn test_time_0() { + let mut t1: i64 = libc::time(std::ptr::null_mut()); + let mut t2: i64 = 0_i64; + let mut t3: i64 = libc::time((&mut t2 as *mut i64)); + assert!(((((t1) > (1500000000_i64)) as i32) != 0)); + assert!(((((t2) == (t3)) as i32) != 0)); + assert!(((((t3) >= (t1)) as i32) != 0)); +} +pub unsafe fn test_clock_gettime_1() { + let mut ts: ::libc::timespec = unsafe { std::mem::zeroed() }; + assert!(((((libc::clock_gettime(0, (&mut ts as *mut ::libc::timespec))) == (0)) as i32) != 0)); + assert!(((((ts.tv_sec) > (1500000000_i64)) as i32) != 0)); + assert!( + (((((((ts.tv_nsec) >= (0_i64)) as i32) != 0) + && ((((ts.tv_nsec) < (1000000000_i64)) as i32) != 0)) as i32) + != 0) + ); +} +pub unsafe fn print_tm_2(mut t: i64) { + let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; + assert!( + (((!((libc::gmtime_r( + (&mut t as *mut i64).cast_const(), + (&mut tm as *mut ::libc::tm) + )) + .is_null())) as i32) + != 0) + ); + printf( + (c"%d-%d-%d %d:%d:%d wday=%d yday=%d\n".as_ptr().cast_mut()).cast_const() as *const i8, + tm.tm_year, + tm.tm_mon, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec, + tm.tm_wday, + tm.tm_yday, + ); +} +pub unsafe fn test_gmtime_r_3() { + (unsafe { print_tm_2(0_i64) }); + (unsafe { print_tm_2(1_i64) }); + (unsafe { print_tm_2(86399_i64) }); + (unsafe { print_tm_2(86400_i64) }); + (unsafe { print_tm_2(951782400_i64) }); + (unsafe { print_tm_2(951868799_i64) }); + (unsafe { print_tm_2(1704067199_i64) }); + (unsafe { print_tm_2(1704067200_i64) }); + (unsafe { print_tm_2(1721126096_i64) }); + (unsafe { print_tm_2(4102444800_i64) }); +} +pub unsafe fn test_strftime_4() { + let mut t: i64 = 1721126096_i64; + let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; + assert!( + (((!((libc::gmtime_r( + (&mut t as *mut i64).cast_const(), + (&mut tm as *mut ::libc::tm) + )) + .is_null())) as i32) + != 0) + ); + let mut buf: [libc::c_char; 64] = [(0 as libc::c_char); 64]; + assert!( + ((((libc::strftime( + buf.as_mut_ptr(), + ::std::mem::size_of::<[libc::c_char; 64]>(), + (c"%Y-%m-%d %H:%M:%S".as_ptr().cast_mut()).cast_const(), + (&mut tm as *mut ::libc::tm).cast_const() + )) > (0_usize)) as i32) + != 0) + ); + printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, + buf.as_mut_ptr(), + ); + assert!( + ((((libc::strftime( + buf.as_mut_ptr(), + ::std::mem::size_of::<[libc::c_char; 64]>(), + (c"%a, %d %b %Y %T".as_ptr().cast_mut()).cast_const(), + (&mut tm as *mut ::libc::tm).cast_const() + )) > (0_usize)) as i32) + != 0) + ); + printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, + buf.as_mut_ptr(), + ); + assert!( + ((((libc::strftime( + buf.as_mut_ptr(), + ::std::mem::size_of::<[libc::c_char; 64]>(), + (c"day %j 100%%".as_ptr().cast_mut()).cast_const(), + (&mut tm as *mut ::libc::tm).cast_const() + )) > (0_usize)) as i32) + != 0) + ); + printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, + buf.as_mut_ptr(), + ); + assert!( + ((((libc::strftime( + buf.as_mut_ptr(), + ::std::mem::size_of::<[libc::c_char; 64]>(), + (c"%e".as_ptr().cast_mut()).cast_const(), + (&mut tm as *mut ::libc::tm).cast_const() + )) > (0_usize)) as i32) + != 0) + ); + printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, + buf.as_mut_ptr(), + ); + let mut small: [libc::c_char; 4] = [(0 as libc::c_char); 4]; + assert!( + ((((libc::strftime( + small.as_mut_ptr(), + ::std::mem::size_of::<[libc::c_char; 4]>(), + (c"%Y-%m-%d".as_ptr().cast_mut()).cast_const(), + (&mut tm as *mut ::libc::tm).cast_const() + )) == (0_usize)) as i32) + != 0) + ); +} +pub unsafe fn test_gettimeofday_5() { + let mut tv: ::libc::timeval = unsafe { std::mem::zeroed() }; + assert!( + ((((libc::gettimeofday( + (&mut tv as *mut ::libc::timeval), + (0 as *mut ::libc::c_void) as *mut libc::timezone + )) == (0)) as i32) + != 0) + ); + assert!(((((tv.tv_sec) > (1500000000_i64)) as i32) != 0)); + assert!( + (((((((tv.tv_usec) >= (0_i64)) as i32) != 0) + && ((((tv.tv_usec) < (1000000_i64)) as i32) != 0)) as i32) + != 0) + ); +} +pub unsafe fn test_utimes_6() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_utimes_test.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + let mut times: [::libc::timeval; 2] = [unsafe { std::mem::zeroed() }; 2]; + times[(0) as usize].tv_sec = 1000000000_i64; + times[(0) as usize].tv_usec = 0_i64; + times[(1) as usize].tv_sec = 1000000001_i64; + times[(1) as usize].tv_usec = 0_i64; + assert!(((((libc::utimes(path, (times.as_mut_ptr()).cast_const())) == (0)) as i32) != 0)); + assert!( + ((((libc::utimes( + (c"/tmp/cpp2rust_utimes_test_missing.tmp".as_ptr().cast_mut()).cast_const(), + (times.as_mut_ptr()).cast_const() + )) == (-1_i32)) as i32) + != 0) + ); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_time_0() }); + (unsafe { test_clock_gettime_1() }); + (unsafe { test_gmtime_r_3() }); + (unsafe { test_strftime_4() }); + (unsafe { test_gettimeofday_5() }); + (unsafe { test_utimes_6() }); + return 0; +} diff --git a/tests/unit/sys_time.c b/tests/unit/sys_time.c new file mode 100644 index 00000000..bc5f58b2 --- /dev/null +++ b/tests/unit/sys_time.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include + +static void test_time(void) { + time_t t1 = time(NULL); + time_t t2 = 0; + time_t t3 = time(&t2); + assert(t1 > 1500000000); + assert(t2 == t3); + assert(t3 >= t1); +} + +static void test_clock_gettime(void) { + struct timespec ts; + assert(clock_gettime(CLOCK_REALTIME, &ts) == 0); + assert(ts.tv_sec > 1500000000); + assert(ts.tv_nsec >= 0 && ts.tv_nsec < 1000000000); +} + +static void print_tm(time_t t) { + struct tm tm; + assert(gmtime_r(&t, &tm) != NULL); + printf("%d-%d-%d %d:%d:%d wday=%d yday=%d\n", tm.tm_year, tm.tm_mon, + tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday, tm.tm_yday); +} + +static void test_gmtime_r(void) { + print_tm(0); + print_tm(1); + print_tm(86399); + print_tm(86400); + print_tm(951782400); /* leap day */ + print_tm(951868799); + print_tm(1704067199); /* year boundary */ + print_tm(1704067200); + print_tm(1721126096); + print_tm(4102444800); +} + +static void test_strftime(void) { + time_t t = 1721126096; + struct tm tm; + assert(gmtime_r(&t, &tm) != NULL); + char buf[64]; + assert(strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm) > 0); + printf("%s\n", buf); + assert(strftime(buf, sizeof(buf), "%a, %d %b %Y %T", &tm) > 0); + printf("%s\n", buf); + assert(strftime(buf, sizeof(buf), "day %j 100%%", &tm) > 0); + printf("%s\n", buf); + assert(strftime(buf, sizeof(buf), "%e", &tm) > 0); + printf("%s\n", buf); + char small[4]; + assert(strftime(small, sizeof(small), "%Y-%m-%d", &tm) == 0); +} + +static void test_gettimeofday(void) { + struct timeval tv; + assert(gettimeofday(&tv, NULL) == 0); + assert(tv.tv_sec > 1500000000); + assert(tv.tv_usec >= 0 && tv.tv_usec < 1000000); +} + +static void test_utimes(void) { + const char *path = "/tmp/cpp2rust_utimes_test.tmp"; + FILE *fp = fopen(path, "wb"); + assert(fp != NULL); + assert(fclose(fp) == 0); + struct timeval times[2]; + times[0].tv_sec = 1000000000; + times[0].tv_usec = 0; + times[1].tv_sec = 1000000001; + times[1].tv_usec = 0; + assert(utimes(path, times) == 0); + assert(utimes("/tmp/cpp2rust_utimes_test_missing.tmp", times) == -1); + assert(unlink(path) == 0); +} + +int main(void) { + test_time(); + test_clock_gettime(); + test_gmtime_r(); + test_strftime(); + test_gettimeofday(); + test_utimes(); + return 0; +} From b32c7da8911dd6310acad43846a05fd2a7162988 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 21:00:49 +0100 Subject: [PATCH 4/4] Delete unportable test The output between linux and macos differs --- tests/unit/out/refcount/sys_time.rs | 499 ---------------------------- tests/unit/out/unsafe/sys_time.rs | 186 ----------- tests/unit/sys_time.c | 90 ----- 3 files changed, 775 deletions(-) delete mode 100644 tests/unit/out/refcount/sys_time.rs delete mode 100644 tests/unit/out/unsafe/sys_time.rs delete mode 100644 tests/unit/sys_time.c diff --git a/tests/unit/out/refcount/sys_time.rs b/tests/unit/out/refcount/sys_time.rs deleted file mode 100644 index 295c2000..00000000 --- a/tests/unit/out/refcount/sys_time.rs +++ /dev/null @@ -1,499 +0,0 @@ -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_time_0() { - let t1: Value = Rc::new(RefCell::new({ - let __out = Ptr::::null(); - match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { - Ok(__ts) => { - let __s = __ts.tv_sec(); - if !__out.is_null() { - __out.write(__s); - } - __s - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - let t2: Value = Rc::new(RefCell::new(0_i64)); - let t3: Value = Rc::new(RefCell::new({ - let __out = (t2.as_pointer()); - match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { - Ok(__ts) => { - let __s = __ts.tv_sec(); - if !__out.is_null() { - __out.write(__s); - } - __s - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - })); - assert!(((((*t1.borrow()) > 1500000000_i64) as i32) != 0)); - assert!(((((*t2.borrow()) == (*t3.borrow())) as i32) != 0)); - assert!(((((*t3.borrow()) >= (*t1.borrow())) as i32) != 0)); -} -pub fn test_clock_gettime_1() { - let ts: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match nix::time::clock_gettime(nix::time::ClockId::from_raw(0)) { - Ok(__ts) => { - (ts.as_pointer()).with_mut(|__t| { - *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; - *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; - }); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!(((((*(*ts.borrow()).tv_sec.borrow()) > 1500000000_i64) as i32) != 0)); - assert!( - (((((((*(*ts.borrow()).tv_nsec.borrow()) >= 0_i64) as i32) != 0) - && ((((*(*ts.borrow()).tv_nsec.borrow()) < 1000000000_i64) as i32) != 0)) - as i32) - != 0) - ); -} -pub fn print_tm_2(t: i64) { - let t: Value = Rc::new(RefCell::new(t)); - let tm: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((!(({ - let __res = (tm.as_pointer()); - match jiff::Timestamp::from_second((t.as_pointer()).read()) { - Ok(__ts) => { - let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); - __res.with_mut(|__tm| { - *__tm.tm_sec.borrow_mut() = __dt.second() as i32; - *__tm.tm_min.borrow_mut() = __dt.minute() as i32; - *__tm.tm_hour.borrow_mut() = __dt.hour() as i32; - *__tm.tm_mday.borrow_mut() = __dt.day() as i32; - *__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1; - *__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900; - *__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32; - *__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1; - *__tm.tm_isdst.borrow_mut() = 0; - *__tm.tm_gmtoff.borrow_mut() = 0; - *__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT"); - }); - __res - } - Err(_) => { - libcc2rs::cpp2rust_errno().write(::libc::EOVERFLOW); - Ptr::null() - } - } - }) - .is_null())) as i32) - != 0) - ); - println!( - "{}-{}-{} {}:{}:{} wday={} yday={}", - (*(*tm.borrow()).tm_year.borrow()), - (*(*tm.borrow()).tm_mon.borrow()), - (*(*tm.borrow()).tm_mday.borrow()), - (*(*tm.borrow()).tm_hour.borrow()), - (*(*tm.borrow()).tm_min.borrow()), - (*(*tm.borrow()).tm_sec.borrow()), - (*(*tm.borrow()).tm_wday.borrow()), - (*(*tm.borrow()).tm_yday.borrow()) - ); -} -pub fn test_gmtime_r_3() { - ({ print_tm_2(0_i64) }); - ({ print_tm_2(1_i64) }); - ({ print_tm_2(86399_i64) }); - ({ print_tm_2(86400_i64) }); - ({ print_tm_2(951782400_i64) }); - ({ print_tm_2(951868799_i64) }); - ({ print_tm_2(1704067199_i64) }); - ({ print_tm_2(1704067200_i64) }); - ({ print_tm_2(1721126096_i64) }); - ({ print_tm_2(4102444800_i64) }); -} -pub fn test_strftime_4() { - let t: Value = Rc::new(RefCell::new(1721126096_i64)); - let tm: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((!(({ - let __res = (tm.as_pointer()); - match jiff::Timestamp::from_second((t.as_pointer()).read()) { - Ok(__ts) => { - let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); - __res.with_mut(|__tm| { - *__tm.tm_sec.borrow_mut() = __dt.second() as i32; - *__tm.tm_min.borrow_mut() = __dt.minute() as i32; - *__tm.tm_hour.borrow_mut() = __dt.hour() as i32; - *__tm.tm_mday.borrow_mut() = __dt.day() as i32; - *__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1; - *__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900; - *__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32; - *__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1; - *__tm.tm_isdst.borrow_mut() = 0; - *__tm.tm_gmtoff.borrow_mut() = 0; - *__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT"); - }); - __res - } - Err(_) => { - libcc2rs::cpp2rust_errno().write(::libc::EOVERFLOW); - Ptr::null() - } - } - }) - .is_null())) as i32) - != 0) - ); - let buf: Value> = Rc::new(RefCell::new( - (0..64).map(|_| ::default()).collect::>(), - )); - assert!( - ((({ - let __dt = (tm.as_pointer()).with(|__tm| { - jiff::civil::DateTime::new( - (*__tm.tm_year.borrow() + 1900) as i16, - (*__tm.tm_mon.borrow() + 1) as i8, - *__tm.tm_mday.borrow() as i8, - *__tm.tm_hour.borrow() as i8, - *__tm.tm_min.borrow() as i8, - *__tm.tm_sec.borrow() as i8, - 0, - ) - }); - let __text = match __dt { - Ok(__d) => jiff::fmt::strtime::format( - Ptr::from_string_literal(b"%Y-%m-%d %H:%M:%S") - .to_rust_string() - .as_str(), - __d, - ) - .unwrap_or_default(), - Err(_) => String::new(), - }; - if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { - 0 - } else { - let mut __dst = (buf.as_pointer() as Ptr).clone(); - for __b in __text.as_bytes() { - __dst.write(*__b); - __dst += 1; - } - __dst.write(0); - __text.len() - } - } > 0_usize) as i32) - != 0) - ); - println!("{}", (buf.as_pointer() as Ptr::)); - assert!( - ((({ - let __dt = (tm.as_pointer()).with(|__tm| { - jiff::civil::DateTime::new( - (*__tm.tm_year.borrow() + 1900) as i16, - (*__tm.tm_mon.borrow() + 1) as i8, - *__tm.tm_mday.borrow() as i8, - *__tm.tm_hour.borrow() as i8, - *__tm.tm_min.borrow() as i8, - *__tm.tm_sec.borrow() as i8, - 0, - ) - }); - let __text = match __dt { - Ok(__d) => jiff::fmt::strtime::format( - Ptr::from_string_literal(b"%a, %d %b %Y %T") - .to_rust_string() - .as_str(), - __d, - ) - .unwrap_or_default(), - Err(_) => String::new(), - }; - if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { - 0 - } else { - let mut __dst = (buf.as_pointer() as Ptr).clone(); - for __b in __text.as_bytes() { - __dst.write(*__b); - __dst += 1; - } - __dst.write(0); - __text.len() - } - } > 0_usize) as i32) - != 0) - ); - println!("{}", (buf.as_pointer() as Ptr::)); - assert!( - ((({ - let __dt = (tm.as_pointer()).with(|__tm| { - jiff::civil::DateTime::new( - (*__tm.tm_year.borrow() + 1900) as i16, - (*__tm.tm_mon.borrow() + 1) as i8, - *__tm.tm_mday.borrow() as i8, - *__tm.tm_hour.borrow() as i8, - *__tm.tm_min.borrow() as i8, - *__tm.tm_sec.borrow() as i8, - 0, - ) - }); - let __text = match __dt { - Ok(__d) => jiff::fmt::strtime::format( - Ptr::from_string_literal(b"day %j 100%%") - .to_rust_string() - .as_str(), - __d, - ) - .unwrap_or_default(), - Err(_) => String::new(), - }; - if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { - 0 - } else { - let mut __dst = (buf.as_pointer() as Ptr).clone(); - for __b in __text.as_bytes() { - __dst.write(*__b); - __dst += 1; - } - __dst.write(0); - __text.len() - } - } > 0_usize) as i32) - != 0) - ); - println!("{}", (buf.as_pointer() as Ptr::)); - assert!( - ((({ - let __dt = (tm.as_pointer()).with(|__tm| { - jiff::civil::DateTime::new( - (*__tm.tm_year.borrow() + 1900) as i16, - (*__tm.tm_mon.borrow() + 1) as i8, - *__tm.tm_mday.borrow() as i8, - *__tm.tm_hour.borrow() as i8, - *__tm.tm_min.borrow() as i8, - *__tm.tm_sec.borrow() as i8, - 0, - ) - }); - let __text = match __dt { - Ok(__d) => jiff::fmt::strtime::format( - Ptr::from_string_literal(b"%e").to_rust_string().as_str(), - __d, - ) - .unwrap_or_default(), - Err(_) => String::new(), - }; - if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 64]>() { - 0 - } else { - let mut __dst = (buf.as_pointer() as Ptr).clone(); - for __b in __text.as_bytes() { - __dst.write(*__b); - __dst += 1; - } - __dst.write(0); - __text.len() - } - } > 0_usize) as i32) - != 0) - ); - println!("{}", (buf.as_pointer() as Ptr::)); - let small: Value> = Rc::new(RefCell::new( - (0..4).map(|_| ::default()).collect::>(), - )); - assert!( - ((({ - let __dt = (tm.as_pointer()).with(|__tm| { - jiff::civil::DateTime::new( - (*__tm.tm_year.borrow() + 1900) as i16, - (*__tm.tm_mon.borrow() + 1) as i8, - *__tm.tm_mday.borrow() as i8, - *__tm.tm_hour.borrow() as i8, - *__tm.tm_min.borrow() as i8, - *__tm.tm_sec.borrow() as i8, - 0, - ) - }); - let __text = match __dt { - Ok(__d) => jiff::fmt::strtime::format( - Ptr::from_string_literal(b"%Y-%m-%d") - .to_rust_string() - .as_str(), - __d, - ) - .unwrap_or_default(), - Err(_) => String::new(), - }; - if __text.is_empty() || __text.len() + 1 > ::std::mem::size_of::<[u8; 4]>() { - 0 - } else { - let mut __dst = (small.as_pointer() as Ptr).clone(); - for __b in __text.as_bytes() { - __dst.write(*__b); - __dst += 1; - } - __dst.write(0); - __text.len() - } - } == 0_usize) as i32) - != 0) - ); -} -pub fn test_gettimeofday_5() { - let tv: Value = Rc::new(RefCell::new(Default::default())); - assert!( - (((match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { - Ok(__ts) => { - (tv.as_pointer()).with_mut(|__tv| { - *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; - *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; - }); - 0 - } - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } == 0) as i32) - != 0) - ); - assert!(((((*(*tv.borrow()).tv_sec.borrow()) > 1500000000_i64) as i32) != 0)); - assert!( - (((((((*(*tv.borrow()).tv_usec.borrow()) >= 0_i64) as i32) != 0) - && ((((*(*tv.borrow()).tv_usec.borrow()) < 1000000_i64) as i32) != 0)) - as i32) - != 0) - ); -} -pub fn test_utimes_6() { - let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( - b"/tmp/cpp2rust_utimes_test.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"), - }, - )); - assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); - assert!( - ((({ - (*fp.borrow()).delete(); - 0 - } == 0) as i32) - != 0) - ); - let times: Value> = Rc::new(RefCell::new( - (0..2) - .map(|_| Default::default()) - .collect::>(), - )); - (*(*times.borrow())[(0) as usize].tv_sec.borrow_mut()) = 1000000000_i64; - (*(*times.borrow())[(0) as usize].tv_usec.borrow_mut()) = 0_i64; - (*(*times.borrow())[(1) as usize].tv_sec.borrow_mut()) = 1000000001_i64; - (*(*times.borrow())[(1) as usize].tv_usec.borrow_mut()) = 0_i64; - assert!( - ((({ - let __times = (times.as_pointer() as Ptr); - let __at = __times.with(|__tv| { - nix::sys::time::TimeVal::new( - *__tv.tv_sec.borrow() as ::libc::time_t, - *__tv.tv_usec.borrow() as ::libc::suseconds_t, - ) - }); - let __mt = __times.offset(1).with(|__tv| { - nix::sys::time::TimeVal::new( - *__tv.tv_sec.borrow() as ::libc::time_t, - *__tv.tv_usec.borrow() as ::libc::suseconds_t, - ) - }); - match nix::sys::stat::utimes((*path.borrow()).to_rust_string().as_str(), &__at, &__mt) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == 0) as i32) - != 0) - ); - assert!( - ((({ - let __times = (times.as_pointer() as Ptr); - let __at = __times.with(|__tv| { - nix::sys::time::TimeVal::new( - *__tv.tv_sec.borrow() as ::libc::time_t, - *__tv.tv_usec.borrow() as ::libc::suseconds_t, - ) - }); - let __mt = __times.offset(1).with(|__tv| { - nix::sys::time::TimeVal::new( - *__tv.tv_sec.borrow() as ::libc::time_t, - *__tv.tv_usec.borrow() as ::libc::suseconds_t, - ) - }); - match nix::sys::stat::utimes( - Ptr::from_string_literal(b"/tmp/cpp2rust_utimes_test_missing.tmp") - .to_rust_string() - .as_str(), - &__at, - &__mt, - ) { - Ok(()) => 0, - Err(__e) => { - libcc2rs::cpp2rust_errno().write(__e as i32); - -1 - } - } - } == -1_i32) 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 main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - ({ test_time_0() }); - ({ test_clock_gettime_1() }); - ({ test_gmtime_r_3() }); - ({ test_strftime_4() }); - ({ test_gettimeofday_5() }); - ({ test_utimes_6() }); - return 0; -} diff --git a/tests/unit/out/unsafe/sys_time.rs b/tests/unit/out/unsafe/sys_time.rs deleted file mode 100644 index 9e181bac..00000000 --- a/tests/unit/out/unsafe/sys_time.rs +++ /dev/null @@ -1,186 +0,0 @@ -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 unsafe fn test_time_0() { - let mut t1: i64 = libc::time(std::ptr::null_mut()); - let mut t2: i64 = 0_i64; - let mut t3: i64 = libc::time((&mut t2 as *mut i64)); - assert!(((((t1) > (1500000000_i64)) as i32) != 0)); - assert!(((((t2) == (t3)) as i32) != 0)); - assert!(((((t3) >= (t1)) as i32) != 0)); -} -pub unsafe fn test_clock_gettime_1() { - let mut ts: ::libc::timespec = unsafe { std::mem::zeroed() }; - assert!(((((libc::clock_gettime(0, (&mut ts as *mut ::libc::timespec))) == (0)) as i32) != 0)); - assert!(((((ts.tv_sec) > (1500000000_i64)) as i32) != 0)); - assert!( - (((((((ts.tv_nsec) >= (0_i64)) as i32) != 0) - && ((((ts.tv_nsec) < (1000000000_i64)) as i32) != 0)) as i32) - != 0) - ); -} -pub unsafe fn print_tm_2(mut t: i64) { - let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; - assert!( - (((!((libc::gmtime_r( - (&mut t as *mut i64).cast_const(), - (&mut tm as *mut ::libc::tm) - )) - .is_null())) as i32) - != 0) - ); - printf( - (c"%d-%d-%d %d:%d:%d wday=%d yday=%d\n".as_ptr().cast_mut()).cast_const() as *const i8, - tm.tm_year, - tm.tm_mon, - tm.tm_mday, - tm.tm_hour, - tm.tm_min, - tm.tm_sec, - tm.tm_wday, - tm.tm_yday, - ); -} -pub unsafe fn test_gmtime_r_3() { - (unsafe { print_tm_2(0_i64) }); - (unsafe { print_tm_2(1_i64) }); - (unsafe { print_tm_2(86399_i64) }); - (unsafe { print_tm_2(86400_i64) }); - (unsafe { print_tm_2(951782400_i64) }); - (unsafe { print_tm_2(951868799_i64) }); - (unsafe { print_tm_2(1704067199_i64) }); - (unsafe { print_tm_2(1704067200_i64) }); - (unsafe { print_tm_2(1721126096_i64) }); - (unsafe { print_tm_2(4102444800_i64) }); -} -pub unsafe fn test_strftime_4() { - let mut t: i64 = 1721126096_i64; - let mut tm: ::libc::tm = unsafe { std::mem::zeroed() }; - assert!( - (((!((libc::gmtime_r( - (&mut t as *mut i64).cast_const(), - (&mut tm as *mut ::libc::tm) - )) - .is_null())) as i32) - != 0) - ); - let mut buf: [libc::c_char; 64] = [(0 as libc::c_char); 64]; - assert!( - ((((libc::strftime( - buf.as_mut_ptr(), - ::std::mem::size_of::<[libc::c_char; 64]>(), - (c"%Y-%m-%d %H:%M:%S".as_ptr().cast_mut()).cast_const(), - (&mut tm as *mut ::libc::tm).cast_const() - )) > (0_usize)) as i32) - != 0) - ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); - assert!( - ((((libc::strftime( - buf.as_mut_ptr(), - ::std::mem::size_of::<[libc::c_char; 64]>(), - (c"%a, %d %b %Y %T".as_ptr().cast_mut()).cast_const(), - (&mut tm as *mut ::libc::tm).cast_const() - )) > (0_usize)) as i32) - != 0) - ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); - assert!( - ((((libc::strftime( - buf.as_mut_ptr(), - ::std::mem::size_of::<[libc::c_char; 64]>(), - (c"day %j 100%%".as_ptr().cast_mut()).cast_const(), - (&mut tm as *mut ::libc::tm).cast_const() - )) > (0_usize)) as i32) - != 0) - ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); - assert!( - ((((libc::strftime( - buf.as_mut_ptr(), - ::std::mem::size_of::<[libc::c_char; 64]>(), - (c"%e".as_ptr().cast_mut()).cast_const(), - (&mut tm as *mut ::libc::tm).cast_const() - )) > (0_usize)) as i32) - != 0) - ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); - let mut small: [libc::c_char; 4] = [(0 as libc::c_char); 4]; - assert!( - ((((libc::strftime( - small.as_mut_ptr(), - ::std::mem::size_of::<[libc::c_char; 4]>(), - (c"%Y-%m-%d".as_ptr().cast_mut()).cast_const(), - (&mut tm as *mut ::libc::tm).cast_const() - )) == (0_usize)) as i32) - != 0) - ); -} -pub unsafe fn test_gettimeofday_5() { - let mut tv: ::libc::timeval = unsafe { std::mem::zeroed() }; - assert!( - ((((libc::gettimeofday( - (&mut tv as *mut ::libc::timeval), - (0 as *mut ::libc::c_void) as *mut libc::timezone - )) == (0)) as i32) - != 0) - ); - assert!(((((tv.tv_sec) > (1500000000_i64)) as i32) != 0)); - assert!( - (((((((tv.tv_usec) >= (0_i64)) as i32) != 0) - && ((((tv.tv_usec) < (1000000_i64)) as i32) != 0)) as i32) - != 0) - ); -} -pub unsafe fn test_utimes_6() { - let mut path: *const libc::c_char = - (c"/tmp/cpp2rust_utimes_test.tmp".as_ptr().cast_mut()).cast_const(); - let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); - assert!((((!((fp).is_null())) as i32) != 0)); - assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); - let mut times: [::libc::timeval; 2] = [unsafe { std::mem::zeroed() }; 2]; - times[(0) as usize].tv_sec = 1000000000_i64; - times[(0) as usize].tv_usec = 0_i64; - times[(1) as usize].tv_sec = 1000000001_i64; - times[(1) as usize].tv_usec = 0_i64; - assert!(((((libc::utimes(path, (times.as_mut_ptr()).cast_const())) == (0)) as i32) != 0)); - assert!( - ((((libc::utimes( - (c"/tmp/cpp2rust_utimes_test_missing.tmp".as_ptr().cast_mut()).cast_const(), - (times.as_mut_ptr()).cast_const() - )) == (-1_i32)) as i32) - != 0) - ); - assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); -} -pub fn main() { - unsafe { - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_time_0() }); - (unsafe { test_clock_gettime_1() }); - (unsafe { test_gmtime_r_3() }); - (unsafe { test_strftime_4() }); - (unsafe { test_gettimeofday_5() }); - (unsafe { test_utimes_6() }); - return 0; -} diff --git a/tests/unit/sys_time.c b/tests/unit/sys_time.c deleted file mode 100644 index bc5f58b2..00000000 --- a/tests/unit/sys_time.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#include -#include - -static void test_time(void) { - time_t t1 = time(NULL); - time_t t2 = 0; - time_t t3 = time(&t2); - assert(t1 > 1500000000); - assert(t2 == t3); - assert(t3 >= t1); -} - -static void test_clock_gettime(void) { - struct timespec ts; - assert(clock_gettime(CLOCK_REALTIME, &ts) == 0); - assert(ts.tv_sec > 1500000000); - assert(ts.tv_nsec >= 0 && ts.tv_nsec < 1000000000); -} - -static void print_tm(time_t t) { - struct tm tm; - assert(gmtime_r(&t, &tm) != NULL); - printf("%d-%d-%d %d:%d:%d wday=%d yday=%d\n", tm.tm_year, tm.tm_mon, - tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday, tm.tm_yday); -} - -static void test_gmtime_r(void) { - print_tm(0); - print_tm(1); - print_tm(86399); - print_tm(86400); - print_tm(951782400); /* leap day */ - print_tm(951868799); - print_tm(1704067199); /* year boundary */ - print_tm(1704067200); - print_tm(1721126096); - print_tm(4102444800); -} - -static void test_strftime(void) { - time_t t = 1721126096; - struct tm tm; - assert(gmtime_r(&t, &tm) != NULL); - char buf[64]; - assert(strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm) > 0); - printf("%s\n", buf); - assert(strftime(buf, sizeof(buf), "%a, %d %b %Y %T", &tm) > 0); - printf("%s\n", buf); - assert(strftime(buf, sizeof(buf), "day %j 100%%", &tm) > 0); - printf("%s\n", buf); - assert(strftime(buf, sizeof(buf), "%e", &tm) > 0); - printf("%s\n", buf); - char small[4]; - assert(strftime(small, sizeof(small), "%Y-%m-%d", &tm) == 0); -} - -static void test_gettimeofday(void) { - struct timeval tv; - assert(gettimeofday(&tv, NULL) == 0); - assert(tv.tv_sec > 1500000000); - assert(tv.tv_usec >= 0 && tv.tv_usec < 1000000); -} - -static void test_utimes(void) { - const char *path = "/tmp/cpp2rust_utimes_test.tmp"; - FILE *fp = fopen(path, "wb"); - assert(fp != NULL); - assert(fclose(fp) == 0); - struct timeval times[2]; - times[0].tv_sec = 1000000000; - times[0].tv_usec = 0; - times[1].tv_sec = 1000000001; - times[1].tv_usec = 0; - assert(utimes(path, times) == 0); - assert(utimes("/tmp/cpp2rust_utimes_test_missing.tmp", times) == -1); - assert(unlink(path) == 0); -} - -int main(void) { - test_time(); - test_clock_gettime(); - test_gmtime_r(); - test_strftime(); - test_gettimeofday(); - test_utimes(); - return 0; -}