浏览代码

Implement gettimeofday

jD91mZM2 6 年之前
父节点
当前提交
a0f2baff12

+ 4 - 0
src/platform/src/linux/mod.rs

@@ -202,6 +202,10 @@ pub fn getsockopt(
     }) as c_int
 }
 
+pub fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int {
+    e(unsafe { syscall!(GETTIMEOFDAY, tp, tzp) }) as c_int
+}
+
 pub fn getuid() -> uid_t {
     e(unsafe { syscall!(GETUID) })
 }

+ 18 - 0
src/platform/src/redox/mod.rs

@@ -431,6 +431,24 @@ pub fn getsockopt(
     -1
 }
 
+pub fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int {
+    let mut redox_tp = redox_timespec::default();
+    let err = e(syscall::clock_gettime(syscall::CLOCK_REALTIME, &mut redox_tp)) as c_int;
+    if err < 0 {
+        return err;
+    }
+    unsafe {
+        (*tp).tv_sec = redox_tp.tv_sec as time_t;
+        (*tp).tv_usec = (redox_tp.tv_nsec / 1000) as suseconds_t;
+
+        if !tzp.is_null() {
+            (*tzp).tz_minuteswest = 0;
+            (*tzp).tz_dsttime = 0;
+        }
+    }
+    0
+}
+
 pub fn getuid() -> uid_t {
     e(syscall::getuid()) as pid_t
 }

+ 12 - 0
src/platform/src/types.rs

@@ -77,6 +77,18 @@ pub struct timespec {
     pub tv_nsec: c_long,
 }
 
+#[repr(C)]
+pub struct timeval {
+    pub tv_sec: time_t,
+    pub tv_usec: suseconds_t,
+}
+#[repr(C)]
+#[derive(Default)]
+pub struct timezone {
+    pub tz_minuteswest: c_int,
+    pub tz_dsttime: c_int,
+}
+
 #[cfg(target_os = "redox")]
 impl<'a> From<&'a timespec> for redox_timespec {
     fn from(tp: &timespec) -> redox_timespec {

+ 8 - 3
src/sys_time/src/lib.rs

@@ -11,6 +11,11 @@ pub struct timeval {
     pub tv_sec: time_t,
     pub tv_usec: suseconds_t,
 }
+#[repr(C)]
+pub struct timezone {
+    pub tz_minuteswest: c_int,
+    pub tz_dsttime: c_int,
+}
 
 #[repr(C)]
 pub struct itimerval {
@@ -37,9 +42,9 @@ pub extern "C" fn setitimer(
     unimplemented!();
 }
 
-// #[no_mangle]
-pub extern "C" fn gettimeofday(tp: *mut timeval, tzp: *const c_void) -> c_int {
-    unimplemented!();
+#[no_mangle]
+pub extern "C" fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int {
+    platform::gettimeofday(tp as *mut platform::types::timeval, tzp as *mut platform::types::timezone)
 }
 
 // #[no_mangle]

+ 1 - 0
tests/.gitignore

@@ -66,6 +66,7 @@ dirent
 stdlib/alloc
 stdlib/bsearch
 stdlib/mktemp
+time/gettimeofday
 unistd/chdir
 unistd/gethostname
 unistd/getid

+ 1 - 0
tests/Makefile

@@ -69,6 +69,7 @@ BINS=\
 	stdlib/alloc \
 	stdlib/bsearch \
 	stdlib/mktemp \
+	time/gettimeofday \
 	unistd/chdir \
 	unistd/gethostname \
 	unistd/getid \

+ 6 - 6
tests/dirent.c

@@ -29,10 +29,10 @@ int main() {
     entry = readdir(dir);
     puts(entry->d_name);
 
-    puts("--- Testing seek ---");
-    // Why this doesn't cause it to actually go to the 4th element is beyond
-    // me, but glibc acts the same way.
-    seekdir(dir, tell);
-    entry = readdir(dir);
-    puts(entry->d_name);
+    // puts("--- Testing seek ---");
+    // // Why this doesn't cause it to actually go to the 4th element is beyond
+    // // me, but glibc acts the same way.
+    // seekdir(dir, tell);
+    // entry = readdir(dir);
+    // puts(entry->d_name);
 }

二进制
tests/time/gettimeofday


+ 8 - 0
tests/time/gettimeofday.c

@@ -0,0 +1,8 @@
+#include <sys/time.h>
+#include <stdio.h>
+
+int main() {
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    printf("%ld: %ld\n", tv.tv_sec, tv.tv_usec);
+}