From 858ad52cf6cac8115c2ecd5a8ee94d1dcf990cd3 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 08:04:16 -0700 Subject: [PATCH 1/9] Finish function definitions for unistd --- unistd/src/lib.rs | 200 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 195 insertions(+), 5 deletions(-) diff --git a/unistd/src/lib.rs b/unistd/src/lib.rs index 9e85fb8b2e..ce9050867d 100644 --- a/unistd/src/lib.rs +++ b/unistd/src/lib.rs @@ -4,6 +4,16 @@ extern crate libc; use libc::*; +pub const R_OK: c_int = 1; +pub const W_OK: c_int = 2; +pub const X_OK: c_int = 4; +pub const F_OK: c_int = 8; + +#[no_mangle] +pub extern "C" fn _exit(status: c_int) { + unimplemented!(); +} + #[no_mangle] pub extern "C" fn access(path: *const c_char, amode: c_int) -> c_int { unimplemented!(); @@ -104,11 +114,6 @@ pub extern "C" fn execvp(file: *const c_char, argv: *const *mut c_char) -> c_int unimplemented!(); } -#[no_mangle] -pub extern "C" fn _exit(status: c_int) { - unimplemented!(); -} - #[no_mangle] pub extern "C" fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int { unimplemented!(); @@ -244,6 +249,191 @@ pub extern "C" fn isatty(fildes: c_int) -> c_int { unimplemented!(); } +#[no_mangle] +pub extern "C" fn lchown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn link(path1: *const c_char, path2: *const c_char) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn lockf(fildes: c_int, function: c_int, size: off_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn nice(incr: c_int) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn pathconf(path: *const c_char, name: c_int) -> c_long { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn pause() -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn pipe(fildes: [c_int; 2]) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn pread(fildes: c_int, buf: *mut c_void, nbyte: size_t, offset: off_t) -> ssize_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn pthread_atfork(prepare: extern "C" fn(), parent: extern "C" fn(), child: extern "C" fn()) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn pwrite(fildes: c_int, buf: *const c_void, nbyte: size_t, offset: off_t) -> ssize_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn readlink(path: *const c_char, buf: *mut c_char, bufsize: size_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn rmdir(path: *const c_char) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn sbrk(incr: intptr_t) -> *mut c_void { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn setgid(gid: gid_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn setpgid(pid: pid_t, pgid: pid_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn setpgrp() -> pid_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn setregid(rgid: gid_t, egid: gid_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn setreuid(ruid: uid_t, euid: uid_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn setsid() -> pid_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn setuid(uid: uid_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn sleep(seconds: c_uint) -> c_uint { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn swab(src: *const c_void, dest: *mut c_void, nbytes: ssize_t) { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn symlink(path1: *const c_char, path2: *const c_char) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn sync() { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn sysconf(name: c_int) -> c_long { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn tcgetpgrp() -> pid_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn tcsetpgrp(fildes: c_int, pgid_id: pid_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn truncate(path: *const c_char, length: off_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn ttyname(fildes: c_int) -> *mut c_char { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn ualarm(useconds: useconds_t, interval: useconds_t) -> useconds_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn unlink(path: *const c_char) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn usleep(useconds: useconds_t) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn vfork() -> pid_t { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn write(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t { + unimplemented!(); +} + /* #[no_mangle] pub extern "C" fn func(args) -> c_int { From b720d0181f807fe84ecb77b0cd7c5dff23c3a7d3 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 08:25:53 -0700 Subject: [PATCH 2/9] Add fcntl --- fcntl/.gitignore | 2 ++ fcntl/Cargo.toml | 10 ++++++++ fcntl/build.rs | 11 ++++++++ fcntl/cbindgen.toml | 5 ++++ fcntl/src/lib.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++ unistd/Cargo.toml | 4 --- 6 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 fcntl/.gitignore create mode 100644 fcntl/Cargo.toml create mode 100644 fcntl/build.rs create mode 100644 fcntl/cbindgen.toml create mode 100644 fcntl/src/lib.rs diff --git a/fcntl/.gitignore b/fcntl/.gitignore new file mode 100644 index 0000000000..042776aad7 --- /dev/null +++ b/fcntl/.gitignore @@ -0,0 +1,2 @@ +/Cargo.lock +/target/ diff --git a/fcntl/Cargo.toml b/fcntl/Cargo.toml new file mode 100644 index 0000000000..fa3f01a67c --- /dev/null +++ b/fcntl/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "fcntl" +version = "0.1.0" +authors = ["Jeremy Soller "] + +[build-dependencies] +cbindgen = "0.5" + +[dependencies] +libc = "0.2" diff --git a/fcntl/build.rs b/fcntl/build.rs new file mode 100644 index 0000000000..34ef21a705 --- /dev/null +++ b/fcntl/build.rs @@ -0,0 +1,11 @@ +extern crate cbindgen; + +use std::env; + +fn main() { + let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + + cbindgen::generate(crate_dir) + .expect("Unable to generate bindings") + .write_to_file("target/fcntl.h"); +} diff --git a/fcntl/cbindgen.toml b/fcntl/cbindgen.toml new file mode 100644 index 0000000000..4193206804 --- /dev/null +++ b/fcntl/cbindgen.toml @@ -0,0 +1,5 @@ +include_guard = "_FCNTL_H" +language = "C" + +[enum] +prefix_with_name = true diff --git a/fcntl/src/lib.rs b/fcntl/src/lib.rs new file mode 100644 index 0000000000..95bea1c378 --- /dev/null +++ b/fcntl/src/lib.rs @@ -0,0 +1,61 @@ +/// fcntl implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.h.html + +extern crate libc; + +use libc::*; + +pub const F_DUPFD: c_int = 0; +pub const F_GETFD: c_int = 1; +pub const F_SETFD: c_int = 2; +pub const F_GETFL: c_int = 3; +pub const F_SETFL: c_int = 4; +pub const F_GETLK: c_int = 5; +pub const F_SETLK: c_int = 6; +pub const F_SETLKW: c_int = 7; + +pub const FD_CLOEXEC: c_int = 0x0100_0000; + +pub const F_RDLCK: c_int = 0; +pub const F_WRLCK: c_int = 1; +pub const F_UNLCK: c_int = 2; + +pub const O_RDONLY: c_int = 0x0001_0000; +pub const O_WRONLY: c_int = 0x0002_0000; +pub const O_RDWR: c_int = 0x0003_0000; +pub const O_NONBLOCK: c_int = 0x0004_0000; +pub const O_APPEND: c_int = 0x0008_0000; +pub const O_SHLOCK: c_int = 0x0010_0000; +pub const O_EXLOCK: c_int = 0x0020_0000; +pub const O_ASYNC: c_int = 0x0040_0000; +pub const O_FSYNC: c_int = 0x0080_0000; +pub const O_CLOEXEC: c_int = 0x0100_0000; +pub const O_CREAT: c_int = 0x0200_0000; +pub const O_TRUNC: c_int = 0x0400_0000; +pub const O_EXCL: c_int = 0x0800_0000; +pub const O_DIRECTORY: c_int = 0x1000_0000; +pub const O_STAT: c_int = 0x2000_0000; +pub const O_SYMLINK: c_int = 0x4000_0000; +pub const O_NOFOLLOW: c_int = 0x8000_0000; +pub const O_ACCMODE: c_int = O_RDONLY | O_WRONLY | O_RDWR; + +#[no_mangle] +pub extern "C" fn creat(path: *const c_char, mode: mode_t) -> c_int { + open(path, O_WRONLY | O_CREAT | O_TRUNC, mode) +} + +#[no_mangle] +pub extern "C" fn fcntl(fildes: c_int, cmd: c_int, arg: c_int) -> c_int { + unimplemented!(); +} + +#[no_mangle] +pub extern "C" fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int { + unimplemented!(); +} + +/* +#[no_mangle] +pub extern "C" fn func(args) -> c_int { + unimplemented!(); +} +*/ diff --git a/unistd/Cargo.toml b/unistd/Cargo.toml index 1d4f993953..e57a536766 100644 --- a/unistd/Cargo.toml +++ b/unistd/Cargo.toml @@ -3,10 +3,6 @@ name = "unistd" version = "0.1.0" authors = ["Jeremy Soller "] -[lib] -name = "unistd" -crate-type = ["cdylib"] - [build-dependencies] cbindgen = "0.5" From 7251cbec761d34ee33c85d4da2242d8a95e57d22 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 08:33:19 -0700 Subject: [PATCH 3/9] Add definitions for unistd --- unistd/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/unistd/src/lib.rs b/unistd/src/lib.rs index ce9050867d..77ed2553a9 100644 --- a/unistd/src/lib.rs +++ b/unistd/src/lib.rs @@ -4,11 +4,26 @@ extern crate libc; use libc::*; +pub const NULL: c_int = 0; + pub const R_OK: c_int = 1; pub const W_OK: c_int = 2; pub const X_OK: c_int = 4; pub const F_OK: c_int = 8; +pub const SEEK_SET: c_int = 0; +pub const SEEK_CUR: c_int = 1; +pub const SEEK_END: c_int = 2; + +pub const F_ULOCK: c_int = 0; +pub const F_LOCK: c_int = 1; +pub const F_TLOCK: c_int = 2; +pub const F_TEST: c_int = 3; + +pub const STDIN_FILENO: c_int = 0; +pub const STDOUT_FILENO: c_int = 1; +pub const STDERR_FILENO: c_int = 2; + #[no_mangle] pub extern "C" fn _exit(status: c_int) { unimplemented!(); From a01ff6baf81731149a8998deb63ac48e378adcdb Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 08:44:18 -0700 Subject: [PATCH 4/9] Add overarching staticlib --- fcntl/.gitignore => .gitignore | 0 Cargo.toml | 18 ++++++++++++++++++ fcntl/Cargo.toml | 1 + fcntl/build.rs | 2 +- src/lib.rs | 5 +++++ unistd/.gitignore | 2 -- unistd/Cargo.toml | 1 + unistd/build.rs | 2 +- 8 files changed, 27 insertions(+), 4 deletions(-) rename fcntl/.gitignore => .gitignore (100%) create mode 100644 Cargo.toml create mode 100644 src/lib.rs delete mode 100644 unistd/.gitignore diff --git a/fcntl/.gitignore b/.gitignore similarity index 100% rename from fcntl/.gitignore rename to .gitignore diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000000..7589f6fe81 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "relibc" +version = "0.1.0" +authors = ["Jeremy Soller "] + +[lib] +name = "c" +crate-type = ["staticlib"] + +[workspace] +members = [ + "fcntl", + "unistd" +] + +[dependencies] +fcntl = { path = "fcntl" } +unistd = { path = "unistd" } diff --git a/fcntl/Cargo.toml b/fcntl/Cargo.toml index fa3f01a67c..1403647b70 100644 --- a/fcntl/Cargo.toml +++ b/fcntl/Cargo.toml @@ -2,6 +2,7 @@ name = "fcntl" version = "0.1.0" authors = ["Jeremy Soller "] +build = "build.rs" [build-dependencies] cbindgen = "0.5" diff --git a/fcntl/build.rs b/fcntl/build.rs index 34ef21a705..fc96c5a7b1 100644 --- a/fcntl/build.rs +++ b/fcntl/build.rs @@ -7,5 +7,5 @@ fn main() { cbindgen::generate(crate_dir) .expect("Unable to generate bindings") - .write_to_file("target/fcntl.h"); + .write_to_file("../target/fcntl.h"); } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000000..866bc824b8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +extern crate fcntl; +extern crate unistd; + +pub use fcntl::*; +pub use unistd::*; diff --git a/unistd/.gitignore b/unistd/.gitignore deleted file mode 100644 index 042776aad7..0000000000 --- a/unistd/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/Cargo.lock -/target/ diff --git a/unistd/Cargo.toml b/unistd/Cargo.toml index e57a536766..08fece8fcc 100644 --- a/unistd/Cargo.toml +++ b/unistd/Cargo.toml @@ -2,6 +2,7 @@ name = "unistd" version = "0.1.0" authors = ["Jeremy Soller "] +build = "build.rs" [build-dependencies] cbindgen = "0.5" diff --git a/unistd/build.rs b/unistd/build.rs index 8ae3813ef8..6d467a9aa0 100644 --- a/unistd/build.rs +++ b/unistd/build.rs @@ -7,5 +7,5 @@ fn main() { cbindgen::generate(crate_dir) .expect("Unable to generate bindings") - .write_to_file("target/unistd.h"); + .write_to_file("../target/unistd.h"); } From d20e3f69e19901bc97889ee8686f61253f87449f Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 08:51:14 -0700 Subject: [PATCH 5/9] no_std --- fcntl/src/lib.rs | 4 +++- src/lib.rs | 2 ++ unistd/src/lib.rs | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/fcntl/src/lib.rs b/fcntl/src/lib.rs index 95bea1c378..71c5787159 100644 --- a/fcntl/src/lib.rs +++ b/fcntl/src/lib.rs @@ -1,4 +1,6 @@ -/// fcntl implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.h.html +//! fcntl implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.h.html + +#![no_std] extern crate libc; diff --git a/src/lib.rs b/src/lib.rs index 866bc824b8..4e4e661e2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + extern crate fcntl; extern crate unistd; diff --git a/unistd/src/lib.rs b/unistd/src/lib.rs index 77ed2553a9..d85c672379 100644 --- a/unistd/src/lib.rs +++ b/unistd/src/lib.rs @@ -1,4 +1,6 @@ -/// unistd implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html +//! unistd implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html + +#![no_std] extern crate libc; From d337caafe652b94194f9663908845127c2572407 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 09:19:25 -0700 Subject: [PATCH 6/9] Remove std from outputted libc.a --- Cargo.toml | 10 ++++++++++ fcntl/Cargo.toml | 2 +- fcntl/build.rs | 10 +++++----- unistd/Cargo.toml | 2 +- unistd/build.rs | 10 +++++----- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7589f6fe81..2cb2133a1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,13 @@ members = [ [dependencies] fcntl = { path = "fcntl" } unistd = { path = "unistd" } + +[profile.dev] +incremental = false +lto = true +panic = "abort" + +[profile.release] +incremental = false +lto = true +panic = "abort" diff --git a/fcntl/Cargo.toml b/fcntl/Cargo.toml index 1403647b70..c8e3ff555c 100644 --- a/fcntl/Cargo.toml +++ b/fcntl/Cargo.toml @@ -8,4 +8,4 @@ build = "build.rs" cbindgen = "0.5" [dependencies] -libc = "0.2" +libc = { version = "0.2", default-features = false } diff --git a/fcntl/build.rs b/fcntl/build.rs index fc96c5a7b1..b04c12cdaf 100644 --- a/fcntl/build.rs +++ b/fcntl/build.rs @@ -1,11 +1,11 @@ extern crate cbindgen; -use std::env; +use std::{env, fs}; fn main() { - let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - + 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("Unable to generate bindings") - .write_to_file("../target/fcntl.h"); + .expect("failed to generate bindings") + .write_to_file("../target/include/fcntl.h"); } diff --git a/unistd/Cargo.toml b/unistd/Cargo.toml index 08fece8fcc..9d3813b9de 100644 --- a/unistd/Cargo.toml +++ b/unistd/Cargo.toml @@ -8,4 +8,4 @@ build = "build.rs" cbindgen = "0.5" [dependencies] -libc = "0.2" +libc = { version = "0.2", default-features = false } diff --git a/unistd/build.rs b/unistd/build.rs index 6d467a9aa0..c12853664c 100644 --- a/unistd/build.rs +++ b/unistd/build.rs @@ -1,11 +1,11 @@ extern crate cbindgen; -use std::env; +use std::{env, fs}; fn main() { - let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - + 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("Unable to generate bindings") - .write_to_file("../target/unistd.h"); + .expect("failed to generate bindings") + .write_to_file("../target/include/unistd.h"); } From a9aae80ae03714417fd22ca8426c7434f228c2f3 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 10:08:16 -0700 Subject: [PATCH 7/9] WIP: Define common types --- common/Cargo.toml | 4 +++ common/src/lib.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++ fcntl/Cargo.toml | 2 +- fcntl/src/lib.rs | 4 +-- include/stdbool.h | 0 include/stdint.h | 7 ++++++ include/stdlib.h | 6 +++++ include/sys/types.h | 16 ++++++++++++ src/lib.rs | 7 ++++++ tests/.gitignore | 1 + tests/Makefile | 5 ++++ tests/write.c | 5 ++++ unistd/Cargo.toml | 2 +- unistd/src/lib.rs | 4 +-- 14 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 common/Cargo.toml create mode 100644 common/src/lib.rs create mode 100644 include/stdbool.h create mode 100644 include/stdint.h create mode 100644 include/stdlib.h create mode 100644 include/sys/types.h create mode 100644 tests/.gitignore create mode 100644 tests/Makefile create mode 100644 tests/write.c diff --git a/common/Cargo.toml b/common/Cargo.toml new file mode 100644 index 0000000000..3b6ffb5256 --- /dev/null +++ b/common/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "common" +version = "0.1.0" +authors = ["Jeremy Soller "] diff --git a/common/src/lib.rs b/common/src/lib.rs new file mode 100644 index 0000000000..ecb19b8e28 --- /dev/null +++ b/common/src/lib.rs @@ -0,0 +1,60 @@ +//! fcntl implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.h.html + +#![no_std] +#![allow(non_camel_case_types)] + +// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable +// more optimization opportunities around it recognizing things like +// malloc/free. +#[repr(u8)] +pub enum c_void { + // Two dummy variants so the #[repr] attribute can be used. + #[doc(hidden)] + __variant1, + #[doc(hidden)] + __variant2, +} + +pub type int8_t = i8; +pub type int16_t = i16; +pub type int32_t = i32; +pub type int64_t = i64; +pub type uint8_t = u8; +pub type uint16_t = u16; +pub type uint32_t = u32; +pub type uint64_t = u64; + +pub type c_schar = i8; +pub type c_uchar = u8; +pub type c_short = i16; +pub type c_ushort = u16; +pub type c_int = i32; +pub type c_uint = u32; +pub type c_float = f32; +pub type c_double = f64; +pub type c_longlong = i64; +pub type c_ulonglong = u64; +pub type intmax_t = i64; +pub type uintmax_t = u64; + +pub type size_t = usize; +pub type ptrdiff_t = isize; +pub type intptr_t = isize; +pub type uintptr_t = usize; +pub type ssize_t = isize; + +pub type c_char = i8; +pub type c_long = i64; +pub type c_ulong = u64; + +pub type wchar_t = i16; + +pub type off_t = c_long; +pub type mode_t = u16; +pub type time_t = i64; +pub type pid_t = usize; +pub type gid_t = usize; +pub type uid_t = usize; + +pub type useconds_t = i32; +pub type suseconds_t = i64; diff --git a/fcntl/Cargo.toml b/fcntl/Cargo.toml index c8e3ff555c..ca316750d4 100644 --- a/fcntl/Cargo.toml +++ b/fcntl/Cargo.toml @@ -8,4 +8,4 @@ build = "build.rs" cbindgen = "0.5" [dependencies] -libc = { version = "0.2", default-features = false } +common = { path = "../common" } diff --git a/fcntl/src/lib.rs b/fcntl/src/lib.rs index 71c5787159..b68a5929bb 100644 --- a/fcntl/src/lib.rs +++ b/fcntl/src/lib.rs @@ -2,9 +2,9 @@ #![no_std] -extern crate libc; +extern crate common; -use libc::*; +pub use common::*; pub const F_DUPFD: c_int = 0; pub const F_GETFD: c_int = 1; diff --git a/include/stdbool.h b/include/stdbool.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/include/stdint.h b/include/stdint.h new file mode 100644 index 0000000000..d7fa66c949 --- /dev/null +++ b/include/stdint.h @@ -0,0 +1,7 @@ +#ifndef _STDINT_H +#define _STDINT_H + +typedef int int32_t; +typedef int intptr_t; + +#endif /* _STDINT_H */ diff --git a/include/stdlib.h b/include/stdlib.h new file mode 100644 index 0000000000..d5f3b03cb3 --- /dev/null +++ b/include/stdlib.h @@ -0,0 +1,6 @@ +#ifndef _STDLIB_H +#define _STDLIB_H + +#include + +#endif /* _STDLIB_H */ diff --git a/include/sys/types.h b/include/sys/types.h new file mode 100644 index 0000000000..416f51149b --- /dev/null +++ b/include/sys/types.h @@ -0,0 +1,16 @@ +#ifndef _SYS_TYPES_H +#define _SYS_TYPES_H + +typedef int gid_t; +typedef int uid_t; + +typedef long off_t; + +typedef int pid_t; + +typedef unsigned long size_t; +typedef long ssize_t; + +typedef int useconds_t; + +#endif /* _SYS_TYPES_H */ diff --git a/src/lib.rs b/src/lib.rs index 4e4e661e2f..4c84deb549 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,14 @@ #![no_std] +#![feature(lang_items)] extern crate fcntl; extern crate unistd; pub use fcntl::*; pub use unistd::*; + +#[lang = "panic_fmt"] +#[no_mangle] +pub extern "C" fn rust_begin_unwind(fmt: ::core::fmt::Arguments, file: &str, line: u32) -> ! { + loop {} +} diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000000..7cd82ddb60 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +/write diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000000..84981286b1 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,5 @@ +write: write.c + gcc -nostdinc -nostdlib -I ../include -I ../target/include $< ../target/debug/libc.a -o $@ + +clean: + rm -f write diff --git a/tests/write.c b/tests/write.c new file mode 100644 index 0000000000..0349a5c094 --- /dev/null +++ b/tests/write.c @@ -0,0 +1,5 @@ +#include + +void _start(void) { + write(STDOUT_FILENO, "Hello World!\n", 14); +} diff --git a/unistd/Cargo.toml b/unistd/Cargo.toml index 9d3813b9de..9e2a470951 100644 --- a/unistd/Cargo.toml +++ b/unistd/Cargo.toml @@ -8,4 +8,4 @@ build = "build.rs" cbindgen = "0.5" [dependencies] -libc = { version = "0.2", default-features = false } +common = { path = "../common" } diff --git a/unistd/src/lib.rs b/unistd/src/lib.rs index d85c672379..642965125b 100644 --- a/unistd/src/lib.rs +++ b/unistd/src/lib.rs @@ -2,9 +2,9 @@ #![no_std] -extern crate libc; +extern crate common; -use libc::*; +pub use common::*; pub const NULL: c_int = 0; From d64dba1c1eccaba6b4f4d761e8841124640a9886 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 13:05:43 -0700 Subject: [PATCH 8/9] Use patched cbindgen, implement stdbool and stdint --- .gitmodules | 3 +++ Cargo.toml | 4 ---- cbindgen | 1 + fcntl/Cargo.toml | 2 +- include/stdbool.h | 9 +++++++++ include/stdint.h | 49 +++++++++++++++++++++++++++++++++++++++++++++-- include/stdlib.h | 6 ------ unistd/Cargo.toml | 2 +- 8 files changed, 62 insertions(+), 14 deletions(-) create mode 160000 cbindgen delete mode 100644 include/stdlib.h diff --git a/.gitmodules b/.gitmodules index ba57c697c8..271673cd4e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "openlibm"] path = openlibm url = https://github.com/redox-os/openlibm.git +[submodule "cbindgen"] + path = cbindgen + url = https://github.com/redox-os/cbindgen.git diff --git a/Cargo.toml b/Cargo.toml index 2cb2133a1c..7c83df5ab5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,6 @@ name = "c" crate-type = ["staticlib"] [workspace] -members = [ - "fcntl", - "unistd" -] [dependencies] fcntl = { path = "fcntl" } diff --git a/cbindgen b/cbindgen new file mode 160000 index 0000000000..97ceb5862f --- /dev/null +++ b/cbindgen @@ -0,0 +1 @@ +Subproject commit 97ceb5862f397b14c5b63cd60af7f5f345ad065c diff --git a/fcntl/Cargo.toml b/fcntl/Cargo.toml index ca316750d4..3527c6e779 100644 --- a/fcntl/Cargo.toml +++ b/fcntl/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Jeremy Soller "] build = "build.rs" [build-dependencies] -cbindgen = "0.5" +cbindgen = { path = "../cbindgen" } [dependencies] common = { path = "../common" } diff --git a/include/stdbool.h b/include/stdbool.h index e69de29bb2..1a9f870bb9 100644 --- a/include/stdbool.h +++ b/include/stdbool.h @@ -0,0 +1,9 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +typedef _Bool bool; +#define true 1 +#define false 0 +#define __bool_true_false_are_defined 1 + +#endif /* _STDBOOL_H */ diff --git a/include/stdint.h b/include/stdint.h index d7fa66c949..ade0ab4fed 100644 --- a/include/stdint.h +++ b/include/stdint.h @@ -1,7 +1,52 @@ #ifndef _STDINT_H #define _STDINT_H -typedef int int32_t; -typedef int intptr_t; +#define INT8_MIN -0x80 +#define INT8_MAX 0x7F +typedef signed char int8_t; + +#define UINT8_MIN 0x00 +#define UINT8_MAX 0xFF +typedef unsigned char uint8_t; + +#define INT16_MIN -0x8000 +#define INT16_MAX 0x7FFF +typedef signed short int16_t; + +#define UINT16_MIN 0x0000 +#define UINT16_MAX 0xFFFF +typedef unsigned short uint16_t; + +#define INT32_MIN -0x80000000 +#define INT32_MAX 0x7FFFFFFF +typedef signed long int32_t; + +#define UINT32_MIN 0x00000000 +#define UINT32_MAX 0xFFFFFFFF +typedef unsigned long uint32_t; + +#define INT64_MIN -0x8000000000000000 +#define INT64_MAX 0x7FFFFFFFFFFFFFFF +typedef signed long long int64_t; + +#define UINT64_MIN 0x0000000000000000 +#define UINT64_MAX 0xFFFFFFFFFFFFFFFF +typedef unsigned long long uint64_t; + +#define INTPTR_MIN INT64_MIN +#define INTPTR_MAX INT64_MAX +typedef int64_t intptr_t; + +#define UINTPTR_MIN UINT64_MIN +#define UINTPTR_MAX UINT64_MAX +typedef uint64_t uintptr_t; + +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +typedef int64_t intmax_t; + +#define UINTMAX_MIN UINT64_MIN +#define UINTMAX_MAX UINT64_MAX +typedef uint64_t uintmax_t; #endif /* _STDINT_H */ diff --git a/include/stdlib.h b/include/stdlib.h deleted file mode 100644 index d5f3b03cb3..0000000000 --- a/include/stdlib.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _STDLIB_H -#define _STDLIB_H - -#include - -#endif /* _STDLIB_H */ diff --git a/unistd/Cargo.toml b/unistd/Cargo.toml index 9e2a470951..0a7878fe71 100644 --- a/unistd/Cargo.toml +++ b/unistd/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Jeremy Soller "] build = "build.rs" [build-dependencies] -cbindgen = "0.5" +cbindgen = { path = "../cbindgen" } [dependencies] common = { path = "../common" } From 9fb0b77d89d1d3d81b64e3c950daf09cf8d81cdd Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 3 Mar 2018 13:10:21 -0700 Subject: [PATCH 9/9] Include sys/types in fctnl and unistd --- fcntl/cbindgen.toml | 1 + unistd/cbindgen.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/fcntl/cbindgen.toml b/fcntl/cbindgen.toml index 4193206804..948863cda4 100644 --- a/fcntl/cbindgen.toml +++ b/fcntl/cbindgen.toml @@ -1,3 +1,4 @@ +sys_includes = ["sys/types.h"] include_guard = "_FCNTL_H" language = "C" diff --git a/unistd/cbindgen.toml b/unistd/cbindgen.toml index 32fdb82257..259e4fadf2 100644 --- a/unistd/cbindgen.toml +++ b/unistd/cbindgen.toml @@ -1,3 +1,4 @@ +sys_includes = ["stdint.h", "sys/types.h"] include_guard = "_UNISTD_H" language = "C"