]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/mod.rs
c1a4e8cee9ed4079fdbac145417ef76c7b520351
[rustc.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 = "openbsd")] pub use os::openbsd as platform;
30
31 pub mod backtrace;
32 pub mod c;
33 pub mod condvar;
34 pub mod ext;
35 pub mod fd;
36 pub mod fs;
37 pub mod mutex;
38 pub mod net;
39 pub mod os;
40 pub mod os_str;
41 pub mod pipe;
42 pub mod process;
43 pub mod rwlock;
44 pub mod stack_overflow;
45 pub mod sync;
46 pub mod thread;
47 pub mod thread_local;
48 pub mod time;
49 pub mod stdio;
50
51 pub fn decode_error_kind(errno: i32) -> ErrorKind {
52 match errno as libc::c_int {
53 libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
54 libc::ECONNRESET => ErrorKind::ConnectionReset,
55 libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
56 libc::EPIPE => ErrorKind::BrokenPipe,
57 libc::ENOTCONN => ErrorKind::NotConnected,
58 libc::ECONNABORTED => ErrorKind::ConnectionAborted,
59 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
60 libc::EADDRINUSE => ErrorKind::AddrInUse,
61 libc::ENOENT => ErrorKind::NotFound,
62 libc::EINTR => ErrorKind::Interrupted,
63 libc::EINVAL => ErrorKind::InvalidInput,
64 libc::ETIMEDOUT => ErrorKind::TimedOut,
65 libc::consts::os::posix88::EEXIST => ErrorKind::AlreadyExists,
66
67 // These two constants can have the same value on some systems,
68 // but different values on others, so we can't use a match
69 // clause
70 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
71 ErrorKind::WouldBlock,
72
73 _ => ErrorKind::Other,
74 }
75 }
76
77 pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
78 let one: T = T::one();
79 if t == -one {
80 Err(io::Error::last_os_error())
81 } else {
82 Ok(t)
83 }
84 }
85
86 #[allow(deprecated)]
87 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
88 where T: One + PartialEq + Neg<Output=T>, F: FnMut() -> T
89 {
90 loop {
91 match cvt(f()) {
92 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
93 other => return other,
94 }
95 }
96 }
97
98 pub fn ms_to_timeval(ms: u64) -> libc::timeval {
99 libc::timeval {
100 tv_sec: (ms / 1000) as libc::time_t,
101 tv_usec: ((ms % 1000) * 1000) as libc::suseconds_t,
102 }
103 }