Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/uu/last/src/platform/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// Specific implementation for OpenBSD: tool unsupported (utmpx not supported)

use crate::uu_app;

use uucore::error::UResult;

pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let _matches = uu_app().try_get_matches_from(args)?;

println!("unsupported command on macOS");
Ok(())
}
9 changes: 7 additions & 2 deletions src/uu/last/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
mod unix;
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
pub use self::unix::*;

#[cfg(target_os = "openbsd")]
Expand All @@ -17,3 +17,8 @@ pub use self::openbsd::*;
mod windows;
#[cfg(windows)]
pub use self::windows::*;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use self::macos::*;
22 changes: 12 additions & 10 deletions src/uu/last/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl Last {
.unwrap_or_else(|| time::OffsetDateTime::from_unix_timestamp(0).unwrap());
let time_delta = duration_string(calculate_time_delta(&curr_datetime, &shutdown));
if ut.is_user_process() {
proc_status = Some("- down");
proc_status = Some("- down ");
}
(
self.end_time_string(proc_status, &shutdown),
Expand Down Expand Up @@ -520,16 +520,18 @@ impl Last {
write!(buf, " {host_to_print:<16}").unwrap_or_default();
}

let time_size = 3 + 2 + 2 + 1 + 2;
if self.host_last && !self.no_host && self.time_format != "notime" {
write!(buf, " {time:<time_size$}").unwrap_or_default();
write!(buf, " {end_time:<8}").unwrap_or_default();
write!(buf, " {host_to_print}").unwrap_or_default();
} else if self.time_format != "notime" {
write!(buf, " {time:<time_size$}").unwrap_or_default();
write!(buf, " {end_time:<8}").unwrap_or_default();
if self.time_format != "notime" {
let time_fmt = 12;
let end_time_delta = format!("{end_time:<6} {delta}");
let end_time_delta_fmt = 18;

write!(buf, " {time:<time_fmt$}").unwrap_or_default();
write!(buf, " {end_time_delta:<end_time_delta_fmt$}").unwrap_or_default();
}

if self.host_last && !self.no_host {
write!(buf, " {host_to_print:<16}").unwrap_or_default();
}
write!(buf, " {delta:^6}").unwrap_or_default();
println!("{}", buf.trim_end());
}
}
36 changes: 30 additions & 6 deletions tests/by-util/test_last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_invalid_arg() {
}

#[test]
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_last() {
let regex = Regex::new("still running|still logged in").unwrap();
TestScenario::new(util_name!())
Expand All @@ -27,7 +27,7 @@ fn test_last() {
}

#[test]
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_limit_arg() {
let line_check = |input: &str| input.lines().count() == 3;
new_ucmd!()
Expand Down Expand Up @@ -58,7 +58,7 @@ fn test_timestamp_format_no_time() {
}

#[test]
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_timestamp_format_short() {
let regex = Regex::new(" [0-9][0-9]:[0-9][0-9] ").unwrap();
new_ucmd!()
Expand All @@ -68,7 +68,7 @@ fn test_timestamp_format_short() {
}

#[test]
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_timestamp_format_full() {
let regex = Regex::new(" [0-9][0-9]:[0-9][0-9]:[0-9][0-9] ").unwrap();
new_ucmd!()
Expand All @@ -79,7 +79,7 @@ fn test_timestamp_format_full() {

// 2024-07-11T19:30:44+08:00
#[test]
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_timestamp_format_iso() {
let regex =
Regex::new(" [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]")
Expand All @@ -91,7 +91,7 @@ fn test_timestamp_format_iso() {
}

#[test]
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_short_invalid_utmp_file() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "testfile";
Expand All @@ -108,3 +108,27 @@ fn test_short_invalid_utmp_file() {
.succeeds()
.stdout_matches(&regex);
}

#[test]
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "openbsd")))]
fn test_display_hostname_last_column() {
let output_expected = vec![
"ferris tty2 Sat Mar 8 16:29 still logged in :0",
"ferris tty2 Sat Mar 8 16:24 - 16:29 (00:04) :0",
"reboot system boot Sat Mar 8 16:24 still running 6.8.0-55-generic",
];

let hostlast_arg = "--hostlast";
let result = new_ucmd!()
.arg("--file")
.arg("last.input.1")
.arg(hostlast_arg)
.arg("-n")
.arg("3")
.succeeds();

// Keep only the three 1st lines to compare easier with the expected output (so without the information about the begin date of file)
let output_result: Vec<_> = result.stdout_str().lines().take(3).collect();

assert_eq!(output_expected, output_result);
}
Binary file added tests/fixtures/last/last.input.1
Binary file not shown.