Skip to content

Commit cb6a2d6

Browse files
tcl3awesomekling
authored andcommitted
pgrep: Add -l option to list the process name as well as its pid
1 parent ad85170 commit cb6a2d6

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Base/usr/share/man/man1/pgrep.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ pgrep - look up processes based on name
55
## Synopsis
66

77
```sh
8-
$ pgrep [--count] [-d delimiter] [--ignore-case] [--invert-match] <process-name>
8+
$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--invert-match] <process-name>
99
```
1010

1111
## Options
1212

1313
* `-c`, `--count`: Suppress normal output and print the number of matching processes
1414
* `-d`, `--delimiter`: Set the string used to delimit multiple pids
1515
* `-i`, `--ignore-case`: Make matches case-insensitive
16+
* `-l`, `--list-name`: List the process name in addition to its pid
1617
* `-v`, `--invert-match`: Select non-matching lines
1718

1819
## Arguments

Userland/Utilities/pgrep.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ ErrorOr<int> serenity_main(Main::Arguments args)
2323
bool display_number_of_matches = false;
2424
auto pid_delimiter = "\n"sv;
2525
bool case_insensitive = false;
26+
bool list_process_name = false;
2627
bool invert_match = false;
2728
StringView pattern;
2829

2930
Core::ArgsParser args_parser;
3031
args_parser.add_option(display_number_of_matches, "Suppress normal output and print the number of matching processes", "count", 'c');
3132
args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr);
3233
args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i');
34+
args_parser.add_option(list_process_name, "List the process name in addition to its pid", "list-name", 'l');
3335
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
3436
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
3537
args_parser.parse(args);
@@ -45,24 +47,27 @@ ErrorOr<int> serenity_main(Main::Arguments args)
4547

4648
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
4749

48-
Vector<pid_t> matches;
49-
for (auto& it : all_processes.processes) {
50+
Vector<Core::ProcessStatistics> matches;
51+
for (auto const& it : all_processes.processes) {
5052
auto result = re.match(it.name, PosixFlags::Global);
5153
if (result.success ^ invert_match) {
52-
matches.append(it.pid);
54+
matches.append(it);
5355
}
5456
}
5557

5658
if (display_number_of_matches) {
5759
outln("{}", matches.size());
5860
} else {
59-
quick_sort(matches);
61+
quick_sort(matches, [](auto const& a, auto const& b) { return a.pid < b.pid; });
6062
auto displayed_at_least_one = false;
6163
for (auto& match : matches) {
6264
if (displayed_at_least_one)
63-
out("{}{}"sv, pid_delimiter, match);
64-
else
65-
out("{}"sv, match);
65+
out("{}"sv, pid_delimiter);
66+
67+
out("{}"sv, match.pid);
68+
69+
if (list_process_name)
70+
out(" {}"sv, match.name);
6671

6772
displayed_at_least_one = true;
6873
}

0 commit comments

Comments
 (0)