blob: 8302ed61dd30c99e9c46a0efed96e5e67eeb0cac (
plain) (
blame)
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
|
use std::io::prelude::*;
use std::io::{ErrorKind, self};
use std::ffi::OsString;
use std::os::unix::prelude::*;
use libc::{gethostname, _SC_HOST_NAME_MAX};
pub fn copy_stream(src: &mut dyn Read, dst: &mut dyn Write) -> io::Result<()> {
let mut buf = vec![0; 65536];
loop {
match src.read(&mut buf) {
Ok(0) => return Ok(()),
Ok(len) => dst.write_all(&buf[..len])?,
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
}
}
}
pub fn hostname() -> OsString {
let mut bytes = vec![0; _SC_HOST_NAME_MAX as usize + 2];
if unsafe { gethostname(bytes.as_mut_ptr(), bytes.len()) } == -1 {
panic!("gethostname failed: {}", std::io::Error::last_os_error());
}
if bytes[_SC_HOST_NAME_MAX as usize + 1] != 0 {
panic!("hostname longer than HOST_NAME_MAX");
}
let bytes: Vec<_> = bytes
.into_iter()
.take_while(|b| *b != 0)
.map(|b| b as u8)
.collect();
OsString::from_vec(bytes)
}
|