/* * Copyright (c) 2021, Aziz Berkay Yesilyurt * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include ErrorOr serenity_main(Main::Arguments args) { TRY(Core::System::pledge("stdio rpath")); TRY(Core::System::unveil("/sys/kernel/processes", "r")); TRY(Core::System::unveil("/etc/passwd", "r")); TRY(Core::System::unveil(nullptr, nullptr)); auto pid_delimiter = "\n"sv; bool case_insensitive = false; bool invert_match = false; StringView pattern; Core::ArgsParser args_parser; args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr); args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i'); args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v'); args_parser.add_positional_argument(pattern, "Process name to search for", "process-name"); args_parser.parse(args); PosixOptions options {}; if (case_insensitive) options |= PosixFlags::Insensitive; Regex re(pattern, options); if (re.parser_result.error != regex::Error::NoError) { return 1; } auto all_processes = TRY(Core::ProcessStatisticsReader::get_all()); Vector matches; for (auto& it : all_processes.processes) { auto result = re.match(it.name, PosixFlags::Global); if (result.success ^ invert_match) { matches.append(it.pid); } } quick_sort(matches); auto displayed_at_least_one = false; for (auto& match : matches) { if (displayed_at_least_one) out("{}{}"sv, pid_delimiter, match); else out("{}"sv, match); displayed_at_least_one = true; } if (displayed_at_least_one) outln(); return 0; }