Skip to content
Snippets Groups Projects
Unverified Commit 6d117366 authored by Paul Sajna's avatar Paul Sajna Committed by GitHub
Browse files

Merge branch 'master' into stat

parents 35dbc8d3 bbf5c34c
No related branches found
No related tags found
1 merge request!110Partial implementation of sys/stat.h
Showing
with 1080 additions and 177 deletions
......@@ -25,19 +25,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "cbindgen"
version = "0.5.2"
dependencies = [
"clap 2.31.1 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"standalone-syn 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cc"
version = "1.0.7"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
......@@ -47,7 +47,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clap"
version = "2.31.1"
version = "2.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -62,7 +62,7 @@ dependencies = [
[[package]]
name = "compiler_builtins"
version = "0.1.0"
source = "git+https://github.com/rust-lang-nursery/compiler-builtins.git#8fe94f3553f92cd6ba21aadc852bd6a3e01194db"
source = "git+https://github.com/rust-lang-nursery/compiler-builtins.git#263a703b10351d8930e48045b4fd09768991b867"
[[package]]
name = "crt0"
......@@ -145,21 +145,13 @@ version = "0.1.0"
dependencies = [
"cbindgen 0.5.2",
"platform 0.1.0",
"socket 0.1.0",
]
[[package]]
name = "itoa"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
......@@ -199,7 +191,7 @@ dependencies = [
[[package]]
name = "num-traits"
version = "0.2.1"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
......@@ -293,11 +285,10 @@ dependencies = [
[[package]]
name = "remove_dir_all"
version = "0.3.0"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -324,7 +315,7 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.32"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -351,13 +342,13 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.11"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -467,11 +458,11 @@ dependencies = [
[[package]]
name = "tempdir"
version = "0.3.6"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"remove_dir_all 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"remove_dir_all 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -505,7 +496,7 @@ name = "toml"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -534,6 +525,8 @@ version = "0.1.0"
dependencies = [
"cbindgen 0.5.2",
"platform 0.1.0",
"stdio 0.1.0",
"string 0.1.0",
]
[[package]]
......@@ -547,7 +540,7 @@ dependencies = [
name = "va_list-helper"
version = "0.0.2"
dependencies = [
"cc 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -572,11 +565,6 @@ dependencies = [
"platform 0.1.0",
]
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.4"
......@@ -586,11 +574,6 @@ dependencies = [
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
......@@ -605,36 +588,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4"
"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf"
"checksum cc 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "87f38f122db5615319a985757e526c00161d924d19b71a0f3e80c52bab1adcf6"
"checksum cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "2b4911e4bdcb4100c7680e7e854ff38e23f1b34d4d9e079efae3da2801341ffc"
"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"
"checksum clap 2.31.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5dc18f6f4005132120d9711636b32c46a233fad94df6217fa1d81c5e97a9f200"
"checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536"
"checksum compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins.git)" = "<none>"
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum itoa 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "92a9df60778f789c37f76778ae8d0a2471c41baa8b059d98a5873c978f549587"
"checksum libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)" = "f54263ad99207254cf58b5f701ecb432c717445ea2ee8af387334bdd1a03fdff"
"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
"checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2"
"checksum num-traits 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3c2bd9b9d21e48e956b763c9f37134dc62d9e95da6edb3f672cacb6caf3cd3"
"checksum num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dee092fcdf725aee04dd7da1d21debff559237d49ef1cb3e69bcb8ece44c7364"
"checksum proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cd07deb3c6d1d9ff827999c7f9b04cdfd66b1b17ae508e14fe47b620f2282ae0"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5"
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum remove_dir_all 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b5d2f806b0fcdabd98acd380dc8daef485e22bcb7cddc811d1337967f2528cf5"
"checksum remove_dir_all 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dfc5b3ce5d5ea144bb04ebd093a9e14e9765bcfec866aecda9b6dec43b3d1e24"
"checksum sc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4ebbb026ba4a707c25caec2db5ef59ad8b41f7ad77cad06257e06229c891f376"
"checksum serde 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)" = "c73f63e08b33f6e59dfb3365b009897ebc3a3edc4af6e4f3ce8e483cf3d80ce7"
"checksum serde 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "4fe95aa0d46f04ce5c3a88bdcd4114ecd6144ed0b2725ebca2f1127744357807"
"checksum serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "652bc323d694dc925829725ec6c890156d8e70ae5202919869cb00fe2eff3788"
"checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab"
"checksum serde_json 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "fab6c4d75bedcf880711c85e39ebf8ccc70d0eba259899047ec5d7436643ee17"
"checksum serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "5c508584d9913df116b91505eec55610a2f5b16e9ed793c46e4d0152872b3e74"
"checksum standalone-quote 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dcedac1d6d98e7e9d1d6e628f5635af9566688ae5f6cea70a3976f495ae8d839"
"checksum standalone-syn 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "115808f5187c07c23cb93eee49d542fae54c6e8285d3a24c6ff683fcde9243db"
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum tempdir 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f73eebdb68c14bcb24aef74ea96079830e7fa7b31a6106e42ea7ee887c1e134e"
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum textwrap 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c0b59b6b4b44d867f1370ef1bd91bfb262bf07bf0ae65c202ea2fbc16153b693"
"checksum toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7540f4ffc193e0d3c94121edb19b055670d369f77d5804db11ae053a45b6e7e"
......@@ -643,8 +625,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
......@@ -6,3 +6,6 @@ pub const O_RDWR: c_int = 0x0002;
pub const O_CREAT: c_int = 0x0040;
pub const O_TRUNC: c_int = 0x0200;
pub const O_ACCMODE: c_int = O_RDONLY | O_WRONLY | O_RDWR;
pub const O_APPEND: c_int = 0o2000;
pub const O_CLOEXEC: c_int = 0o2_000_000;
pub const O_EXCL: c_int = 0o200;
......@@ -61,8 +61,8 @@ pub unsafe fn cstr_from_bytes_with_nul_unchecked(bytes: &[u8]) -> *const c_char
pub struct FileWriter(pub c_int);
impl FileWriter {
pub fn write(&mut self, buf: &[u8]) {
write(self.0, buf);
pub fn write(&mut self, buf: &[u8]) -> isize {
write(self.0, buf)
}
}
......@@ -73,6 +73,14 @@ impl fmt::Write for FileWriter {
}
}
pub struct FileReader(pub c_int);
impl FileReader {
pub fn read(&mut self, buf: &mut [u8]) -> isize {
read(self.0, buf)
}
}
pub struct StringWriter(pub *mut u8, pub usize);
impl StringWriter {
......
......@@ -130,10 +130,22 @@ pub fn getuid() -> uid_t {
e(unsafe { syscall!(GETUID) })
}
pub fn kill(pid: pid_t, sig: c_int) -> c_int {
e(unsafe { syscall!(KILL, pid, sig) }) as c_int
}
pub fn killpg(pgrp: pid_t, sig: c_int) -> c_int {
e(unsafe { syscall!(KILL, -(pgrp as isize) as pid_t, sig) }) as c_int
}
pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
e(unsafe { syscall!(LINKAT, AT_FDCWD, path1, AT_FDCWD, path2, 0) }) as c_int
}
pub fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
e(unsafe { syscall!(LSEEK, fildes, offset, whence) }) as off_t
}
pub fn lstat(file: *const c_char, buf: *mut stat) -> c_int {
e(unsafe { syscall!(NEWFSTATAT, AT_FDCWD, file, buf, AT_SYMLINK_NOFOLLOW) }) as c_int
}
......@@ -189,3 +201,7 @@ pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
e(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
}
pub fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
e(unsafe { syscall!(CLOCK_GETTIME, clk_id, tp) }) as c_int
}
......@@ -169,12 +169,28 @@ pub fn getuid() -> uid_t {
e(syscall::getuid()) as pid_t
}
pub fn kill(pid: pid_t, sig: c_int) -> c_int {
e(syscall::kill(pid, sig as usize)) as c_int
}
pub fn killpg(pgrp: pid_t, sig: c_int) -> c_int {
e(syscall::kill(-(pgrp as isize) as pid_t, sig as usize)) as c_int
}
pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
let path1 = unsafe { c_str(path1) };
let path2 = unsafe { c_str(path2) };
e(unsafe { syscall::link(path1.as_ptr(), path2.as_ptr()) }) as c_int
}
pub fn lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t {
e(syscall::lseek(
fd as usize,
offset as isize,
whence as usize,
)) as off_t
}
pub fn lstat(path: *const c_char, buf: *mut stat) -> c_int {
let path = unsafe { c_str(path) };
match syscall::open(path, O_RDONLY | O_NOFOLLOW) {
......@@ -185,7 +201,6 @@ pub fn lstat(path: *const c_char, buf: *mut stat) -> c_int {
res
}
}
}
pub fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
let flags = O_CREAT | O_EXCL | O_CLOEXEC | O_DIRECTORY | mode as usize & 0o777;
......@@ -286,3 +301,17 @@ pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
pub fn write(fd: c_int, buf: &[u8]) -> ssize_t {
e(syscall::write(fd as usize, buf)) as ssize_t
}
pub fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
let mut redox_tp = unsafe { redox_timespec::from(&*tp) };
match e(syscall::clock_gettime(clk_id as usize, &mut redox_tp)) as c_int {
-1 => -1,
_ => {
unsafe {
(*tp).tv_sec = redox_tp.tv_sec;
(*tp).tv_nsec = redox_tp.tv_nsec as i64;
};
0
}
}
}
......@@ -68,6 +68,7 @@ pub type clockid_t = i32;
pub type timer_t = c_void;
#[repr(C)]
#[derive(Default)]
pub struct timespec {
pub tv_sec: time_t,
pub tv_nsec: c_long,
......
......@@ -27,12 +27,12 @@ pub type sigset_t = sys_sigset_t;
#[no_mangle]
pub extern "C" fn kill(pid: pid_t, sig: c_int) -> c_int {
unimplemented!();
platform::kill(pid, sig)
}
#[no_mangle]
pub extern "C" fn killpg(pgrp: pid_t, sig: c_int) -> c_int {
unimplemented!();
platform::killpg(pgrp, sig)
}
#[no_mangle]
......
......@@ -8,6 +8,10 @@ build = "build.rs"
cbindgen = { path = "../../cbindgen" }
[dependencies]
compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] }
platform = { path = "../platform" }
va_list = { path = "../../va_list", features = ["no_std"] }
fcntl = { path = "../fcntl" }
string = { path = "../string" }
stdlib = { path = "../stdlib" }
errno = { path = "../errno"}
......@@ -5,3 +5,6 @@ language = "C"
[enum]
prefix_with_name = true
[export.rename]
"AtomicBool" = "volatile char"
use platform::types::*;
pub const BUFSIZ: size_t = 1024;
pub const UNGET: size_t = 8;
pub const FILENAME_MAX: c_int = 4096;
pub const F_PERM: c_int = 1;
pub const F_NORD: c_int = 4;
pub const F_NOWR: c_int = 8;
pub const F_EOF: c_int = 16;
pub const F_ERR: c_int = 32;
pub const F_SVB: c_int = 64;
pub const F_APP: c_int = 128;
pub const F_BADJ: c_int = 256;
pub const SEEK_SET: c_int = 0;
pub const SEEK_CUR: c_int = 1;
pub const SEEK_END: c_int = 2;
pub const _IOFBF: c_int = 0;
pub const _IOLBF: c_int = 1;
pub const _IONBF: c_int = 2;
#[allow(non_camel_case_types)]
pub type fpos_t = off_t;
use core::sync::atomic::AtomicBool;
use core::ptr;
use super::{constants, internal, BUFSIZ, FILE, UNGET};
#[allow(non_upper_case_globals)]
static mut default_stdin_buf: [u8; BUFSIZ as usize + UNGET] = [0; BUFSIZ as usize + UNGET];
#[allow(non_upper_case_globals)]
static mut default_stdin: FILE = FILE {
flags: constants::F_PERM | constants::F_NOWR | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: 0,
buf: unsafe { &mut default_stdin_buf as *mut [u8] as *mut u8 },
buf_size: BUFSIZ as usize,
buf_char: -1,
unget: UNGET,
lock: AtomicBool::new(false),
};
#[allow(non_upper_case_globals)]
static mut default_stdout_buf: [u8; BUFSIZ as usize] = [0; BUFSIZ as usize];
#[allow(non_upper_case_globals)]
static mut default_stdout: FILE = FILE {
flags: constants::F_PERM | constants::F_NORD | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: 1,
buf: unsafe { &mut default_stdout_buf } as *mut [u8] as *mut u8,
buf_size: BUFSIZ as usize,
buf_char: b'\n' as i8,
unget: 0,
lock: AtomicBool::new(false),
};
#[allow(non_upper_case_globals)]
static mut default_stderr_buf: [u8; BUFSIZ as usize] = [0; BUFSIZ as usize];
#[allow(non_upper_case_globals)]
static mut default_stderr: FILE = FILE {
flags: constants::F_PERM | constants::F_NORD | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: 2,
buf: unsafe { &mut default_stderr_buf } as *mut [u8] as *mut u8,
buf_size: BUFSIZ as usize,
buf_char: -1,
unget: 0,
lock: AtomicBool::new(false),
};
// Don't ask me how the casting below works, I have no idea
// " as *const FILE as *mut FILE" rust pls
//
// -- Tommoa
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut stdin: *mut FILE = unsafe { &default_stdin } as *const FILE as *mut FILE;
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut stdout: *mut FILE = unsafe { &default_stdout } as *const FILE as *mut FILE;
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut stderr: *mut FILE = unsafe { &default_stderr } as *const FILE as *mut FILE;
use super::{internal, BUFSIZ, FILE, UNGET};
use stdlib::calloc;
use core::{mem, ptr};
use core::sync::atomic::AtomicBool;
use platform::types::*;
use super::constants::*;
use fcntl::*;
use platform;
use errno;
/// Parse mode flags as a string and output a mode flags integer
pub unsafe fn parse_mode_flags(mode_str: *const c_char) -> i32 {
use string::strchr;
let mut flags = if !strchr(mode_str, b'+' as i32).is_null() {
O_RDWR
} else if (*mode_str) == b'r' as i8 {
O_RDONLY
} else {
O_WRONLY
};
if !strchr(mode_str, b'x' as i32).is_null() {
flags |= O_EXCL;
}
if !strchr(mode_str, b'e' as i32).is_null() {
flags |= O_CLOEXEC;
}
if (*mode_str) != b'r' as i8 {
flags |= O_CREAT;
}
if (*mode_str) == b'w' as i8 {
flags |= O_TRUNC;
}
if (*mode_str) != b'a' as i8 {
flags |= O_APPEND;
}
flags
}
/// Open a file with the file descriptor `fd` in the mode `mode`
pub unsafe fn _fdopen(fd: c_int, mode: *const c_char) -> *mut FILE {
use string::strchr;
if *mode != b'r' as i8 && *mode != b'w' as i8 && *mode != b'a' as i8 {
platform::errno = errno::EINVAL;
return ptr::null_mut();
}
let mut flags = 0;
if strchr(mode, b'+' as i32).is_null() {
flags |= if *mode == b'r' as i8 { F_NOWR } else { F_NORD };
}
if !strchr(mode, b'e' as i32).is_null() {
sys_fcntl(fd, F_SETFD, FD_CLOEXEC);
}
if *mode == 'a' as i8 {
let f = sys_fcntl(fd, F_GETFL, 0);
if (f & O_APPEND) == 0 {
sys_fcntl(fd, F_SETFL, f | O_APPEND);
}
flags |= F_APP;
}
let file = calloc(mem::size_of::<FILE>() + BUFSIZ + UNGET, 1) as *mut FILE;
// Allocate the file
(*file) = FILE {
flags: flags,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: fd,
buf: (file as *mut u8).add(mem::size_of::<FILE>() + UNGET),
buf_size: BUFSIZ,
buf_char: -1,
unget: UNGET,
lock: AtomicBool::new(false),
};
file
}
/// Write buffer `buf` of length `l` into `stream`
pub fn fwritex(buf: *const u8, l: size_t, stream: &mut FILE) -> size_t {
use core::ptr::copy_nonoverlapping;
use core::slice;
let buf: &'static [u8] = unsafe { slice::from_raw_parts(buf, l) };
let mut l = l;
let mut advance = 0;
if stream.wend.is_null() && !stream.can_write() {
// We can't write to this stream
return 0;
}
if l > stream.wend as usize - stream.wpos as usize {
// We can't fit all of buf in the buffer
return stream.write(buf);
}
let i = if stream.buf_char >= 0 {
let mut i = l;
while i > 0 && buf[i - 1] != b'\n' {
i -= 1;
}
if i > 0 {
let n = stream.write(buf);
if n < i {
return n;
}
advance += i;
l -= i;
}
i
} else {
0
};
unsafe {
// Copy and reposition
copy_nonoverlapping(&buf[advance..] as *const _ as *const u8, stream.wpos, l);
stream.wpos = stream.wpos.add(l);
}
l + i
}
/// Flush `stream` without locking it.
pub fn fflush_unlocked(stream: &mut FILE) -> c_int {
if stream.wpos > stream.wbase {
stream.write(&[]);
if stream.wpos.is_null() {
return -1;
}
}
if stream.rpos < stream.rend {
stream.seek(stream.rpos as i64 - stream.rend as i64, SEEK_CUR);
}
stream.wpos = ptr::null_mut();
stream.wend = ptr::null_mut();
stream.wbase = ptr::null_mut();
stream.rpos = ptr::null_mut();
stream.rend = ptr::null_mut();
0
}
use super::{constants, FILE};
use platform;
use platform::types::*;
use core::{mem, ptr, slice};
pub fn ftello(stream: &mut FILE) -> off_t {
let pos = stream.seek(
0,
if (stream.flags & constants::F_APP > 0) && stream.wpos > stream.wbase {
constants::SEEK_END
} else {
constants::SEEK_CUR
},
);
if pos < 0 {
return pos;
}
pos - (stream.rend as i64 - stream.rpos as i64) + (stream.wpos as i64 - stream.wbase as i64)
}
This diff is collapsed.
use core::{fmt, mem, slice, str};
use core::{fmt, slice, str};
use platform::types::*;
use vl::VaList;
......
......@@ -12,3 +12,4 @@ platform = { path = "../platform" }
ralloc = { path = "../../ralloc", default-features = false }
ctype = { path = "../ctype" }
errno = { path = "../errno" }
rand = { git = "https://github.com/rust-lang-nursery/rand/", default-features = false }
......@@ -8,8 +8,10 @@ extern crate ctype;
extern crate errno;
extern crate platform;
extern crate ralloc;
extern crate rand;
use core::{ptr, str};
use rand::{Rng, SeedableRng, XorShiftRng};
use errno::*;
use platform::types::*;
......@@ -19,8 +21,10 @@ static ALLOCATOR: ralloc::Allocator = ralloc::Allocator;
pub const EXIT_FAILURE: c_int = 1;
pub const EXIT_SUCCESS: c_int = 0;
pub const RAND_MAX: c_int = 2147483647;
static mut ATEXIT_FUNCS: [Option<extern "C" fn()>; 32] = [None; 32];
static mut RNG: Option<XorShiftRng> = None;
#[no_mangle]
pub unsafe extern "C" fn a64l(s: *const c_char) -> c_long {
......@@ -375,8 +379,16 @@ pub extern "C" fn qsort(
}
#[no_mangle]
pub extern "C" fn rand() -> c_int {
unimplemented!();
pub unsafe extern "C" fn rand() -> c_int {
match RNG {
Some(ref mut rng) => rng.gen_range::<c_int>(0, RAND_MAX),
None => {
let mut rng = XorShiftRng::from_seed([1; 16]);
let ret = rng.gen_range::<c_int>(0, RAND_MAX);
RNG = Some(rng);
ret
}
}
}
#[no_mangle]
......@@ -425,8 +437,8 @@ pub extern "C" fn setstate(state: *const c_char) -> *mut c_char {
}
#[no_mangle]
pub extern "C" fn srand(seed: c_uint) {
unimplemented!();
pub unsafe extern "C" fn srand(seed: c_uint) {
RNG = Some(XorShiftRng::from_seed([seed as u8; 16]));
}
#[no_mangle]
......
......@@ -4,3 +4,7 @@ language = "C"
[enum]
prefix_with_name = true
[defines]
"target_os = linux" = "__linux__"
"target_os = redox" = "__redox__"
......@@ -6,6 +6,33 @@ extern crate platform;
use platform::types::*;
#[cfg(target_os = "redox")]
pub const CLOCK_REALTIME: c_int = 1;
#[cfg(target_os = "redox")]
pub const CLOCK_MONOTONIC: c_int = 4;
#[cfg(target_os = "linux")]
pub const CLOCK_REALTIME: c_int = 0;
#[cfg(target_os = "linux")]
pub const CLOCK_MONOTONIC: c_int = 1;
#[cfg(target_os = "linux")]
pub const CLOCK_PROCESS_CPUTIME_ID: c_int = 2;
#[cfg(target_os = "linux")]
pub const CLOCK_THREAD_CPUTIME_ID: c_int = 3;
#[cfg(target_os = "linux")]
pub const CLOCK_MONOTONIC_RAW: c_int = 4;
#[cfg(target_os = "linux")]
pub const CLOCK_REALTIME_COARSE: c_int = 5;
#[cfg(target_os = "linux")]
pub const CLOCK_MONOTONIC_COARSE: c_int = 6;
#[cfg(target_os = "linux")]
pub const CLOCK_BOOTTIME: c_int = 7;
#[cfg(target_os = "linux")]
pub const CLOCK_REALTIME_ALARM: c_int = 8;
#[cfg(target_os = "linux")]
pub const CLOCK_BOOTTIME_ALARM: c_int = 9;
#[cfg(target_os = "linux")]
pub const CLOCK_TAI: c_int = 11;
/*
*#[repr(C)]
*pub struct timespec {
......@@ -59,7 +86,7 @@ pub extern "C" fn clock_getres(clock_id: clockid_t, res: *mut timespec) -> c_int
#[no_mangle]
pub extern "C" fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int {
unimplemented!();
platform::clock_gettime(clock_id, tp)
}
#[no_mangle]
......@@ -134,7 +161,14 @@ pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut t
#[no_mangle]
pub extern "C" fn time(tloc: *mut time_t) -> time_t {
unimplemented!();
let mut ts: timespec = Default::default();
platform::clock_gettime(CLOCK_REALTIME, &mut ts);
unsafe {
if !tloc.is_null() {
*tloc = ts.tv_sec
};
}
ts.tv_sec
}
#[no_mangle]
......
......@@ -9,3 +9,5 @@ cbindgen = { path = "../../cbindgen" }
[dependencies]
platform = { path = "../platform" }
stdio = { path = "../stdio" }
string = { path = "../string" }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment