-
-
Save stremin/89fb80a03808c8e1710d21512341adcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let buf = unsafe { mmap_stdin() }; | |
} | |
#[link(name = "c")] | |
extern { | |
fn mmap(addr: *mut u8, len: usize, prot: i32, flags: i32, fd: i32, offset: i64) -> *mut u8; | |
fn __errno_location() -> *const i32; | |
fn lseek(fd: i32, offset: i64, whence: i32) -> i64; | |
fn open(path: *const u8, oflag: i32) -> i32; | |
} | |
#[allow(dead_code)] | |
unsafe fn mmap_stdin<'a>() -> &'a [u8] { | |
mmap_fd(0) | |
} | |
#[allow(dead_code)] | |
unsafe fn mmap_path<'a>(path: &str) -> &'a [u8] { | |
let mut path2 = vec![]; | |
path2.extend_from_slice(path.as_bytes()); | |
path2.push(0); | |
let fd = open(path2.as_ptr(), 0); | |
if fd == -1 { | |
panic!("open failed, errno {}", *__errno_location()); | |
} | |
mmap_fd(fd) | |
} | |
unsafe fn mmap_fd<'a>(fd: i32) -> &'a [u8] { | |
let seek_end = 2; | |
let size = lseek(fd, 0, seek_end); | |
if size == -1 { | |
panic!("lseek failed, errno {}", *__errno_location()); | |
} | |
let prot_read = 0x01; | |
let map_private = 0x02; | |
let map_populate = 0x08000; | |
let ptr = mmap(0 as _, size as usize, prot_read, map_private | map_populate, fd, 0); | |
if ptr as isize == -1 { | |
panic!("mmap failed, errno {}", *__errno_location()); | |
} | |
std::slice::from_raw_parts(ptr, size as usize) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment