Last active
August 26, 2016 15:21
-
-
Save philippkeller/1f7cd1df9c48a5694c7e1e7c7952277d 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
#![allow(dead_code, | |
non_camel_case_types, | |
non_upper_case_globals, | |
non_snake_case)] | |
extern crate libc; | |
extern crate apue; | |
use apue::ToPtr; | |
pub type __int8_t = ::std::os::raw::c_char; | |
pub type __uint8_t = ::std::os::raw::c_uchar; | |
pub type __int16_t = ::std::os::raw::c_short; | |
pub type __uint16_t = ::std::os::raw::c_ushort; | |
pub type __int32_t = ::std::os::raw::c_int; | |
pub type __uint32_t = ::std::os::raw::c_uint; | |
pub type __int64_t = ::std::os::raw::c_longlong; | |
pub type __uint64_t = ::std::os::raw::c_ulonglong; | |
pub type __darwin_intptr_t = ::std::os::raw::c_long; | |
pub type __darwin_natural_t = ::std::os::raw::c_uint; | |
pub type __darwin_ct_rune_t = ::std::os::raw::c_int; | |
pub type MY_FILE = __sFILE; | |
pub type off_t = __darwin_off_t; | |
pub type ssize_t = isize; | |
extern "C" { | |
pub static mut __stdinp: *mut MY_FILE; | |
pub static mut __stdoutp: *mut MY_FILE; | |
pub static mut __stderrp: *mut MY_FILE; | |
pub static sys_nerr: ::std::os::raw::c_int; | |
pub static mut sys_errlist: [*const ::std::os::raw::c_char; 0usize]; | |
} | |
pub type __darwin_nl_item = ::std::os::raw::c_int; | |
pub type __darwin_wctrans_t = ::std::os::raw::c_int; | |
pub type __darwin_wctype_t = __uint32_t; | |
pub type size_t = usize; | |
pub type fpos_t = __darwin_off_t; | |
pub type __darwin_off_t = __int64_t; | |
#[repr(C)] | |
#[derive(Copy, Clone)] | |
#[derive(Debug)] | |
pub struct __sbuf { | |
pub _base: *mut ::std::os::raw::c_uchar, | |
pub _size: ::std::os::raw::c_int, | |
_bindgen_padding_0_: [u8; 4usize], | |
} | |
impl ::std::default::Default for __sbuf { | |
fn default() -> Self { unsafe { ::std::mem::zeroed() } } | |
} | |
pub enum __sFILEX { } | |
#[repr(C)] | |
#[derive(Copy, Clone)] | |
#[derive(Debug)] | |
pub struct __sFILE { | |
pub _p: *mut ::std::os::raw::c_uchar, | |
pub _r: ::std::os::raw::c_int, | |
pub _w: ::std::os::raw::c_int, | |
pub _flags: ::std::os::raw::c_short, | |
pub _file: ::std::os::raw::c_short, | |
pub _bf: __sbuf, | |
pub _lbfsize: ::std::os::raw::c_int, | |
pub _cookie: *mut ::std::os::raw::c_void, | |
pub _close: ::std::option::Option<unsafe extern "C" fn(arg1: | |
*mut ::std::os::raw::c_void) | |
-> ::std::os::raw::c_int>, | |
pub _read: ::std::option::Option<unsafe extern "C" fn(arg1: | |
*mut ::std::os::raw::c_void, | |
arg2: | |
*mut ::std::os::raw::c_char, | |
arg3: | |
::std::os::raw::c_int) | |
-> ::std::os::raw::c_int>, | |
pub _seek: ::std::option::Option<unsafe extern "C" fn(arg1: | |
*mut ::std::os::raw::c_void, | |
arg2: fpos_t, | |
arg3: | |
::std::os::raw::c_int) | |
-> fpos_t>, | |
pub _write: ::std::option::Option<unsafe extern "C" fn(arg1: | |
*mut ::std::os::raw::c_void, | |
arg2: | |
*const ::std::os::raw::c_char, | |
arg3: | |
::std::os::raw::c_int) | |
-> ::std::os::raw::c_int>, | |
pub _ub: __sbuf, | |
pub _extra: *mut __sFILEX, | |
pub _ur: ::std::os::raw::c_int, | |
pub _ubuf: [::std::os::raw::c_uchar; 3usize], | |
pub _nbuf: [::std::os::raw::c_uchar; 1usize], | |
pub _lb: __sbuf, | |
pub _blksize: ::std::os::raw::c_int, | |
pub _offset: fpos_t, | |
} | |
impl ::std::default::Default for __sFILE { | |
fn default() -> Self { unsafe { ::std::mem::zeroed() } } | |
} | |
unsafe fn a(fp: *mut libc::FILE) { | |
let fp = &mut *(fp as *mut MY_FILE); | |
let is_unbuffered = (fp._flags & libc::_IONBF as i16) != 0; | |
println!("{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}", | |
fp._r, fp._w, fp._flags, fp._file, fp._bf._size, fp._lbfsize, fp._ub._size, fp._ur, fp._lb._size, fp._blksize, fp._offset); | |
} | |
fn main() { | |
unsafe { | |
let fp1 = libc::fdopen(libc::STDIN_FILENO, &('r' as libc::c_char)); | |
let fp2 = libc::fdopen(libc::STDOUT_FILENO, &('w' as libc::c_char)); | |
let fp3 = libc::fdopen(libc::STDERR_FILENO, &('w' as libc::c_char)); | |
let passwd = libc::fopen("/etc/passwd".to_ptr(), &('r' as libc::c_char)); | |
a(fp1); | |
a(fp2); | |
a(fp3); | |
a(passwd); | |
} | |
} |
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
extern crate libc; | |
use std::ffi::CString; | |
pub trait ToPtr { | |
fn to_ptr(&self) -> *const i8; | |
} | |
impl ToPtr for str { | |
fn to_ptr(&self) -> *const i8 { | |
CString::new(self).unwrap().as_ptr() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment