1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 13:07:46 +00:00

Merge pull request #2743 from thomasqueirozb/who_uresult

who: use UResult
This commit is contained in:
Sylvestre Ledru 2021-11-19 21:41:14 +01:00 committed by GitHub
commit b4416abc82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 20 deletions

View file

@ -7,8 +7,8 @@
// spell-checker:ignore (ToDO) ttyname hostnames runlevel mesg wtmp statted boottime deadprocs initspawn clockchange curr runlvline pidstr exitstr hoststr
#[macro_use]
extern crate uucore;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
use uucore::libc::{ttyname, STDIN_FILENO, S_IWGRP};
use uucore::utmpx::{self, time, Utmpx};
@ -59,7 +59,8 @@ fn get_long_usage() -> String {
)
}
pub fn uumain(args: impl uucore::Args) -> i32 {
#[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args
.collect_str(InvalidEncodingHandling::Ignore)
.accept_any();
@ -157,9 +158,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
args: files,
};
who.exec();
0
who.exec()
}
pub fn uu_app() -> App<'static, 'static> {
@ -326,7 +325,7 @@ fn current_tty() -> String {
}
impl Who {
fn exec(&mut self) {
fn exec(&mut self) -> UResult<()> {
let run_level_chk = |_record: i16| {
#[cfg(not(target_os = "linux"))]
return false;
@ -362,7 +361,7 @@ impl Who {
for ut in records {
if !self.my_line_only || cur_tty == ut.tty_device() {
if self.need_users && ut.is_user_process() {
self.print_user(&ut);
self.print_user(&ut)?;
} else if self.need_runlevel && run_level_chk(ut.record_type()) {
if cfg!(target_os = "linux") {
self.print_runlevel(&ut);
@ -383,6 +382,7 @@ impl Who {
if ut.record_type() == utmpx::BOOT_TIME {}
}
}
Ok(())
}
#[inline]
@ -464,7 +464,7 @@ impl Who {
self.print_line("", ' ', "system boot", &time_string(ut), "", "", "", "");
}
fn print_user(&self, ut: &Utmpx) {
fn print_user(&self, ut: &Utmpx) -> UResult<()> {
let mut p = PathBuf::from("/dev");
p.push(ut.tty_device().as_str());
let mesg;
@ -491,7 +491,13 @@ impl Who {
};
let s = if self.do_lookup {
crash_if_err!(1, ut.canon_host())
ut.canon_host().map_err_context(|| {
let host = ut.host();
format!(
"failed to canonicalize {}",
host.split(':').next().unwrap_or(&host).quote()
)
})?
} else {
ut.host()
};
@ -507,6 +513,8 @@ impl Who {
hoststr.as_str(),
"",
);
Ok(())
}
#[allow(clippy::too_many_arguments)]

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());
}
}