Skip to content

Commit

Permalink
w: Format time and get cmdline
Browse files Browse the repository at this point in the history
This change allows w to display time in a similar way to the original
procps w. Additionally, w now fetches the cmdline for each entry.
  • Loading branch information
fortifiedhill committed Mar 27, 2024
1 parent 386d565 commit 61789e2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ xattr = "1.3.1"
tempfile = "3.9.0"
rand = { version = "0.8", features = ["small_rng"] }
bytesize = "1.3.0"
time = { version = "0.3", features = ["formatting"] }

[workspace.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
utmpx = "0.1"
Expand Down Expand Up @@ -75,6 +76,7 @@ tempfile = { workspace = true }
libc = { workspace = true }
rand = { workspace = true }
uucore = { workspace = true, features = ["entries", "process", "signals"] }
time = { workspace = true, features = ["formatting"] }

[target.'cfg(unix)'.dev-dependencies]
xattr = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions src/uu/w/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ categories = ["command-line-utilities"]
[dependencies]
uucore = { workspace = true, features = ["utmpx"] }
clap = { workspace = true }
time = { workspace = true, features = ["formatting"] }

['cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
utmpx = { workspace = true }
Expand Down
19 changes: 15 additions & 4 deletions src/uu/w/src/w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

use clap::crate_version;
use clap::{Arg, ArgAction, Command};
use std::process;
use std::{fs, path::Path, process};
use time;
use uucore::utmpx::Utmpx;
use uucore::{error::UResult, format_usage, help_about, help_usage};

Expand All @@ -22,18 +23,28 @@ struct UserInfo {
command: String,
}

fn format_time(time: time::OffsetDateTime) -> Result<String, time::error::Format> {
let time_format = time::format_description::parse("[hour]:[minute]").unwrap();
time.format(&time_format)
}

Check warning on line 29 in src/uu/w/src/w.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/w/src/w.rs#L26-L29

Added lines #L26 - L29 were not covered by tests

fn fetch_cmdline(pid: i32) -> Result<String, std::io::Error> {
let cmdline_path = Path::new("/proc").join(pid.to_string()).join("cmdline");
fs::read_to_string(cmdline_path)
}

Check warning on line 34 in src/uu/w/src/w.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/w/src/w.rs#L31-L34

Added lines #L31 - L34 were not covered by tests

fn fetch_user_info() -> Result<Vec<UserInfo>, std::io::Error> {
let mut user_info_list = Vec::new();
for entry in Utmpx::iter_all_records() {
if entry.is_user_process() {
let user_info = UserInfo {
user: entry.user(),
terminal: entry.tty_device(),
login_time: format!("{}", entry.login_time()), // Needs formatting
login_time: format_time(entry.login_time()).unwrap_or_default(),

Check warning on line 43 in src/uu/w/src/w.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/w/src/w.rs#L43

Added line #L43 was not covered by tests
idle_time: String::new(), // Placeholder, needs actual implementation
jcpu: String::new(), // Placeholder, needs actual implementation
pcpu: String::new(), // Placeholder, needs actual implementation
command: String::new(), // Placeholder, needs actual implementation
command: fetch_cmdline(entry.pid()).unwrap_or_default(),

Check warning on line 47 in src/uu/w/src/w.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/w/src/w.rs#L47

Added line #L47 was not covered by tests
};
user_info_list.push(user_info);
}
Expand All @@ -50,7 +61,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
match fetch_user_info() {
Ok(user_info) => {
if !no_header {
println!("USER\tTTY\t\tLOGIN@\t\tIDLE\tJCPU\tPCPU\tWHAT");
println!("USER\tTTY\tLOGIN@\tIDLE\tJCPU\tPCPU\tWHAT");

Check warning on line 64 in src/uu/w/src/w.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/w/src/w.rs#L64

Added line #L64 was not covered by tests
}
for user in user_info {
println!(
Expand Down

0 comments on commit 61789e2

Please sign in to comment.