Is there a way to use functions from unistd.h in Rust code?

727 views Asked by At

I'm trying to implement a malloc type function, but I can't figure out what to use instead of the sbrk function found in unistd.h for C. Is there any way to FFI unistd.h into a Rust program?

1

There are 1 answers

6
Daniel Robertson On BEST ANSWER

The Rust Programming Language book as some good info on FFI. If you use libc, and cargo you could use something like the following.

extern crate libc;

use libc;

extern {
    fn sbrk(x: usize) -> *mut libc::c_void;
}

fn call_sbrk(x: usize) -> *mut libc::c_void {
    unsafe {
        sbrk(x)
    }
}

fn main() {
    let x = call_sbrk(42);
    println!("{:p}", x);
}

with something like the following in your Cargo.toml

[dependencies]
libc = "^0.2.7"