diff --git a/Cargo.lock b/Cargo.lock index f2bdefd4a5261e3fd2c2c34996d0a79ddba7ed6b..316b19b9aa5f5ecbee1d2fd42a847b1735f5a7c2 100644 --- a/Cargo.lock +++ b/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" diff --git a/Cargo.toml b/Cargo.toml index 6d7103c8f7ae425671273f2a989ad4ed8dd5addb..fd952f39026c59ca1fa7ca098a1400349bce7439 100644 --- a/Cargo.toml +++ b/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" } diff --git a/include/bits/timespec.h b/include/bits/timespec.h new file mode 100644 index 0000000000000000000000000000000000000000..52d696fc3129c3a14a9f2dce1c351e0c5aea567f --- /dev/null +++ b/include/bits/timespec.h @@ -0,0 +1,4 @@ +typedef struct { + time_t tv_sec; + long tv_nsec; +} timespec; diff --git a/include/sys/types.h b/include/sys/types.h index 0813cddfe4974a14426f1475a8693c66cb23498d..7312acfd4bb99cdca80da29228c06e194231d747 100644 --- a/include/sys/types.h +++ b/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 */ diff --git a/src/lib.rs b/src/lib.rs index cd56891d761912eec73aaab3d046adbcb96f9c1f..8becd7bd0de31a7f58d4982ed4435eee611fbc70 100644 --- a/src/lib.rs +++ b/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; diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index daff60cb5760fde81bd0776f39f94af6f3fd73ea..8915e6fea7ef6bdde20a694c506218870f271c23 100644 --- a/src/platform/src/linux/mod.rs +++ b/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 } diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 0ea4bda2073454a74f9968f0c351413b5471f741..e663a1b2da29c342d484d27e227c1ba0efd0e28d 100644 --- a/src/platform/src/redox/mod.rs +++ b/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 diff --git a/src/platform/src/types.rs b/src/platform/src/types.rs index 786af774d50c671dd6e66d39fed20e7c35181512..45ae64499d508ff2d7c5307a623ca10624751a99 100644 --- a/src/platform/src/types.rs +++ b/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, +} diff --git a/src/sys_time/Cargo.toml b/src/sys_time/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..20b3ac9c6f4e5bfa7d2940dd18b05c893388e062 --- /dev/null +++ b/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" } diff --git a/src/sys_time/build.rs b/src/sys_time/build.rs new file mode 100644 index 0000000000000000000000000000000000000000..9d087ef9c1e3c50e9f1052194a6be433f1d59634 --- /dev/null +++ b/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"); +} diff --git a/src/sys_time/cbindgen.toml b/src/sys_time/cbindgen.toml new file mode 100644 index 0000000000000000000000000000000000000000..bc1c1315262bf03875f9e5d9fe213c1f9dbb8dba --- /dev/null +++ b/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 diff --git a/src/sys_time/src/lib.rs b/src/sys_time/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..d56c5755a2823155225202165eef4fab4856027a --- /dev/null +++ b/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!(); +} +*/ diff --git a/src/time/Cargo.toml b/src/time/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..fe104d90be5710270d1ec4316b72fbbe3b892094 --- /dev/null +++ b/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" } diff --git a/src/time/build.rs b/src/time/build.rs new file mode 100644 index 0000000000000000000000000000000000000000..7bc136af55e270baf7bd3da72f96c02bfc734916 --- /dev/null +++ b/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"); +} diff --git a/src/time/cbindgen.toml b/src/time/cbindgen.toml new file mode 100644 index 0000000000000000000000000000000000000000..ef3a5240e6d6dd30ae2b1b7421ecc5f4783a7f8f --- /dev/null +++ b/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 diff --git a/src/time/src/lib.rs b/src/time/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..5b90e86f7482758341965c01524964cb4af57da7 --- /dev/null +++ b/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!(); +} +*/ diff --git a/src/unistd/src/lib.rs b/src/unistd/src/lib.rs index 8a1d149f887b8aad5ab05f60d54d8c3607a60f31..7915a7e1421b8b790bf5e8b9b8d3a53546e0ef9d 100644 --- a/src/unistd/src/lib.rs +++ b/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] diff --git a/tests/.gitignore b/tests/.gitignore index 42fe4750c666a6810fd66def2384df6045e3cc7c..620d8c72440cdb7d870a79e4ce161596092d3106 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -20,10 +20,12 @@ /link /link.out /math +/setid +/sleep /pipe /printf /rmdir /setid -/unlink /stdlib/strtol +/unlink /write diff --git a/tests/Makefile b/tests/Makefile index 24f1ba3a0443600b3e3d3433ca2f644ca1e9e223..ae73d603b19f36217f1cd5eeb74f99d01cfbf736 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -20,8 +20,9 @@ BINS=\ printf \ rmdir \ setid \ + sleep \ + stdlib/strtol \ unlink \ - stdlib/strtol \ write all: $(BINS) diff --git a/tests/sleep.c b/tests/sleep.c new file mode 100644 index 0000000000000000000000000000000000000000..ab66a4e75e540ce88a0fcb6ae49b33d7ccb8af79 --- /dev/null +++ b/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"); +}