summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMahmoud Mandour <ma.mandourr@gmail.com>2021-08-28 11:11:51 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-08-28 15:08:00 +0430
commit259ecb3d11e4f88da4f672d2eec1ec0deebcf01a (patch)
tree49ce0fcc77b448cdeff1923c9b18544c64e557b0
parent95751987fb505b4586c7a3a32a9e0242b0004a05 (diff)
downloadserenity-259ecb3d11e4f88da4f672d2eec1ec0deebcf01a.zip
ps: Select specific processes by their PIDs
This adds a `-q` option, which expects a comma-separated list of PIDs as a value. On using it, only the processes associated with the supplied PIDs are output.
-rw-r--r--Userland/Utilities/ps.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Utilities/ps.cpp b/Userland/Utilities/ps.cpp
index 9b8c55be55..8eb458e9b4 100644
--- a/Userland/Utilities/ps.cpp
+++ b/Userland/Utilities/ps.cpp
@@ -50,10 +50,12 @@ int main(int argc, char** argv)
bool every_process_flag = false;
bool full_format_flag = false;
+ String pid_list;
Core::ArgsParser args_parser;
args_parser.add_option(every_process_flag, "Show every process", nullptr, 'e');
args_parser.add_option(full_format_flag, "Full format", nullptr, 'f');
+ args_parser.add_option(pid_list, "A comma-separated list of PIDs. Only processes matching those PIDs will be selected", nullptr, 'q', "pid-list");
args_parser.parse(argc, argv);
Vector<Column> columns;
@@ -88,6 +90,28 @@ int main(int argc, char** argv)
return 1;
auto& processes = all_processes.value().processes;
+
+ if (!pid_list.is_empty()) {
+ every_process_flag = true;
+ auto string_parts = pid_list.split_view(',');
+ Vector<pid_t> selected_pids;
+ selected_pids.ensure_capacity(string_parts.size());
+
+ for (size_t i = 0; i < string_parts.size(); i++) {
+ auto pid = string_parts[i].to_int();
+
+ if (!pid.has_value()) {
+ warnln("Invalid value for -q: {}", pid_list);
+ warnln("Could not parse '{}' as a PID.", string_parts[i]);
+ return 1;
+ }
+
+ selected_pids.append(pid.value());
+ }
+
+ processes.remove_all_matching([&](auto& a) { return selected_pids.find(a.pid) == selected_pids.end(); });
+ }
+
quick_sort(processes, [](auto& a, auto& b) { return a.pid < b.pid; });
Vector<Vector<String>> rows;