1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 07:34:57 +00:00
serenity/Userland/Utilities/pidof.cpp
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

88 lines
2.5 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
struct Options {
bool single_shot { false };
Optional<pid_t> pid_to_omit;
StringView process_name;
StringView pid_separator { " "sv };
};
static ErrorOr<int> pid_of(Options const& options)
{
bool displayed_at_least_one = false;
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
for (auto& it : all_processes.processes) {
if (it.name != options.process_name || options.pid_to_omit == it.pid)
continue;
if (displayed_at_least_one)
out("{}{}"sv, options.pid_separator, it.pid);
else
out("{}"sv, it.pid);
displayed_at_least_one = true;
if (options.single_shot)
break;
}
if (displayed_at_least_one)
outln();
return 0;
}
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));
Options options;
Core::ArgsParser args_parser;
args_parser.add_option(Core::ArgsParser::Option {
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
.help_string = "Omit the given PID, or the parent process if the special value %PPID is passed",
.short_name = 'o',
.value_name = "pid",
.accept_value = [&options](auto omit_pid_value) {
if (omit_pid_value.is_empty())
return false;
if (omit_pid_value == "%PPID"sv) {
options.pid_to_omit = getppid();
return true;
}
auto number = omit_pid_value.to_uint();
if (!number.has_value())
return false;
options.pid_to_omit = number.value();
return true;
},
});
args_parser.add_option(options.single_shot, "Only return one pid", nullptr, 's');
args_parser.add_option(options.pid_separator, "Use `separator` to separate multiple pids", nullptr, 'S', "separator");
args_parser.add_positional_argument(options.process_name, "Process name to search for", "process-name");
args_parser.parse(args);
return pid_of(options);
}