summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorJames Mintram <me@jamesrm.com>2022-04-01 20:47:59 +0100
committerLinus Groh <mail@linusgroh.de>2022-04-02 23:48:17 +0100
commitcd2f67c4ea4120c9afd3fcc28ae6c79096906251 (patch)
tree0d8e22b152d4b6229b74d6ec87ff7cc991be90cb /Userland/Utilities
parent3818840ee684bdd0b62ca71ea50823f00e718bd1 (diff)
downloadserenity-cd2f67c4ea4120c9afd3fcc28ae6c79096906251.zip
top: Add support for quitting top by pressing q
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/top.cpp30
1 files changed, 29 insertions, 1 deletions
diff --git a/Userland/Utilities/top.cpp b/Userland/Utilities/top.cpp
index d8813c0468..1b43ae362d 100644
--- a/Userland/Utilities/top.cpp
+++ b/Userland/Utilities/top.cpp
@@ -177,6 +177,27 @@ static void parse_args(Main::Arguments arguments, TopOption& top_option)
args_parser.parse(arguments);
}
+static bool check_quit()
+{
+ char c = '\0';
+ read(STDIN_FILENO, &c, sizeof(c));
+ return c == 'q' || c == 'Q';
+}
+
+static int g_old_stdin;
+
+static void restore_stdin()
+{
+ fcntl(STDIN_FILENO, F_SETFL, g_old_stdin);
+}
+
+static void enable_nonblocking_stdin()
+{
+ g_old_stdin = fcntl(STDIN_FILENO, F_GETFL);
+ fcntl(STDIN_FILENO, F_SETFL, g_old_stdin | O_NONBLOCK);
+ atexit(restore_stdin);
+}
+
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath tty sigaction"));
@@ -192,6 +213,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TopOption top_option;
parse_args(arguments, top_option);
+ enable_nonblocking_stdin();
+
Vector<ThreadData*> threads;
auto prev = get_snapshot();
usleep(10000);
@@ -277,6 +300,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
threads.clear_with_capacity();
prev = move(current);
- sleep(top_option.delay_time);
+
+ for (int sleep_slice = 0; sleep_slice < top_option.delay_time * 1000; sleep_slice += 100) {
+ if (check_quit())
+ exit(0);
+ usleep(100 * 1000);
+ }
}
}