types.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. use core::mem;
  2. #[cfg(target_os = "redox")]
  3. use syscall::data::TimeSpec as redox_timespec;
  4. // Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
  5. // more optimization opportunities around it recognizing things like
  6. // malloc/free.
  7. #[repr(u8)]
  8. pub enum c_void {
  9. // Two dummy variants so the #[repr] attribute can be used.
  10. #[doc(hidden)]
  11. __variant1,
  12. #[doc(hidden)]
  13. __variant2,
  14. }
  15. pub type int8_t = i8;
  16. pub type int16_t = i16;
  17. pub type int32_t = i32;
  18. pub type int64_t = i64;
  19. pub type uint8_t = u8;
  20. pub type uint16_t = u16;
  21. pub type uint32_t = u32;
  22. pub type uint64_t = u64;
  23. pub type c_schar = i8;
  24. pub type c_uchar = u8;
  25. pub type c_short = i16;
  26. pub type c_ushort = u16;
  27. pub type c_int = i32;
  28. pub type c_uint = u32;
  29. pub type c_float = f32;
  30. pub type c_double = f64;
  31. pub type c_longlong = i64;
  32. pub type c_ulonglong = u64;
  33. pub type intmax_t = i64;
  34. pub type uintmax_t = u64;
  35. pub type size_t = usize;
  36. pub type ptrdiff_t = isize;
  37. pub type intptr_t = isize;
  38. pub type uintptr_t = usize;
  39. pub type ssize_t = isize;
  40. pub type c_char = i8;
  41. pub type c_long = i64;
  42. pub type c_ulong = u64;
  43. pub type wchar_t = i32;
  44. pub type wint_t = u32;
  45. pub type wctype_t = i64;
  46. pub type off_t = c_long;
  47. pub type mode_t = c_int;
  48. pub type time_t = c_long;
  49. pub type pid_t = c_int;
  50. pub type id_t = c_uint;
  51. pub type gid_t = c_int;
  52. pub type uid_t = c_int;
  53. pub type dev_t = c_long;
  54. pub type ino_t = c_ulong;
  55. pub type nlink_t = c_ulong;
  56. pub type blksize_t = c_long;
  57. pub type blkcnt_t = c_ulong;
  58. pub type useconds_t = c_uint;
  59. pub type suseconds_t = c_int;
  60. pub type clock_t = c_long;
  61. pub type clockid_t = c_int;
  62. pub type timer_t = *mut c_void;
  63. #[repr(C)]
  64. #[derive(Default)]
  65. pub struct timespec {
  66. pub tv_sec: time_t,
  67. pub tv_nsec: c_long,
  68. }
  69. #[repr(C)]
  70. #[derive(Default)]
  71. pub struct timeval {
  72. pub tv_sec: time_t,
  73. pub tv_usec: suseconds_t,
  74. }
  75. #[repr(C)]
  76. #[derive(Default)]
  77. pub struct timezone {
  78. pub tz_minuteswest: c_int,
  79. pub tz_dsttime: c_int,
  80. }
  81. #[repr(C)]
  82. #[derive(Default)]
  83. pub struct itimerval {
  84. pub it_interval: timeval,
  85. pub it_value: timeval,
  86. }
  87. #[cfg(target_os = "redox")]
  88. impl<'a> From<&'a timespec> for redox_timespec {
  89. fn from(tp: &timespec) -> redox_timespec {
  90. redox_timespec {
  91. tv_sec: tp.tv_sec,
  92. tv_nsec: tp.tv_nsec as i32,
  93. }
  94. }
  95. }
  96. #[repr(C)]
  97. pub struct stat {
  98. pub st_dev: dev_t,
  99. pub st_ino: ino_t,
  100. pub st_nlink: nlink_t,
  101. pub st_mode: mode_t,
  102. pub st_uid: uid_t,
  103. pub st_gid: gid_t,
  104. pub st_rdev: dev_t,
  105. pub st_size: off_t,
  106. pub st_blksize: blksize_t,
  107. pub st_blocks: blkcnt_t,
  108. pub st_atim: timespec,
  109. pub st_mtim: timespec,
  110. pub st_ctim: timespec,
  111. // Compared to glibc, our struct is for some reason 24 bytes too small.
  112. // Accessing atime works, so clearly the struct isn't incorrect...
  113. // This works.
  114. pub _pad: [c_char; 24],
  115. }
  116. pub const AF_INET: c_int = 2;
  117. pub const SOCK_STREAM: c_int = 1;
  118. pub const SOCK_DGRAM: c_int = 2;
  119. pub const SOCK_NONBLOCK: c_int = 0o4000;
  120. pub const SOCK_CLOEXEC: c_int = 0o2000000;
  121. pub const SIG_BLOCK: c_int = 0;
  122. pub const SIG_UNBLOCK: c_int = 1;
  123. pub const SIG_SETMASK: c_int = 2;
  124. pub type in_addr_t = [u8; 4];
  125. pub type in_port_t = u16;
  126. pub type sa_family_t = u16;
  127. pub type socklen_t = u32;
  128. #[repr(C)]
  129. pub struct sockaddr {
  130. pub sa_family: sa_family_t,
  131. pub data: [c_char; 14],
  132. }
  133. #[repr(C)]
  134. #[derive(Debug, Clone, Copy)]
  135. pub struct in_addr {
  136. pub s_addr: in_addr_t,
  137. }
  138. #[repr(C)]
  139. pub struct sockaddr_in {
  140. pub sin_family: sa_family_t,
  141. pub sin_port: in_port_t,
  142. pub sin_addr: in_addr,
  143. }
  144. #[repr(C)]
  145. pub struct sigaction {
  146. pub sa_handler: Option<extern "C" fn(c_int)>,
  147. pub sa_flags: c_ulong,
  148. pub sa_restorer: Option<unsafe extern "C" fn()>,
  149. pub sa_mask: sigset_t,
  150. }
  151. pub type sigset_t = c_ulong;
  152. const UTSLENGTH: usize = 65;
  153. #[repr(C)]
  154. pub struct utsname {
  155. pub sysname: [c_char; UTSLENGTH],
  156. pub nodename: [c_char; UTSLENGTH],
  157. pub release: [c_char; UTSLENGTH],
  158. pub version: [c_char; UTSLENGTH],
  159. pub machine: [c_char; UTSLENGTH],
  160. pub domainname: [c_char; UTSLENGTH],
  161. }
  162. #[repr(C)]
  163. pub struct dirent {
  164. pub d_ino: ino_t,
  165. pub d_off: off_t,
  166. pub d_reclen: c_ushort,
  167. pub d_type: c_uchar,
  168. pub d_name: [c_char; 256],
  169. }
  170. #[repr(C)]
  171. #[derive(Default)]
  172. pub struct winsize {
  173. ws_row: c_ushort,
  174. ws_col: c_ushort,
  175. ws_xpixel: c_ushort,
  176. ws_ypixel: c_ushort,
  177. }
  178. #[repr(C)]
  179. pub struct rusage {
  180. pub ru_utime: timeval,
  181. pub ru_stime: timeval,
  182. pub ru_maxrss: c_long,
  183. pub ru_ixrss: c_long,
  184. pub ru_idrss: c_long,
  185. pub ru_isrss: c_long,
  186. pub ru_minflt: c_long,
  187. pub ru_majflt: c_long,
  188. pub ru_nswap: c_long,
  189. pub ru_inblock: c_long,
  190. pub ru_oublock: c_long,
  191. pub ru_msgsnd: c_long,
  192. pub ru_msgrcv: c_long,
  193. pub ru_nsignals: c_long,
  194. pub ru_nvcsw: c_long,
  195. pub ru_nivcsw: c_long,
  196. }
  197. #[repr(C)]
  198. pub struct tms {
  199. tms_utime: clock_t,
  200. tms_stime: clock_t,
  201. tms_cutime: clock_t,
  202. tms_cstime: clock_t,
  203. }
  204. pub const FD_SETSIZE: usize = 1024;
  205. #[repr(C)]
  206. pub struct fd_set {
  207. pub fds_bits: [c_ulong; FD_SETSIZE / (8 * mem::size_of::<c_ulong>())],
  208. }
  209. pub const F_OK: c_int = 0;
  210. pub const R_OK: c_int = 4;
  211. pub const W_OK: c_int = 2;
  212. pub const X_OK: c_int = 1;
  213. pub type cc_t = u8;
  214. pub type speed_t = u32;
  215. pub type tcflag_t = u32;
  216. pub const NCCS: usize = 32;
  217. #[repr(C)]
  218. pub struct termios {
  219. c_iflag: tcflag_t,
  220. c_oflag: tcflag_t,
  221. c_cflag: tcflag_t,
  222. c_lflag: tcflag_t,
  223. c_line: cc_t,
  224. c_cc: [cc_t; NCCS],
  225. __c_ispeed: speed_t,
  226. __c_ospeed: speed_t,
  227. }