Move crt0 and platform into src folder
Add cargo fmt script
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "platform"
|
||||
version = "0.1.0"
|
||||
authors = ["Jeremy Soller <jackpot51@gmail.com>"]
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
sc = "0.2"
|
||||
|
||||
[target.'cfg(target_os = "redox")'.dependencies]
|
||||
redox_syscall = "0.1"
|
||||
@@ -0,0 +1,54 @@
|
||||
//! fcntl implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.h.html
|
||||
|
||||
#![no_std]
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
#[cfg(all(not(feature = "no_std"), target_os = "linux"))]
|
||||
#[macro_use]
|
||||
extern crate sc;
|
||||
|
||||
pub use sys::*;
|
||||
|
||||
#[cfg(all(not(feature = "no_std"), target_os = "linux"))]
|
||||
#[path = "linux/mod.rs"]
|
||||
mod sys;
|
||||
|
||||
#[cfg(all(not(feature = "no_std"), target_os = "redox"))]
|
||||
#[path = "redox/mod.rs"]
|
||||
mod sys;
|
||||
|
||||
pub mod types;
|
||||
|
||||
use core::fmt;
|
||||
|
||||
use types::*;
|
||||
|
||||
pub unsafe fn c_str(s: *const c_char) -> &'static [u8] {
|
||||
use core::slice;
|
||||
|
||||
let mut size = 0;
|
||||
|
||||
loop {
|
||||
if *s.offset(size) == 0 {
|
||||
break;
|
||||
}
|
||||
size += 1;
|
||||
}
|
||||
|
||||
slice::from_raw_parts(s as *const u8, size as usize)
|
||||
}
|
||||
|
||||
pub struct FileWriter(pub c_int);
|
||||
|
||||
impl FileWriter {
|
||||
pub fn write(&mut self, buf: &[u8]) {
|
||||
write(self.0, buf);
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Write for FileWriter {
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
self.write(s.as_bytes());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
use types::*;
|
||||
|
||||
const AT_FDCWD: c_int = -100;
|
||||
|
||||
pub fn brk(addr: *const c_void) -> c_int {
|
||||
unsafe {
|
||||
let newbrk = syscall!(BRK, addr);
|
||||
if newbrk < addr as usize {
|
||||
-1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn chdir(path: *const c_char) -> c_int {
|
||||
unsafe { syscall!(CHDIR, path) as c_int }
|
||||
}
|
||||
|
||||
pub fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
|
||||
unsafe { syscall!(CHOWN, owner as u32, group as u32) as c_int }
|
||||
}
|
||||
|
||||
pub fn close(fildes: c_int) -> c_int {
|
||||
unsafe { syscall!(CLOSE, fildes) as c_int }
|
||||
}
|
||||
|
||||
pub fn dup(fildes: c_int) -> c_int {
|
||||
unsafe { syscall!(DUP, fildes) as c_int }
|
||||
}
|
||||
|
||||
pub fn dup2(fildes: c_int, fildes2: c_int) -> c_int {
|
||||
unsafe { syscall!(DUP2, fildes, fildes2) as c_int }
|
||||
}
|
||||
|
||||
pub fn exit(status: c_int) -> ! {
|
||||
unsafe {
|
||||
syscall!(EXIT, status);
|
||||
}
|
||||
loop {}
|
||||
}
|
||||
|
||||
pub fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
|
||||
unsafe { syscall!(FCHOWN, fildes, owner, group) as c_int }
|
||||
}
|
||||
|
||||
pub fn fchdir(fildes: c_int) -> c_int {
|
||||
unsafe { syscall!(FCHDIR, fildes) as c_int }
|
||||
}
|
||||
|
||||
pub fn fsync(fildes: c_int) -> c_int {
|
||||
unsafe { syscall!(FSYNC, fildes) as c_int }
|
||||
}
|
||||
|
||||
pub fn ftruncate(fildes: c_int, length: off_t) -> c_int {
|
||||
unsafe { syscall!(FTRUNCATE, fildes, length) as c_int }
|
||||
}
|
||||
|
||||
pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
|
||||
unsafe {
|
||||
syscall!(GETCWD, buf, size);
|
||||
buf as *mut c_char
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getegid() -> gid_t {
|
||||
unsafe { syscall!(GETEGID) }
|
||||
}
|
||||
|
||||
pub fn geteuid() -> uid_t {
|
||||
unsafe { syscall!(GETEUID) }
|
||||
}
|
||||
|
||||
pub fn getgid() -> gid_t {
|
||||
unsafe { syscall!(GETGID) }
|
||||
}
|
||||
|
||||
pub fn getpgid(pid: pid_t) -> pid_t {
|
||||
unsafe { syscall!(GETPGID, pid) }
|
||||
}
|
||||
|
||||
pub fn getpid() -> pid_t {
|
||||
unsafe { syscall!(GETPID) }
|
||||
}
|
||||
|
||||
pub fn getppid() -> pid_t {
|
||||
unsafe { syscall!(GETPPID) }
|
||||
}
|
||||
|
||||
pub fn getuid() -> uid_t {
|
||||
unsafe { syscall!(GETUID) }
|
||||
}
|
||||
|
||||
pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
|
||||
unsafe { syscall!(LINK, path1, path2) as c_int }
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
|
||||
unsafe { syscall!(OPEN, path, oflag, mode) as c_int }
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
|
||||
unsafe { syscall!(OPENAT, AT_FDCWD, path, oflag, mode) as c_int }
|
||||
}
|
||||
|
||||
pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
|
||||
unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) as ssize_t }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use core::ops::Deref;
|
||||
|
||||
pub struct RawFile(usize);
|
||||
|
||||
impl RawFile {
|
||||
pub fn open<T: AsRef<[u8]>>(path: T, flags: usize) -> Result<RawFile> {
|
||||
open(path, flags).map(RawFile)
|
||||
}
|
||||
|
||||
pub fn dup(&self, buf: &[u8]) -> Result<RawFile> {
|
||||
dup(self.0, buf).map(RawFile)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RawFile {
|
||||
fn drop(&mut self) {
|
||||
let _ = close(self.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for RawFile {
|
||||
type Target = usize;
|
||||
|
||||
fn deref(&self) -> &usize {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
use core::slice;
|
||||
use syscall;
|
||||
use c_str;
|
||||
use types::*;
|
||||
|
||||
pub fn brk(addr: *const c_void) -> c_int {
|
||||
syscall::brk(addr as usize).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn chdir(path: *const c_char) -> c_int {
|
||||
let path = unsafe { c_str(path) };
|
||||
syscall::chdir(path).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
|
||||
let fd = syscall::open(cstr_to_slice(path));
|
||||
syscall::fchown(fd as usize, owner as usize, group as usize).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn close(fd: c_int) -> c_int {
|
||||
syscall::close(fd as usize);
|
||||
0
|
||||
}
|
||||
|
||||
pub fn dup(fd: c_int) -> c_int {
|
||||
syscall::dup(fd as usize, &[]).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn dup2(fd1: c_int, fd2: c_int) -> c_int {
|
||||
syscall::dup2(fd1 as usize, fd2 as usize, &[]).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn exit(status: c_int) -> ! {
|
||||
syscall::exit(status as usize);
|
||||
loop {}
|
||||
}
|
||||
|
||||
pub fn fchown(fd: c_int, owner: uid_t, group: gid_t) -> c_int {
|
||||
syscall::fchown(owner as usize, group as usize).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn fchdir(fd: c_int) -> c_int {
|
||||
let result = fpath(fd as usize, &[]);
|
||||
if result.is_ok() {
|
||||
syscall::chdir(path).unwrap_or(-1) as c_int
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fsync(fd: c_int) -> c_int {
|
||||
syscall::fsync(fd as usize).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn ftruncate(fd: c_int, len: off_t) -> c_int {
|
||||
syscall::ftruncate(fd as usize, len as usize).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn getcwd(buf: *mut c_char, size: size_t) -> c_int {
|
||||
// XXX: do something with size maybe
|
||||
let rbuf = unsafe { c_str(buf) };
|
||||
syscall::getcwd(rbuf);
|
||||
unsafe { &*(rbuf as *mut [c_char]) }
|
||||
}
|
||||
|
||||
pub fn getegid() -> gid_t {
|
||||
syscall::getegid().unwrap_or(-1) as gid_t
|
||||
}
|
||||
|
||||
pub fn geteuid() -> uid_t {
|
||||
syscall::geteuid().unwrap_or(-1) as uid_t
|
||||
}
|
||||
|
||||
pub fn getgid() -> gid_t {
|
||||
syscall::getgid().unwrap_or(-1) as gid_t
|
||||
}
|
||||
|
||||
pub fn getpgid(pid: pid_t) -> pid_t {
|
||||
syscall::getpgid(pid as usize).unwrap_or(-1) as pid_t
|
||||
}
|
||||
|
||||
pub fn getpid() -> pid_t {
|
||||
syscall::getpid().unwrap_or(-1) as pid_t
|
||||
}
|
||||
|
||||
pub fn getppid() -> pid_t {
|
||||
syscall::getppid().unwrap_or(-1) as pid_t
|
||||
}
|
||||
|
||||
pub fn getuid() -> uid_t {
|
||||
syscall::getuid().unwrap_or(-1) as pid_t
|
||||
}
|
||||
|
||||
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) };
|
||||
syscall::link(path1, path2).unwrap_or(-1) as c_int
|
||||
}
|
||||
|
||||
pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
|
||||
let path = unsafe { c_str(path) };
|
||||
syscall::open(path, (oflag as usize) | (mode as usize)).unwrap() as c_int
|
||||
}
|
||||
|
||||
pub fn write(fd: c_int, buf: &[u8]) -> ssize_t {
|
||||
syscall::write(fd as usize, buf);
|
||||
buf.len() as ssize_t
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// 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 wint_t = i32;
|
||||
|
||||
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;
|
||||
Reference in New Issue
Block a user