Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions src/uu/who/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use uucore::utmpx::{self, UtmpxRecord, time};
use std::borrow::Cow;
use std::ffi::CStr;
use std::fmt::Write;
use std::io::{self, Write as _};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the "as _"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it would conflict with use std::fmt::Write;:

error[E0252]: the name `Write` is defined multiple times
  --> src/uu/who/src/platform/unix.rs:21:21
   |
20 | use std::fmt::Write;
   |     --------------- previous import of the trait `Write` here
21 | use std::io::{self, Write};
   |                     ^^^^^ `Write` reimported here
   |

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, just remove the declaration then ?!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. needed for std{out,err}.

use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;

Expand Down Expand Up @@ -216,7 +217,7 @@ impl Who {
let records = utmpx::Utmpx::iter_all_records_from(f);

if self.include_heading {
self.print_heading();
self.print_heading()?;
}
let cur_tty = if self.my_line_only {
current_tty()
Expand All @@ -232,14 +233,20 @@ impl Who {
match ut.record_type() {
rt if self.need_runlevel && run_level_chk(rt) => {
if cfg!(target_os = "linux") {
self.print_runlevel(&ut);
self.print_runlevel(&ut)?;
}
}
utmpx::BOOT_TIME if self.need_boottime => self.print_boottime(&ut),
utmpx::NEW_TIME if self.need_clockchange => self.print_clockchange(&ut),
utmpx::INIT_PROCESS if self.need_initspawn => self.print_initspawn(&ut),
utmpx::LOGIN_PROCESS if self.need_login => self.print_login(&ut),
utmpx::DEAD_PROCESS if self.need_deadprocs => self.print_deadprocs(&ut),
utmpx::BOOT_TIME if self.need_boottime => self.print_boottime(&ut)?,
utmpx::NEW_TIME if self.need_clockchange => {
self.print_clockchange(&ut)?;
}
utmpx::INIT_PROCESS if self.need_initspawn => {
self.print_initspawn(&ut)?;
}
utmpx::LOGIN_PROCESS if self.need_login => self.print_login(&ut)?,
utmpx::DEAD_PROCESS if self.need_deadprocs => {
self.print_deadprocs(&ut)?;
}
_ => {}
}
}
Expand All @@ -252,7 +259,7 @@ impl Who {
}

#[inline]
fn print_runlevel(&self, ut: &UtmpxRecord) {
fn print_runlevel(&self, ut: &UtmpxRecord) -> UResult<()> {
let last = (ut.pid() / 256) as u8 as char;
let curr = (ut.pid() % 256) as u8 as char;
let runlevel_line = translate!("who-runlevel", "level" => curr);
Expand All @@ -268,11 +275,12 @@ impl Who {
"",
if last.is_control() { "" } else { &comment },
"",
);
)?;
Ok(())
}

#[inline]
fn print_clockchange(&self, ut: &UtmpxRecord) {
fn print_clockchange(&self, ut: &UtmpxRecord) -> UResult<()> {
self.print_line(
"",
' ',
Expand All @@ -282,11 +290,12 @@ impl Who {
"",
"",
"",
);
)?;
Ok(())
}

#[inline]
fn print_login(&self, ut: &UtmpxRecord) {
fn print_login(&self, ut: &UtmpxRecord) -> UResult<()> {
let comment = translate!("who-login-id", "id" => ut.terminal_suffix());
let pidstr = format!("{}", ut.pid());
self.print_line(
Expand All @@ -298,11 +307,12 @@ impl Who {
&pidstr,
&comment,
"",
);
)?;
Ok(())
}

#[inline]
fn print_deadprocs(&self, ut: &UtmpxRecord) {
fn print_deadprocs(&self, ut: &UtmpxRecord) -> UResult<()> {
let comment = translate!("who-login-id", "id" => ut.terminal_suffix());
let pidstr = format!("{}", ut.pid());
let e = ut.exit_status();
Expand All @@ -316,11 +326,12 @@ impl Who {
&pidstr,
&comment,
&exitstr,
);
)?;
Ok(())
}

#[inline]
fn print_initspawn(&self, ut: &UtmpxRecord) {
fn print_initspawn(&self, ut: &UtmpxRecord) -> UResult<()> {
let comment = translate!("who-login-id", "id" => ut.terminal_suffix());
let pidstr = format!("{}", ut.pid());
self.print_line(
Expand All @@ -332,11 +343,12 @@ impl Who {
&pidstr,
&comment,
"",
);
)?;
Ok(())
}

#[inline]
fn print_boottime(&self, ut: &UtmpxRecord) {
fn print_boottime(&self, ut: &UtmpxRecord) -> UResult<()> {
self.print_line(
"",
' ',
Expand All @@ -346,7 +358,8 @@ impl Who {
"",
"",
"",
);
)?;
Ok(())
}

fn print_user(&self, ut: &UtmpxRecord) -> UResult<()> {
Expand Down Expand Up @@ -396,7 +409,7 @@ impl Who {
format!("{}", ut.pid()).as_str(),
hoststr.as_str(),
"",
);
)?;

Ok(())
}
Expand All @@ -412,7 +425,7 @@ impl Who {
pid: &str,
comment: &str,
exit: &str,
) {
) -> UResult<()> {
let mut buf = String::with_capacity(64);
let msg = vec![' ', state].into_iter().collect::<String>();

Expand All @@ -435,11 +448,14 @@ impl Who {
if self.include_exit {
write!(buf, " {exit:<12}").unwrap();
}
println!("{}", buf.trim_end());
let mut stdout = io::stdout().lock();
writeln!(stdout, "{}", buf.trim_end()).map_err_context(|| translate!("who-write-error"))?;

Ok(())
}

#[inline]
fn print_heading(&self) {
fn print_heading(&self) -> UResult<()> {
self.print_line(
&translate!("who-heading-name"),
' ',
Expand All @@ -449,6 +465,7 @@ impl Who {
&translate!("who-heading-pid"),
&translate!("who-heading-comment"),
&translate!("who-heading-exit"),
);
)?;
Ok(())
}
}
Loading