Skip to content

Commit 380da40

Browse files
committed
uptime: make it compile on Windows
1 parent 0cd237b commit 380da40

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/uu/uptime/src/uptime.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use chrono::{Local, TimeZone, Utc};
99
use clap::ArgMatches;
1010
use std::ffi::OsString;
11+
#[cfg(not(windows))]
1112
use std::fs;
1213
use std::io;
14+
#[cfg(unix)]
1315
use std::os::unix::fs::FileTypeExt;
1416
use thiserror::Error;
1517
use uucore::error::set_exit_code;
@@ -27,7 +29,7 @@ use uucore::{format_usage, help_about, help_usage};
2729

2830
#[cfg(target_os = "openbsd")]
2931
use utmp_classic::{parse_from_path, UtmpEntry};
30-
#[cfg(not(target_os = "openbsd"))]
32+
#[cfg(not(any(windows, target_os = "openbsd")))]
3133
use uucore::utmpx::*;
3234

3335
const ABOUT: &str = help_about!("uptime.md");
@@ -42,7 +44,7 @@ use uucore::libc::getloadavg;
4244

4345
#[cfg(windows)]
4446
extern "C" {
45-
fn GetTickCount() -> uucore::libc::uint32_t;
47+
fn GetTickCount() -> u32;
4648
}
4749

4850
#[derive(Debug, Error)]
@@ -203,6 +205,13 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
203205
Ok(())
204206
}
205207

208+
// TODO implement function
209+
#[cfg(not(unix))]
210+
fn uptime_with_file(_file_path: &OsString) -> UResult<()> {
211+
show_error!("not implemented yet");
212+
Ok(())
213+
}
214+
206215
/// Default uptime behaviour i.e. when no file argument is given.
207216
fn default_uptime(matches: &ArgMatches) -> UResult<()> {
208217
#[cfg(target_os = "openbsd")]
@@ -345,7 +354,7 @@ fn print_time() {
345354
print!(" {} ", local_time.format("%H:%M:%S"));
346355
}
347356

348-
#[cfg(not(target_os = "openbsd"))]
357+
#[cfg(not(any(windows, target_os = "openbsd")))]
349358
fn get_uptime_from_boot_time(boot_time: time_t) -> i64 {
350359
let now = Local::now().timestamp();
351360
#[cfg(target_pointer_width = "64")]

tests/by-util/test_uptime.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
use crate::common::util::TestScenario;
1010

11-
#[cfg(not(any(target_os = "macos", target_os = "openbsd")))]
11+
#[cfg(not(any(windows, target_os = "macos", target_os = "openbsd")))]
1212
use bincode::serialize;
1313
use regex::Regex;
14-
#[cfg(not(any(target_os = "macos", target_os = "openbsd")))]
14+
#[cfg(not(any(windows, target_os = "macos", target_os = "openbsd")))]
1515
use serde::Serialize;
16-
#[cfg(not(any(target_os = "macos", target_os = "openbsd")))]
16+
#[cfg(not(any(windows, target_os = "macos", target_os = "openbsd")))]
1717
use serde_big_array::BigArray;
18-
#[cfg(not(any(target_os = "macos", target_os = "openbsd")))]
18+
#[cfg(not(any(windows, target_os = "macos", target_os = "openbsd")))]
1919
use std::fs::File;
20-
#[cfg(not(any(target_os = "macos", target_os = "openbsd")))]
20+
#[cfg(not(any(windows, target_os = "macos", target_os = "openbsd")))]
2121
use std::{io::Write, path::PathBuf};
2222

2323
#[test]
@@ -26,6 +26,7 @@ fn test_invalid_arg() {
2626
}
2727

2828
#[test]
29+
#[cfg(not(windows))]
2930
fn test_uptime() {
3031
TestScenario::new(util_name!())
3132
.ucmd()
@@ -38,7 +39,7 @@ fn test_uptime() {
3839

3940
/// Checks for files without utmpx records for which boot time cannot be calculated
4041
#[test]
41-
#[cfg(not(any(target_os = "openbsd", target_os = "freebsd")))]
42+
#[cfg(not(any(windows, target_os = "openbsd", target_os = "freebsd")))]
4243
// Disabled for freebsd, since it doesn't use the utmpxname() sys call to change the default utmpx
4344
// file that is accessed using getutxent()
4445
fn test_uptime_for_file_without_utmpx_records() {
@@ -82,7 +83,7 @@ fn test_uptime_with_fifo() {
8283
}
8384

8485
#[test]
85-
#[cfg(not(target_os = "freebsd"))]
86+
#[cfg(not(any(windows, target_os = "freebsd")))]
8687
fn test_uptime_with_non_existent_file() {
8788
// Disabled for freebsd, since it doesn't use the utmpxname() sys call to change the default utmpx
8889
// file that is accessed using getutxent()
@@ -98,7 +99,7 @@ fn test_uptime_with_non_existent_file() {
9899
// TODO create a similar test for macos
99100
// This will pass
100101
#[test]
101-
#[cfg(not(any(target_os = "openbsd", target_os = "macos")))]
102+
#[cfg(not(any(windows, target_os = "openbsd", target_os = "macos")))]
102103
#[cfg_attr(
103104
all(target_arch = "aarch64", target_os = "linux"),
104105
ignore = "Issue #7159 - Test not supported on ARM64 Linux"
@@ -244,8 +245,10 @@ fn test_uptime_with_extra_argument() {
244245
.fails()
245246
.stderr_contains("extra operand 'b'");
246247
}
248+
247249
/// Checks whether uptime displays the correct stderr msg when its called with a directory
248250
#[test]
251+
#[cfg(unix)]
249252
fn test_uptime_with_dir() {
250253
let ts = TestScenario::new(util_name!());
251254
let at = &ts.fixtures;

0 commit comments

Comments
 (0)