1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:14:58 +00:00

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.
This commit is contained in:
Mahmoud Mandour 2021-08-28 11:11:51 +02:00 committed by Ali Mohammad Pur
parent 95751987fb
commit 259ecb3d11

View file

@ -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;