From a9aae80ae03714417fd22ca8426c7434f228c2f3 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Sat, 3 Mar 2018 10:08:16 -0700
Subject: [PATCH] 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 00000000..3b6ffb52
--- /dev/null
+++ b/common/Cargo.toml
@@ -0,0 +1,4 @@
+[package]
+name = "common"
+version = "0.1.0"
+authors = ["Jeremy Soller <jackpot51@gmail.com>"]
diff --git a/common/src/lib.rs b/common/src/lib.rs
new file mode 100644
index 00000000..ecb19b8e
--- /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 c8e3ff55..ca316750 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 71c57871..b68a5929 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 00000000..e69de29b
diff --git a/include/stdint.h b/include/stdint.h
new file mode 100644
index 00000000..d7fa66c9
--- /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 00000000..d5f3b03c
--- /dev/null
+++ b/include/stdlib.h
@@ -0,0 +1,6 @@
+#ifndef _STDLIB_H
+#define _STDLIB_H
+
+#include <sys/types.h>
+
+#endif /* _STDLIB_H */
diff --git a/include/sys/types.h b/include/sys/types.h
new file mode 100644
index 00000000..416f5114
--- /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 4e4e661e..4c84deb5 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 00000000..7cd82ddb
--- /dev/null
+++ b/tests/.gitignore
@@ -0,0 +1 @@
+/write
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 00000000..84981286
--- /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 00000000..0349a5c0
--- /dev/null
+++ b/tests/write.c
@@ -0,0 +1,5 @@
+#include <unistd.h>
+
+void _start(void) {
+    write(STDOUT_FILENO, "Hello World!\n", 14);
+}
diff --git a/unistd/Cargo.toml b/unistd/Cargo.toml
index 9d3813b9..9e2a4709 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 d85c6723..64296512 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;
 
-- 
GitLab