Add all stdlib functions

This commit is contained in:
Jeremy Soller
2018-03-03 17:44:09 -07:00
parent f5ef0af883
commit 172517e4f8
4 changed files with 342 additions and 7 deletions
+335 -4
View File
@@ -1,6 +1,7 @@
//! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html
#![no_std]
#![feature(core_intrinsics)]
#![feature(global_allocator)]
extern crate platform;
@@ -16,6 +17,27 @@ pub const EXIT_SUCCESS: c_int = 0;
static mut ATEXIT_FUNCS: [usize; 32] = [0; 32];
#[no_mangle]
pub extern "C" fn a64l(s: *const c_char) -> c_long {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn abort() {
use core::intrinsics;
intrinsics::abort();
}
#[no_mangle]
pub extern "C" fn abs(i: c_int) -> c_int {
if i < 0 {
-i
} else {
i
}
}
#[no_mangle]
pub unsafe extern "C" fn atexit(func: extern "C" fn()) -> c_int {
for i in 0..ATEXIT_FUNCS.len() {
@@ -28,6 +50,67 @@ pub unsafe extern "C" fn atexit(func: extern "C" fn()) -> c_int {
1
}
#[no_mangle]
pub extern "C" fn atof(s: *const c_char) -> c_double {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn atoi(s: *const c_char) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn atol(s: *const c_char) -> c_long {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn bsearch(key: *const c_void, base: *const c_void, nel: size_t, width: size_t, compar: extern "C" fn(*const c_void, *const c_void) -> c_int) -> *mut c_void {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn calloc(nelem: size_t, elsize: size_t) -> *mut c_void {
use core::intrinsics;
let size = nelem * elsize;
let ptr = malloc(size);
if ! ptr.is_null() {
intrinsics::write_bytes(ptr as *mut u8, 0, size);
}
ptr
}
#[repr(C)]
pub struct div_t {
quot: c_int,
rem: c_int,
}
#[no_mangle]
pub extern "C" fn div(numer: c_int, denom: c_int) -> div_t {
div_t {
quot: numer / denom,
rem: numer % denom
}
}
#[no_mangle]
pub extern "C" fn drand48() -> c_double {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn ecvt(value: c_double, ndigit: c_int, decpt: *mut c_int, sign: *mut c_int) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn erand(xsubi: [c_ushort; 3]) -> c_double {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn exit(status: c_int) {
use core::mem;
@@ -42,18 +125,266 @@ pub unsafe extern "C" fn exit(status: c_int) {
platform::exit(status);
}
#[no_mangle]
pub extern "C" fn fcvt(value: c_double, ndigit: c_int, decpt: *mut c_int, sign: *mut c_int) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn free(ptr: *mut c_void) {
let ptr = (ptr as *mut u8).offset(-8);
let ptr = (ptr as *mut u8).offset(-16);
let size = *(ptr as *mut u64);
let _align = *(ptr as *mut u64).offset(1);
ralloc::free(ptr, size as usize);
}
#[no_mangle]
pub extern "C" fn gcvt(value: c_double, ndigit: c_int, buf: *mut c_char) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn getenv(name: *const c_char) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn getsubopt(optionp: *mut *mut c_char, tokens: *const *mut c_char, valuep: *mut *mut c_char) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn grantpt(fildes: c_int) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn initstate(seec: c_uint, state: *mut c_char, size: size_t) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn jrand48(xsubi: [c_ushort; 3]) -> c_long {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn l64a(value: c_long) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn labs(i: c_long) -> c_long {
if i < 0 {
-i
} else {
i
}
}
#[no_mangle]
pub extern "C" fn lcong48(param: [c_ushort; 7]) {
unimplemented!();
}
#[repr(C)]
pub struct ldiv_t {
quot: c_long,
rem: c_long,
}
#[no_mangle]
pub extern "C" fn ldiv(numer: c_long, denom: c_long) -> ldiv_t {
ldiv_t {
quot: numer / denom,
rem: numer % denom
}
}
#[no_mangle]
pub extern "C" fn lrand48() -> c_long {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void {
let ptr = ralloc::alloc(size + 8, 8);
*(ptr as *mut u64) = size as u64;
ptr.offset(8) as *mut c_void
let align = 8;
let ptr = ralloc::alloc(size + 16, align);
if ! ptr.is_null() {
*(ptr as *mut u64) = (size + 16) as u64;
*(ptr as *mut u64).offset(1) = align as u64;
ptr.offset(16) as *mut c_void
} else {
ptr as *mut c_void
}
}
#[no_mangle]
pub extern "C" fn mblen(s: *const c_char, n: size_t) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn mbstowcs(pwcs: *mut wchar_t, s: *const c_char, n: size_t) -> size_t {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn mbtowc(pwc: *mut wchar_t, s: *const c_char, n: size_t) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn mktemp(template: *mut c_char) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn mkstemp(template: *mut c_char) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn mrand48() -> c_long {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn nrand48(xsubi: [c_ushort; 3]) -> c_long {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn ptsname(fildes: c_int) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn putenv(s: *mut c_char) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn qsort(base: *mut c_void, nel: size_t, width: size_t, compar: extern "C" fn(*const c_void, *const c_void) -> c_int) {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn rand() -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn rand_r(seed: *mut c_uint) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn random() -> c_long {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
let old_ptr = (ptr as *mut u8).offset(-16);
let old_size = *(old_ptr as *mut u64);
let align = *(old_ptr as *mut u64).offset(1);
let ptr = ralloc::realloc(old_ptr, old_size as usize, size + 16, align as usize);
if ! ptr.is_null() {
*(ptr as *mut u64) = (size + 16) as u64;
*(ptr as *mut u64).offset(1) = align;
ptr.offset(16) as *mut c_void
} else {
ptr as *mut c_void
}
}
#[no_mangle]
pub extern "C" fn realpath(file_name: *const c_char, resolved_name: *mut c_char) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn seed48(seed16v: [c_ushort; 3]) -> c_ushort {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn setkey(key: *const c_char) {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn setstate(state: *const c_char) -> *mut c_char {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn srand(seed: c_uint) {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn srand48(seed: c_long) {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn srandom(seed: c_uint) {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn strtod(s: *const c_char, endptr: *mut *mut c_char) -> c_double {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn strtol(s: *const c_char, endptr: *mut *mut c_char, base: c_int) -> c_long {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn strtoul(s: *const c_char, endptr: *mut *mut c_char, base: c_int) -> c_ulong {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn system(command: *const c_char) -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn ttyslot() -> c_int {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn unlockpt(fildes: c_int) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn valloc(size: size_t) -> *mut c_void {
let align = 4096;
let ptr = ralloc::alloc(size + 16, align);
if ! ptr.is_null() {
*(ptr as *mut u64) = (size + 16) as u64;
*(ptr as *mut u64).offset(1) = align as u64;
ptr.offset(16) as *mut c_void
} else {
ptr as *mut c_void
}
}
#[no_mangle]
pub extern "C" fn wcstombs(s: *mut c_char, pwcs: *const wchar_t, n: size_t) -> size_t {
unimplemented!();
}
#[no_mangle]
pub extern "C" fn wctomb(s: *mut c_char, wchar: wchar_t) -> c_int {
unimplemented!();
}
/*
+4
View File
@@ -1,10 +1,14 @@
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
write(STDERR_FILENO, "malloc\n", 7);
char * ptr = (char *)malloc(256);
write(STDERR_FILENO, "set\n", 4);
int i;
for(i = 0; i < 256; i++) {
ptr[i] = (char)i;
}
write(STDERR_FILENO, "free\n", 5);
free(ptr);
}
+2 -2
View File
@@ -4,11 +4,11 @@
int main(int argc, char **argv) {
int fd = creat("create.out", 0755);
if (fd >= 0) {
write(fd, "Hello World!\n", 14);
write(fd, "Hello World!\n", 13);
close(fd);
return 0;
} else {
write(2, "creat failed\n", 14);
write(STDERR_FILENO, "creat failed\n", 13);
return 1;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
#include <unistd.h>
int main(int argc, char **argv) {
write(STDOUT_FILENO, "Hello World!\n", 14);
write(STDOUT_FILENO, "Hello World!\n", 13);
return 0;
}