From a6e701a67bf9d21ede5ccecbce3b3c9fef6ce8c4 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sun, 14 May 2023 13:44:40 +0100 Subject: [PATCH] ps: Add the `-a` option, to list all processes associated with terminals --- Base/usr/share/man/man1/ps.md | 3 ++- Userland/Utilities/ps.cpp | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Base/usr/share/man/man1/ps.md b/Base/usr/share/man/man1/ps.md index 0765c8da9b..36b543bf11 100644 --- a/Base/usr/share/man/man1/ps.md +++ b/Base/usr/share/man/man1/ps.md @@ -5,7 +5,7 @@ ps - list currently running processes ## Synopsis ```**sh -$ ps [--version] [-A] [-e] [-f] [-q pid-list] +$ ps [--version] [-a] [-A] [-e] [-f] [-q pid-list] ``` ## Description @@ -15,6 +15,7 @@ For each process, print its PID (process ID), to which TTY it belongs, and invok ## Options +* `-a`: Consider all processes that are associated with a TTY. * `-A` or `-e`: Consider all processes, not just those in the current TTY. * `-f`: Also print for each process: UID (as resolved username), PPID (parent PID), and STATE (Runnable, Sleeping, Selecting, Reading, etc.) * `-q pid-list`: Only consider the given PIDs, if they exist. diff --git a/Userland/Utilities/ps.cpp b/Userland/Utilities/ps.cpp index bda5e143c7..75195171cd 100644 --- a/Userland/Utilities/ps.cpp +++ b/Userland/Utilities/ps.cpp @@ -54,10 +54,12 @@ ErrorOr serenity_main(Main::Arguments arguments) }; bool every_process_flag = false; + bool every_terminal_process_flag = false; bool full_format_flag = false; StringView pid_list; Core::ArgsParser args_parser; + args_parser.add_option(every_terminal_process_flag, "Show every process associated with terminals", nullptr, 'a'); args_parser.add_option(every_process_flag, "Show every process", nullptr, 'A'); args_parser.add_option(every_process_flag, "Show every process (Equivalent to -A)", nullptr, 'e'); args_parser.add_option(full_format_flag, "Full format", nullptr, 'f'); @@ -140,8 +142,14 @@ ErrorOr serenity_main(Main::Arguments arguments) for (auto const& process : processes) { auto tty = TRY(String::from_deprecated_string(process.tty)); - if (!every_process_flag && tty != this_pseudo_tty_name) + if (every_process_flag) { + // Don't skip any. + } else if (every_terminal_process_flag) { + if (tty.is_empty()) + continue; + } else if (tty != this_pseudo_tty_name) { continue; + } Vector row; TRY(row.try_resize(columns.size()));