Debian Codegraph @

src/hostname.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/};
2
3 /// Returns the hostname of the current system.
4 ///
5 /// It is unusual, although technically possible, for this routine to return
6 /// an error. It is difficult to list out the error conditions, but one such
7 /// possibility is platform support.
8 ///
9 /// # Platform specific behavior
10 ///
11 /// On Windows, this currently uses the "physical DNS hostname" computer name.
12 /// This may change in the future.
13 ///
14 /// On Unix, this returns the result of the `gethostname` function from the
15 /// `libc` linked into the program.
16 pub fn hostnamerust-analyzer cargo grep-cli 0.1.12 hostname/hostname().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#> {
17 #[cfg(windows)]
18 {
19 use winapi_util::sysinfo::{ComputerNameKind, get_computer_name};
20 get_computer_name(ComputerNameKind::PhysicalDnsHostname)
21 }
22 #[cfg(unix)]
23 {
24 gethostnamerust-analyzer cargo grep-cli 0.1.12 hostname/gethostname().()
25 }
26 #[cfg(not(any(windows, unix)))]
27 {
28 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::new(
29 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
30 "hostname could not be found on unsupported platform",
31 ))
32 }
33 }
34
35 #[cfg(unix)]
36 fn gethostnamerust-analyzer cargo grep-cli 0.1.12 hostname/gethostname().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#> {
37 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStringExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStringExt#;
38
39 // SAFETY: There don't appear to be any safety requirements for calling
40 // sysconf.
41 let limit = unsafe { libcrust-analyzer cargo libc 0.2.177 crate/::sysconfrust-analyzer cargo libc 0.2.177 sysconf().(libcrust-analyzer cargo libc 0.2.177 crate/::_SC_HOST_NAME_MAXrust-analyzer cargo libc 0.2.177 unix/linux_like/linux/_SC_HOST_NAME_MAX.) };
42 if limit rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialEq<Self>`]eq().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().1 {
43 // It is in theory possible for sysconf to return -1 for a limit but
44 // *not* set errno, in which case, io::Error::last_os_error is
45 // indeterminate. But untangling that is super annoying because std
46 // doesn't expose any unix-specific APIs for inspecting the errno. (We
47 // could do it ourselves, but it just doesn't seem worth doing?)
48 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::last_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]last_os_error().());
49 }
50 let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(maxlen) = usize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[usize][`TryFrom<i64>`]try_from().(limit) else {
51 let msg = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("host name max limit ({}) overflowed usize", limit);
52 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, msg));
53 };
54 // maxlen here includes the NUL terminator.
55 let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; maxlen];
56 // SAFETY: The pointer we give is valid as it is derived directly from a
57 // Vec. Similarly, `maxlen` is the length of our Vec, and is thus valid
58 // to write to.
59 let rc = unsafe {
60 libcrust-analyzer cargo libc 0.2.177 crate/::gethostnamerust-analyzer cargo libc 0.2.177 gethostname().(buf.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<libcrust-analyzer cargo libc 0.2.177 crate/::c_charrust-analyzer cargo libc 0.2.177 primitives/c_char#>(), maxlen)
61 };
62 if rc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1 {
63 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::last_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]last_os_error().());
64 }
65 // POSIX says that if the hostname is bigger than `maxlen`, then it may
66 // write a truncate name back that is not necessarily NUL terminated (wtf,
67 // lol). So if we can't find a NUL terminator, then just give up.
68 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(zeropos) = buf.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|&b| b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0) else {
69 let msg = "could not find NUL terminator in hostname";
70 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, msg));
71 };
72 buf.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(zeropos);
73 buf.shrink_to_fitrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]shrink_to_fit().();
74 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::from_vecrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsString][OsStringExt]from_vec().(buf))
75 }
76
77 #[cfg(test)]
78 mod testsrust-analyzer cargo grep-cli 0.1.12 hostname/tests/ {
79 use superrust-analyzer cargo grep-cli 0.1.12 hostname/::*;
80
81 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
82 fn print_hostnamerust-analyzer cargo grep-cli 0.1.12 hostname/tests/print_hostname().() {
83 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{:?}", hostnamerust-analyzer cargo grep-cli 0.1.12 hostname/hostname().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
84 }
85 }