浏览代码

Implement sys/times.h on linux

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

+ 9 - 0
Cargo.lock

@@ -349,6 +349,7 @@ dependencies = [
  "sys_socket 0.1.0",
  "sys_stat 0.1.0",
  "sys_time 0.1.0",
+ "sys_times 0.1.0",
  "sys_un 0.1.0",
  "sys_utsname 0.1.0",
  "sys_wait 0.1.0",
@@ -579,6 +580,14 @@ dependencies = [
  "platform 0.1.0",
 ]
 
+[[package]]
+name = "sys_times"
+version = "0.1.0"
+dependencies = [
+ "cbindgen 0.5.2",
+ "platform 0.1.0",
+]
+
 [[package]]
 name = "sys_un"
 version = "0.1.0"

+ 1 - 0
Cargo.toml

@@ -41,6 +41,7 @@ sys_resource = { path = "src/sys_resource" }
 sys_socket = { path = "src/sys_socket" }
 sys_stat = { path = "src/sys_stat" }
 sys_time = { path = "src/sys_time" }
+sys_times = { path = "src/sys_times" }
 sys_un = { path = "src/sys_un" }
 sys_utsname = { path = "src/sys_utsname" }
 sys_wait = { path = "src/sys_wait" }

+ 1 - 0
src/lib.rs

@@ -31,6 +31,7 @@ pub extern crate sys_resource;
 pub extern crate sys_socket;
 pub extern crate sys_stat;
 pub extern crate sys_time;
+pub extern crate sys_times;
 pub extern crate sys_un;
 pub extern crate sys_utsname;
 pub extern crate sys_wait;

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

@@ -392,6 +392,10 @@ pub fn socketpair(domain: c_int, kind: c_int, protocol: c_int, socket_vector: *m
     e(unsafe { syscall!(SOCKETPAIR, domain, kind, protocol, socket_vector) }) as c_int
 }
 
+pub fn times(out: *mut tms) -> clock_t {
+    unsafe { syscall!(TIMES, out) as clock_t }
+}
+
 pub fn uname(utsname: *mut utsname) -> c_int {
     e(unsafe { syscall!(UNAME, utsname, 0) }) as c_int
 }

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

@@ -821,6 +821,15 @@ pub fn socketpair(domain: c_int, kind: c_int, protocol: c_int, socket_vector: *m
     -1
 }
 
+pub fn times(out: *mut tms) -> clock_t {
+    let _ = write!(
+        ::FileWriter(2),
+        "unimplemented: times({:p})",
+        out
+    );
+    !0
+}
+
 pub fn unlink(path: *const c_char) -> c_int {
     let path = unsafe { c_str(path) };
     e(syscall::unlink(path)) as c_int

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

@@ -223,3 +223,11 @@ pub struct rusage {
     pub ru_nvcsw: c_long,
     pub ru_nivcsw: c_long
 }
+
+#[repr(C)]
+pub struct tms {
+    tms_utime: clock_t,
+    tms_stime: clock_t,
+    tms_cutime: clock_t,
+    tms_cstime: clock_t
+}

+ 10 - 0
src/sys_times/Cargo.toml

@@ -0,0 +1,10 @@
+[package]
+name = "sys_times"
+version = "0.1.0"
+authors = ["jD91mZM2 <me@krake.one>"]
+
+[build-dependencies]
+cbindgen = { path = "../../cbindgen" }
+
+[dependencies]
+platform = { path = "../platform" }

+ 11 - 0
src/sys_times/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/times.h");
+}

+ 6 - 0
src/sys_times/cbindgen.toml

@@ -0,0 +1,6 @@
+include_guard = "_SYS_TIMES_H"
+language = "C"
+style = "Tag"
+
+[enum]
+prefix_with_name = true

+ 20 - 0
src/sys_times/src/lib.rs

@@ -0,0 +1,20 @@
+//! sys/times.h implementation
+
+#![no_std]
+
+extern crate platform;
+
+use platform::types::*;
+
+#[repr(C)]
+pub struct tms {
+    tms_utime: clock_t,
+    tms_stime: clock_t,
+    tms_cutime: clock_t,
+    tms_cstime: clock_t
+}
+
+#[no_mangle]
+pub extern "C" fn times(out: *mut tms) -> clock_t {
+    platform::times(out as *mut platform::types::tms)
+}

+ 1 - 0
tests/.gitignore

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

+ 1 - 0
tests/Makefile

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

+ 13 - 0
tests/time/times.c

@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <sys/times.h>
+#include <unistd.h>
+
+int main() {
+    struct tms tms;
+    printf("return: %ld\n", times(&tms));
+
+    printf("tm_utime: %ld\n", tms.tms_utime);
+    printf("tm_stime: %ld\n", tms.tms_stime);
+    printf("tm_cutime: %ld\n", tms.tms_cutime);
+    printf("tm_cstime: %ld\n", tms.tms_cstime);
+}