1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#![crate_name = "posix"]
#![crate_type = "lib"]
#![feature(core, path, std_misc)]
#![allow(non_camel_case_types)]
#![allow(raw_pointer_derive)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

use std::path::{Path};
use std::ffi::{AsOsStr};
use std::os::unix::{OsStrExt};

pub use os::arch::{char_t, schar_t, uchar_t, short_t, ushort_t, int_t, uint_t, long_t};
pub use os::arch::{ulong_t, longlong_t, ulonglong_t, float_t, double_t, size_t, ssize_t}; 

#[macro_use]
mod macros {
    macro_rules! new {
        ($name:ident) => {
            impl $name {
                pub fn new() -> $name {
                    unsafe { ::std::mem::zeroed() }
                }
            }
        }
    }
}

pub mod aio;
pub mod arpa;
pub mod cpio;
pub mod ctype;
pub mod dirent;
pub mod dlfcn;
pub mod errno;
pub mod fcntl;
pub mod fenv;
pub mod float;
pub mod fmtmsg;
pub mod fnmatch;
pub mod ftw;
pub mod glob;
pub mod grp;
pub mod iconv;
pub mod inttypes;
pub mod langinfo;
pub mod libgen;
pub mod limits;
pub mod locale;
pub mod monetary;
pub mod mqueue;
pub mod ndbm;
pub mod net;
pub mod netdb;
pub mod netinet;
pub mod nl_types;
pub mod poll;
pub mod pthread;
pub mod pwd;
pub mod regex;
pub mod sched;
pub mod search;
pub mod semaphore;
pub mod setjmp;
pub mod spawn;
pub mod stddef;
pub mod stdint;
pub mod stdio;
pub mod stdlib;
pub mod string;
pub mod strings;
pub mod stropts;
pub mod signal;
pub mod sys;
pub mod syslog;
pub mod tar;
pub mod termios;
pub mod time;
pub mod ulimit;
pub mod unistd;
pub mod utime;
pub mod utmpx;
pub mod wchar;
pub mod wctype;
pub mod wordexp;

#[repr(u8)]
#[derive(Copy)]
pub enum void_t {
    __variant1,
    __variant2,
}

#[cfg(target_os = "linux")]
mod os {
    #[cfg(target_arch = "x86_64")]
    pub mod arch {
        pub type bool_t       = i8;
        pub type char_t       = i8;
        pub type schar_t      = i8;
        pub type uchar_t      = u8;
        pub type short_t      = i16;
        pub type ushort_t     = u16;
        pub type int_t        = i32;
        pub type uint_t       = u32;
        pub type long_t       = i64;
        pub type ulong_t      = u64;
        pub type longlong_t   = i64;
        pub type ulonglong_t  = u64;
        pub type float_t      = f32;
        pub type double_t     = f64;
        pub type size_t       = u64;
        pub type ssize_t      = i64;
    }

    #[cfg(target_arch = "x86")]
    pub mod arch {
        pub type bool_t       = i8;
        pub type char_t       = i8;
        pub type schar_t      = i8;
        pub type uchar_t      = u8;
        pub type short_t      = i16;
        pub type ushort_t     = u16;
        pub type int_t        = i32;
        pub type uint_t       = u32;
        pub type long_t       = i32;
        pub type ulong_t      = u32;
        pub type longlong_t   = i64;
        pub type ulonglong_t  = u64;
        pub type float_t      = f32;
        pub type double_t     = f64;
        pub type size_t       = u32;
        pub type ssize_t      = i32;
    }
}

pub trait NTStr {
    fn as_ptr(&self) -> *const char_t;
}

pub struct NTStrBorrowed<'a> {
    data: &'a [u8],
}

pub struct NTStrOwned {
    vec: Vec<u8>,
}

impl<'a> NTStr for NTStrBorrowed<'a> {
    fn as_ptr(&self) -> *const char_t {
        self.data.as_ptr() as *const _
    }
}

impl NTStr for NTStrOwned {
    fn as_ptr(&self) -> *const char_t {
        self.vec.as_ptr() as *const _
    }
}

pub trait ToNTStr {
    fn to_nt_str(&self) -> NTStrOwned;
}

impl<'a> ToNTStr for &'a [u8] {
    fn to_nt_str(&self) -> NTStrOwned {
        let mut vec = self.to_vec();
        vec.push(0);
        NTStrOwned { vec: vec }
    }
}

impl ToNTStr for Path {
    fn to_nt_str(&self) -> NTStrOwned {
        self.as_os_str().as_bytes().to_nt_str()
    }
}

impl<'a> ToNTStr for &'a str {
    fn to_nt_str(&self) -> NTStrOwned {
        self.as_bytes().to_nt_str()
    }
}

pub trait AsNTStr<'a> {
    fn as_nt_str(&'a self) -> NTStrBorrowed<'a>;
}

impl<'a> AsNTStr<'a> for &'a [u8] {
    fn as_nt_str(&'a self) -> NTStrBorrowed<'a> {
        NTStrBorrowed { data: *self }
    }
}

pub trait AsSlice: Sized {
    fn as_slice(&self) -> &[u8] {
        unsafe {
            ::std::mem::transmute(::std::raw::Slice {
                data: self as *const _ as *const u8,
                len: ::std::mem::size_of_val(self),
            })
        }
    }
}

pub trait AsMutSlice: Sized {
    fn as_mut_slice(&mut self) -> &mut [u8] {
        unsafe {
            ::std::mem::transmute(::std::raw::Slice {
                data: self as *mut _ as *const u8,
                len: ::std::mem::size_of_val(self),
            })
        }
    }
}