1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 21:47:46 +00:00

uucore/utmpx: remove unwrap in cannon_host

Default to hostname if getaddrinfo fails
This commit is contained in:
Thomas Queiroz 2021-11-09 19:57:24 -03:00
parent 2e12316ae1
commit 235152a6b7
No known key found for this signature in database
GPG key ID: 229D2DDF7ECA5F8F

View file

@ -236,17 +236,20 @@ impl Utmpx {
flags: AI_CANONNAME,
..AddrInfoHints::default()
};
let sockets = getaddrinfo(Some(hostname), None, Some(hints))
.unwrap()
.collect::<IOResult<Vec<_>>>()?;
for socket in sockets {
if let Some(ai_canonname) = socket.canonname {
return Ok(if display.is_empty() {
ai_canonname
} else {
format!("{}:{}", ai_canonname, display)
});
if let Ok(sockets) = getaddrinfo(Some(hostname), None, Some(hints)) {
let sockets = sockets.collect::<IOResult<Vec<_>>>()?;
for socket in sockets {
if let Some(ai_canonname) = socket.canonname {
return Ok(if display.is_empty() {
ai_canonname
} else {
format!("{}:{}", ai_canonname, display)
});
}
}
} else {
// GNU coreutils has this behavior
return Ok(hostname.to_string());
}
}