Browse Source

Merge pull request #6 from ian-h-chamberlain/clock_gettime

pull/7/head
Meziu 3 years ago committed by GitHub
parent
commit
ce703e7c06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      src/lib.rs

31
src/lib.rs

@ -1,5 +1,8 @@
#![no_std] #![no_std]
use core::mem::MaybeUninit;
use core::ptr;
extern crate libc; extern crate libc;
/// Call this somewhere to force Rust to link this module. /// 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 /// See https://github.com/rust-lang/rust/issues/47384
pub fn init() {} 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] #[no_mangle]
extern "C" fn posix_memalign( extern "C" fn posix_memalign(
memptr: *mut *mut libc::c_void, memptr: *mut *mut libc::c_void,
@ -36,3 +44,26 @@ unsafe extern "C" fn realpath(
resolved_path 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
}

Loading…
Cancel
Save