diff --git a/rules/cstdlib/tgt_refcount.rs b/rules/cstdlib/tgt_refcount.rs index cb04f452..a965f5b0 100644 --- a/rules/cstdlib/tgt_refcount.rs +++ b/rules/cstdlib/tgt_refcount.rs @@ -30,6 +30,26 @@ fn f6(a0: Ptr) -> Ptr { } } +fn f10(a0: Ptr, a1: Ptr) -> Ptr { + let __resolved = a1.clone(); + match ::std::fs::canonicalize(a0.to_rust_string()) { + Ok(__p) => { + let mut __bytes = __p.into_os_string().into_encoded_bytes(); + __bytes.push(0); + if __resolved.is_null() { + Ptr::alloc_array(__bytes.into_boxed_slice()) + } else { + __resolved.with_slice_mut(__bytes.len(), |__s| __s.copy_from_slice(&__bytes)); + __resolved + } + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + Ptr::null() + } + } +} + fn f8(a0: AnyPtr, a1: AnyPtr, a2: usize, a3: usize, a4: fn(AnyPtr, AnyPtr) -> i32) -> AnyPtr { let __base = a1.reinterpret_cast::(); let mut __lo: isize = 0; diff --git a/tests/unit/out/unsafe/stdlib_h.rs b/tests/unit/out/unsafe/stdlib_h.rs index 2a9d6e03..674bcb94 100644 --- a/tests/unit/out/unsafe/stdlib_h.rs +++ b/tests/unit/out/unsafe/stdlib_h.rs @@ -36,6 +36,42 @@ pub unsafe fn test_setenv_getenv_0() { ((((libc::strcmp(v, (c"replaced".as_ptr().cast_mut()).cast_const())) == (0)) as i32) != 0) ); } +pub unsafe fn test_realpath_1() { + let mut buf: [libc::c_char; 4096] = [(0 as libc::c_char); 4096]; + assert!( + (((!((libc::realpath((c"/".as_ptr().cast_mut()).cast_const(), buf.as_mut_ptr())).is_null())) + as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (buf.as_mut_ptr()).cast_const(), + (c"/".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + let mut p: *mut libc::c_char = libc::realpath( + (c"/".as_ptr().cast_mut()).cast_const(), + std::ptr::null_mut(), + ); + assert!((((!((p).is_null())) as i32) != 0)); + assert!( + ((((libc::strcmp((p).cast_const(), (c"/".as_ptr().cast_mut()).cast_const())) == (0)) + as i32) + != 0) + ); + libcc2rs::free_unsafe((p as *mut libc::c_char as *mut ::libc::c_void)); + (*libcc2rs::cpp2rust_errno_unsafe()) = 0; + assert!( + ((((libc::realpath( + (c"/cpp2rust_definitely_missing".as_ptr().cast_mut()).cast_const(), + buf.as_mut_ptr() + )) + .is_null()) as i32) + != 0) + ); + assert!(((((*libcc2rs::cpp2rust_errno_unsafe()) == (2)) as i32) != 0)); +} pub fn main() { unsafe { std::process::exit(main_0() as i32); @@ -43,5 +79,6 @@ pub fn main() { } unsafe fn main_0() -> i32 { (unsafe { test_setenv_getenv_0() }); + (unsafe { test_realpath_1() }); return 0; } diff --git a/tests/unit/stdlib_h.c b/tests/unit/stdlib_h.c index 724a9a17..91250b56 100644 --- a/tests/unit/stdlib_h.c +++ b/tests/unit/stdlib_h.c @@ -1,5 +1,6 @@ // no-compile: refcount #include +#include #include #include @@ -14,7 +15,21 @@ static void test_setenv_getenv(void) { assert(strcmp(v, "replaced") == 0); } +static void test_realpath(void) { + char buf[4096]; + assert(realpath("/", buf) != NULL); + assert(strcmp(buf, "/") == 0); + char *p = realpath("/", NULL); + assert(p != NULL); + assert(strcmp(p, "/") == 0); + free(p); + errno = 0; + assert(realpath("/cpp2rust_definitely_missing", buf) == NULL); + assert(errno == ENOENT); +} + int main(void) { test_setenv_getenv(); + test_realpath(); return 0; }