mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
500044906d
This is not guaranteed to always work correctly as ArgsParser deals in StringViews and might have a non-properly-null-terminated string as a value. As a bonus, using StringView (and DeprecatedString where necessary) leads to nicer looking code too :^)
58 lines
1.6 KiB
C++
58 lines
1.6 KiB
C++
/*
|
|
* Copyright (c) 2021, Aziz Berkay Yesilyurt <abyesilyurt@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/QuickSort.h>
|
|
#include <AK/Vector.h>
|
|
#include <LibCore/ArgsParser.h>
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
|
#include <LibCore/System.h>
|
|
#include <LibMain/Main.h>
|
|
#include <LibRegex/Regex.h>
|
|
|
|
ErrorOr<int> 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));
|
|
|
|
bool case_insensitive = false;
|
|
bool invert_match = false;
|
|
StringView pattern;
|
|
|
|
Core::ArgsParser args_parser;
|
|
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<PosixExtended> re(pattern, options);
|
|
if (re.parser_result.error != regex::Error::NoError) {
|
|
return 1;
|
|
}
|
|
|
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
|
|
|
Vector<pid_t> 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);
|
|
|
|
for (auto& match : matches) {
|
|
outln("{}", match);
|
|
}
|
|
|
|
return 0;
|
|
}
|