Parcourir la source

Merge pull request #49 from sajattack/time

Add time and sys/time
Jeremy Soller il y a 7 ans
Parent
commit
3890ec58f0

+ 18 - 0
Cargo.lock

@@ -262,6 +262,8 @@ dependencies = [
  "stdio 0.1.0",
  "stdlib 0.1.0",
  "string 0.1.0",
+ "sys_time 0.1.0",
+ "time 0.1.0",
  "unistd 0.1.0",
  "wctype 0.1.0",
 ]
@@ -503,6 +505,14 @@ dependencies = [
  "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "sys_time"
+version = "0.1.0"
+dependencies = [
+ "cbindgen 0.5.0",
+ "platform 0.1.0",
+]
+
 [[package]]
 name = "tempdir"
 version = "0.3.6"
@@ -539,6 +549,14 @@ dependencies = [
  "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "time"
+version = "0.1.0"
+dependencies = [
+ "cbindgen 0.5.0",
+ "platform 0.1.0",
+]
+
 [[package]]
 name = "toml"
 version = "0.4.5"

+ 2 - 0
Cargo.toml

@@ -23,6 +23,8 @@ stat = { path = "src/stat" }
 stdio = { path = "src/stdio" }
 stdlib = { path = "src/stdlib" }
 string = { path = "src/string" }
+sys_time = { path = "src/sys_time" }
+time = { path = "src/time" }
 unistd = { path = "src/unistd" }
 wctype = { path = "src/wctype" }
 

+ 4 - 0
include/bits/timespec.h

@@ -0,0 +1,4 @@
+typedef struct {
+    time_t tv_sec;
+    long tv_nsec;
+} timespec;

+ 8 - 0
include/sys/types.h

@@ -24,4 +24,12 @@ typedef long time_t;
 
 typedef int useconds_t;
 
+typedef long suseconds_t;
+
+typedef long clock_t;
+
+typedef int clockid_t;
+
+typedef void* timer_t;
+
 #endif /* _SYS_TYPES_H */

+ 2 - 0
src/lib.rs

@@ -14,6 +14,8 @@ extern crate stat;
 extern crate stdio;
 extern crate stdlib;
 extern crate string;
+extern crate sys_time;
+extern crate time;
 extern crate unistd;
 extern crate wctype;
 

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

@@ -123,6 +123,10 @@ pub fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
     e(unsafe { syscall!(MKDIRAT, AT_FDCWD, path, mode) }) as c_int
 }
 
+pub fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
+    e(unsafe { syscall!(NANOSLEEP, rqtp, rmtp) }) as c_int
+}
+
 pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
     e(unsafe { syscall!(OPENAT, AT_FDCWD, path, oflag, mode) }) as c_int
 }

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

@@ -1,7 +1,9 @@
 use core::ptr;
 use core::slice;
+use core::mem;
 use syscall;
 use syscall::flag::*;
+use syscall::data::TimeSpec as redox_timespec;
 
 use c_str;
 use errno;
@@ -135,6 +137,32 @@ pub fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
     }
 }
 
+pub fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
+    unsafe {
+        let redox_rqtp = redox_timespec {
+            tv_sec: (*rqtp).tv_sec,
+            tv_nsec: (*rqtp).tv_nsec as i32,
+        };
+        let mut redox_rmtp: redox_timespec;
+        if rmtp.is_null() {
+            redox_rmtp = redox_timespec::default();
+        } else {
+            redox_rmtp = redox_timespec {
+                tv_sec: (*rmtp).tv_sec,
+                tv_nsec: (*rmtp).tv_nsec as i32,
+            };
+        }
+        match e(syscall::nanosleep(&redox_rqtp, &mut redox_rmtp)) as c_int {
+            -1 => -1,
+            _ => {
+                (*rmtp).tv_sec = redox_rmtp.tv_sec;
+                (*rmtp).tv_nsec = redox_rmtp.tv_nsec as i64;
+                0
+            }
+        }
+    }
+}
+
 pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
     let path = unsafe { c_str(path) };
     e(syscall::open(path, (oflag as usize) | (mode as usize))) as c_int

+ 11 - 1
src/platform/src/types.rs

@@ -46,7 +46,7 @@ pub type wchar_t = i16;
 pub type wint_t = i32;
 pub type wctype_t = i64;
 
-pub type off_t = c_long;
+pub type off_t = i64;
 pub type mode_t = u16;
 pub type time_t = i64;
 pub type pid_t = usize;
@@ -59,3 +59,13 @@ pub type blksize_t = isize;
 
 pub type useconds_t = i32;
 pub type suseconds_t = i64;
