Skip to content

Commit

Permalink
uptime: Bring output closer to Linux/BSDs, and add -p flag
Browse files Browse the repository at this point in the history
The -p flag is equivalent to the previous behavior: outputting the
uptime in a human-readable form.

We don't seem to expose the number of online users or the load averages,
so those sections are missing from the output compared to those OSes.
  • Loading branch information
AtkinsSJ authored and awesomekling committed Jan 25, 2024
1 parent 388856d commit 8faeb13
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Base/usr/share/man/man1/uptime.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ uptime - Tell how long the system has been running
$ uptime
```

## Description

`uptime` outputs information about the system, in a single line, to STDOUT.
This information includes when the system came online and how long it has been up.

## Options

* `-p`, `--pretty`: Output only the uptime, in human-readable format.

## Examples

```sh
$ uptime
2024-01-24 06:23:27 up 4:20:00
```

```sh
$ uptime -p
Up 2 minutes, 20 seconds
```
21 changes: 19 additions & 2 deletions Userland/Utilities/uptime.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2022, Karol Kosek <[email protected]>
* Copyright (c) 2024, Sam Atkins <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/NumberFormat.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>

ErrorOr<int> serenity_main(Main::Arguments)
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));

bool pretty_output = false;

Core::ArgsParser args_parser;
args_parser.add_option(pretty_output, "Output only the uptime, in human-readable format", "pretty", 'p');
args_parser.parse(arguments);

auto file = TRY(Core::File::open("/sys/kernel/uptime"sv, Core::File::OpenMode::Read));

TRY(Core::System::pledge("stdio"));
Expand All @@ -25,6 +34,14 @@ ErrorOr<int> serenity_main(Main::Arguments)
return Error::from_string_literal("Couldn't convert to number");
auto seconds = maybe_seconds.release_value();

outln("Up {}", human_readable_time(seconds));
if (pretty_output) {
outln("Up {}", human_readable_time(seconds));
} else {
auto current_time = TRY(Core::DateTime::now().to_string());
// FIXME: To match Linux and the BSDs, we should also include the number of current users,
// and some load averages, but these don't seem to be available yet.
outln("{} up {}", current_time, human_readable_digital_time(seconds));
}

return 0;
}

0 comments on commit 8faeb13

Please sign in to comment.