diff --git a/src/lib.rs b/src/lib.rs index 3385b95..7783976 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,8 @@ #![no_std] +use core::mem::MaybeUninit; +use core::ptr; + extern crate libc; /// Call this somewhere to force Rust to link this module. @@ -8,6 +11,11 @@ extern crate libc; /// See https://github.com/rust-lang/rust/issues/47384 pub fn init() {} +extern "C" { + // Not provided by libc: https://github.com/rust-lang/libc/issues/1995 + fn __errno() -> *mut libc::c_int; +} + #[no_mangle] extern "C" fn posix_memalign( memptr: *mut *mut libc::c_void, @@ -36,3 +44,26 @@ unsafe extern "C" fn realpath( resolved_path } + +#[no_mangle] +unsafe extern "C" fn clock_gettime( + clock_id: libc::clockid_t, + tp: *mut libc::timespec, +) -> libc::c_int { + let mut retval = -1; + match clock_id { + libc::CLOCK_REALTIME => { + let mut tv = MaybeUninit::uninit(); + + retval = libc::gettimeofday(tv.as_mut_ptr(), ptr::null_mut()); + if retval == 0 { + let tv = tv.assume_init(); + (*tp).tv_nsec = tv.tv_usec * 1000; + (*tp).tv_sec = tv.tv_sec; + } + } + _ => *__errno() = libc::EINVAL, + } + + retval +}