+
+pub type clock_t = i64;
+pub type clockid_t = i32;
+pub type timer_t = c_void;
+
+#[repr(C)]
+pub struct timespec {
+    pub tv_sec: time_t,
+    pub tv_nsec: c_long,
+}

+ 11 - 0
src/sys_time/Cargo.toml

@@ -0,0 +1,11 @@
+[package]
+name = "sys_time"
+version = "0.1.0"
+authors = ["Jeremy Soller <jackpot51@gmail.com>"]
+build = "build.rs"
+
+[build-dependencies]
+cbindgen = { path = "../../cbindgen" }
+
+[dependencies]
+platform = { path = "../platform" }

+ 11 - 0
src/sys_time/build.rs

@@ -0,0 +1,11 @@
+extern crate cbindgen;
+
+use std::{env, fs};
+
+fn main() {
+    let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
+    fs::create_dir_all("../../target/include").expect("failed to create include directory");
+    cbindgen::generate(crate_dir)
+        .expect("failed to generate bindings")
+        .write_to_file("../../target/include/sys/time.h");
+}

+ 6 - 0
src/sys_time/cbindgen.toml

@@ -0,0 +1,6 @@
+sys_includes = ["sys/types.h"]
+include_guard = "_SYS_TIME_H"
+language = "C"
+
+[enum]
+prefix_with_name = true

+ 61 - 0
src/sys_time/src/lib.rs

@@ -0,0 +1,61 @@
+//! sys/time implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/systime.h.html
+
+#![no_std]
+
+extern crate platform;
+
+use platform::types::*;
+
+#[repr(C)]
+pub struct timeval {
+    pub tv_sec: time_t,
+    pub tv_usec: suseconds_t,
+}
+
+#[repr(C)]
+pub struct itimerval {
+    pub it_interval: timeval,
+    pub it_value: timeval,
+}
+
+#[repr(C)]
+pub struct fd_set {
+    pub fds_bits: [c_long; 16usize],
+}
+
+pub extern "C" fn getitimer(which: c_int, value: *mut itimerval) -> c_int {
+    unimplemented!();
+}
+
+pub extern "C" fn setitimer(
+    which: c_int,
+    value: *const itimerval,
+    ovalue: *mut itimerval,
+) -> c_int {
+    unimplemented!();
+}
+
+pub extern "C" fn gettimeofday(tp: *mut timeval, tzp: *const c_void) -> c_int {
+    unimplemented!();
+}
+
+pub extern "C" fn select(
+    nfds: c_int,
+    readfds: *mut fd_set,
+    writefds: *mut fd_set,
+    errorfds: *mut fd_set,
+    timeout: *mut timeval,
+) -> c_int {
+    unimplemented!();
+}
+
+pub extern "C" fn utimes(path: *const c_char, times: [timeval; 2]) -> c_int {
+    unimplemented!();
+}
+
+/*
+#[no_mangle]
+pub extern "C" fn func(args) -> c_int {
+    unimplemented!();
+}
+*/

+ 11 - 0
src/time/Cargo.toml

@@ -0,0 +1,11 @@
+[package]
+name = "time"
+version = "0.1.0"
+authors = ["Jeremy Soller <jackpot51@gmail.com>"]
+build = "build.rs"
+
+[build-dependencies]
+cbindgen = { path = "../../cbindgen" }
+
+[dependencies]
+platform = { path = "../platform" }

+ 11 - 0
src/time/build.rs

@@ -0,0 +1,11 @@
+extern crate cbindgen;
+
+use std::{env, fs};
+
+fn main() {
+    let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
+    fs::create_dir_all("../../target/include").expect("failed to create include directory");
+    cbindgen::generate(crate_dir)
+        .expect("failed to generate bindings")
+        .write_to_file("../../target/include/time.h");
+}

+ 6 - 0
src/time/cbindgen.toml

@@ -0,0 +1,6 @@
+sys_includes = ["sys/types.h", "bits/timespec.h", "stdint.h"]
+include_guard = "_TIME_H"
+language = "C"
+
+[enum]
+prefix_with_name = true

+ 184 - 0
src/time/src/lib.rs

