]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/mod.rs
Add netbsd amd64 support
[rust.git] / src / libstd / sys / unix / mod.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(missing_docs)]
12 #![allow(non_camel_case_types)]
13
14 use prelude::v1::*;
15
16 use io::{self, ErrorKind};
17 use libc;
18 use num::One;
19 use ops::Neg;
20
21 #[cfg(target_os = "android")]   pub use os::android as platform;
22 #[cfg(target_os = "bitrig")]    pub use os::bitrig as platform;
23 #[cfg(target_os = "dragonfly")] pub use os::dragonfly as platform;
24 #[cfg(target_os = "freebsd")]   pub use os::freebsd as platform;
25 #[cfg(target_os = "ios")]       pub use os::ios as platform;
26 #[cfg(target_os = "linux")]     pub use os::linux as platform;
27 #[cfg(target_os = "macos")]     pub use os::macos as platform;
28 #[cfg(target_os = "nacl")]      pub use os::nacl as platform;
29 #[cfg(target_os = "netbsd")]    pub use os::netbsd as platform;
30 #[cfg(target_os = "openbsd")]   pub use os::openbsd as platform;
31
32 pub mod backtrace;
33 pub mod c;
34 pub mod condvar;
35 pub mod ext;
36 pub mod fd;
37 pub mod fs;
38 pub mod mutex;
39 pub mod net;
40 pub mod os;
41 pub mod os_str;
42 pub mod pipe;
43 pub mod process;
44 pub mod rwlock;
45 pub mod stack_overflow;
46 pub mod sync;
47 pub mod thread;
48 pub mod thread_local;
49 pub mod time;
50 pub mod stdio;
51
52 pub fn decode_error_kind(errno: i32) -> ErrorKind {
53     match errno as libc::c_int {
54         libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
55         libc::ECONNRESET => ErrorKind::ConnectionReset,
56         libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
57         libc::EPIPE => ErrorKind::BrokenPipe,
58         libc::ENOTCONN => ErrorKind::NotConnected,
59         libc::ECONNABORTED => ErrorKind::ConnectionAborted,
60         libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
61         libc::EADDRINUSE => ErrorKind::AddrInUse,
62         libc::ENOENT => ErrorKind::NotFound,
63         libc::EINTR => ErrorKind::Interrupted,
64         libc::EINVAL => ErrorKind::InvalidInput,
65         libc::ETIMEDOUT => ErrorKind::TimedOut,
66         libc::consts::os::posix88::EEXIST => ErrorKind::AlreadyExists,
67
68         // These two constants can have the same value on some systems,
69         // but different values on others, so we can't use a match
70         // clause
71         x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
72             ErrorKind::WouldBlock,
73
74         _ => ErrorKind::Other,
75     }
76 }
77
78 pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
79     let one: T = T::one();
80     if t == -one {
81         Err(io::Error::last_os_error())
82     } else {
83         Ok(t)
84     }
85 }
86
87 #[allow(deprecated)]
88 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
89     where T: One + PartialEq + Neg<Output=T>, F: FnMut() -> T
90 {
91     loop {
92         match cvt(f()) {
93             Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
94             other => return other,
95         }
96     }
97 }
98
99 pub fn ms_to_timeval(ms: u64) -> libc::timeval {
100     libc::timeval {
101         tv_sec: (ms / 1000) as libc::time_t,
102         tv_usec: ((ms % 1000) * 1000) as libc::suseconds_t,
103     }
104 }