1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #2814 from jfinkels/pinky-uresult

pinky: return UResult from uumain() function
This commit is contained in:
Terts Diepraam 2021-12-29 15:09:58 +01:00 committed by GitHub
commit 12770ca2a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,9 +7,8 @@
// spell-checker:ignore (ToDO) BUFSIZE gecos fullname, mesg iobuf // spell-checker:ignore (ToDO) BUFSIZE gecos fullname, mesg iobuf
#[macro_use]
extern crate uucore;
use uucore::entries::{Locate, Passwd}; use uucore::entries::{Locate, Passwd};
use uucore::error::{FromIo, UResult};
use uucore::libc::S_IWGRP; use uucore::libc::S_IWGRP;
use uucore::utmpx::{self, time, Utmpx}; use uucore::utmpx::{self, time, Utmpx};
@ -52,7 +51,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 let args = args
.collect_str(InvalidEncodingHandling::Ignore) .collect_str(InvalidEncodingHandling::Ignore)
.accept_any(); .accept_any();
@ -122,10 +122,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}; };
if do_short_format { if do_short_format {
pk.short_pinky(); match pk.short_pinky() {
0 Ok(_) => Ok(()),
Err(e) => Err(e.map_err_context(String::new)),
}
} else { } else {
pk.long_pinky() pk.long_pinky();
Ok(())
} }
} }
@ -242,7 +245,7 @@ fn time_string(ut: &Utmpx) -> String {
} }
impl Pinky { impl Pinky {
fn print_entry(&self, ut: &Utmpx) { fn print_entry(&self, ut: &Utmpx) -> std::io::Result<()> {
let mut pts_path = PathBuf::from("/dev"); let mut pts_path = PathBuf::from("/dev");
pts_path.push(ut.tty_device().as_str()); pts_path.push(ut.tty_device().as_str());
@ -291,11 +294,12 @@ impl Pinky {
let mut s = ut.host(); let mut s = ut.host();
if self.include_where && !s.is_empty() { if self.include_where && !s.is_empty() {
s = crash_if_err!(1, ut.canon_host()); s = ut.canon_host()?;
print!(" {}", s); print!(" {}", s);
} }
println!(); println!();
Ok(())
} }
fn print_heading(&self) { fn print_heading(&self) {
@ -314,22 +318,23 @@ impl Pinky {
println!(); println!();
} }
fn short_pinky(&self) { fn short_pinky(&self) -> std::io::Result<()> {
if self.include_heading { if self.include_heading {
self.print_heading(); self.print_heading();
} }
for ut in Utmpx::iter_all_records() { for ut in Utmpx::iter_all_records() {
if ut.is_user_process() { if ut.is_user_process() {
if self.names.is_empty() { if self.names.is_empty() {
self.print_entry(&ut) self.print_entry(&ut)?
} else if self.names.iter().any(|n| n.as_str() == ut.user()) { } else if self.names.iter().any(|n| n.as_str() == ut.user()) {
self.print_entry(&ut); self.print_entry(&ut)?;
} }
} }
} }
Ok(())
} }
fn long_pinky(&self) -> i32 { fn long_pinky(&self) {
for u in &self.names { for u in &self.names {
print!("Login name: {:<28}In real life: ", u); print!("Login name: {:<28}In real life: ", u);
if let Ok(pw) = Passwd::locate(u.as_str()) { if let Ok(pw) = Passwd::locate(u.as_str()) {
@ -359,7 +364,6 @@ impl Pinky {
println!(" ???"); println!(" ???");
} }
} }
0
} }
} }