@@ -0,0 +1,184 @@
+//! time implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
+
+#![no_std]
+
+extern crate platform;
+
+use platform::types::*;
+
+/*
+ *#[repr(C)]
+ *pub struct timespec {
+ *    pub tv_sec: time_t,
+ *    pub tv_nsec: c_long,
+ *}
+ */
+
+#[repr(C)]
+pub struct tm {
+    pub tm_sec: c_int,
+    pub tm_min: c_int,
+    pub tm_hour: c_int,
+    pub tm_mday: c_int,
+    pub tm_mon: c_int,
+    pub tm_year: c_int,
+    pub tm_wday: c_int,
+    pub tm_yday: c_int,
+    pub tm_isdst: c_int,
+    pub tm_gmtoff: c_long,
+    pub tm_zone: *const c_char,
+}
+
+#[repr(C)]
+pub struct itimerspec {
+    pub it_interval: timespec,
+    pub it_value: timespec,
+}
+
+pub struct sigevent;
+
+#[no_mangle]
+pub extern "C" fn asctime(timeptr: *const tm) -> *mut c_char {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_char {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn clock() -> clock_t {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn clock_getres(clock_id: clockid_t, res: *mut timespec) -> c_int {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn clock_settime(clock_id: clockid_t, tp: *const timespec) -> c_int {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn ctime(clock: *const time_t) -> *mut c_char {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn ctime_r(clock: *const time_t, buf: *mut c_char) -> *mut c_char {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn difftime(time1: time_t, time0: time_t) -> f64 {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn getdate(string: *const c_char) -> tm {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn gmtime(timer: *const time_t) -> *mut tm {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn gmtime_r(clock: *const time_t, result: *mut tm) -> *mut tm {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn localtime(timer: *const time_t) -> *mut tm {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn localtime_r(clock: *const time_t, result: *mut tm) -> *mut tm {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn mktime(timeptr: *mut tm) -> time_t {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
+    platform::nanosleep(rqtp, rmtp)
+}
+
+#[no_mangle]
+pub extern "C" fn strftime(
+    s: *mut c_char,
+    maxsize: usize,
+    format: *const c_char,
+    timptr: *const tm,
+) -> usize {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut tm) -> *mut c_char {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn time(tloc: *mut time_t) -> time_t {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn timer_create(
+    clock_id: clockid_t,
+    evp: *mut sigevent,
+    timerid: *mut timer_t,
+) -> c_int {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn timer_delete(timerid: timer_t) -> c_int {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn tzset() {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn timer_settime(
+    timerid: timer_t,
+    flags: c_int,
+    value: *const itimerspec,
+    ovalue: *mut itimerspec,
+) -> c_int {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn timer_gettime(timerid: timer_t, value: *mut itimerspec) -> c_int {
+    unimplemented!();
+}
+
+#[no_mangle]
+pub extern "C" fn timer_getoverrun(timerid: timer_t) -> c_int {
+    unimplemented!();
+}
+
+/*
+#[no_mangle]
+pub extern "C" fn func(args) -> c_int {
+    unimplemented!();
+}
+*/

+ 14 - 2
src/unistd/src/lib.rs

@@ -5,6 +5,7 @@
 extern crate platform;
 
 pub use platform::types::*;
+use core::ptr;
 
 pub const R_OK: c_int = 1;
 pub const W_OK: c_int = 2;
@@ -381,7 +382,13 @@ pub extern "C" fn setuid(uid: uid_t) -> c_int {
 
 #[no_mangle]
 pub extern "C" fn sleep(seconds: c_uint) -> c_uint {
-    unimplemented!();
+    let rqtp = timespec {
+        tv_sec: seconds as i64,
+        tv_nsec: 0,
+    };
+    let rmtp = ptr::null_mut();
+    platform::nanosleep(&rqtp, rmtp);
+    0
 }
 
 #[no_mangle]
@@ -441,7 +448,12 @@ pub extern "C" fn unlink(path: *const c_char) -> c_int {
 
 #[no_mangle]
 pub extern "C" fn usleep(useconds: useconds_t) -> c_int {
-    unimplemented!();
+    let rqtp = timespec {
+        tv_sec: 0,
+        tv_nsec: (useconds * 1000).into(),
+    };
+    let rmtp = ptr::null_mut();
+    platform::nanosleep(&rqtp, rmtp)
 }
 
 #[no_mangle]

+ 3 - 1
tests/.gitignore

@@ -20,10 +20,12 @@
 /link
 /link.out
 /math
+/setid
+/sleep
 /pipe
 /printf
 /rmdir
 /setid
-/unlink
 /stdlib/strtol
+/unlink
 /write

+ 2 - 1
tests/Makefile

@@ -20,8 +20,9 @@ BINS=\
 	printf \
 	rmdir \
 	setid \
+	sleep \
+  	stdlib/strtol \
 	unlink \
-	stdlib/strtol \
 	write
 
 all: $(BINS)

+ 13 - 0
tests/sleep.c

@@ -0,0 +1,13 @@
+#include <time.h>
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char** argv) {
+    sleep(2);
+    perror("sleep");
+    usleep(1000);
+    perror("usleep");
+    timespec tm = {0, 10000};
+    nanosleep(&tm, NULL);
+    perror("nanosleep");
+}