mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:37:34 +00:00
ps: Add some options and slim down the default output style
The following options are now available: -e: Show every process (not just the ones on your TTY) -f: Full format (instead of the short format) We should definitely support more options and formats, I just wanted to get away from the static single style of output. :^)
This commit is contained in:
parent
24a5a6cd2c
commit
6fa5df783b
1 changed files with 87 additions and 16 deletions
103
Userland/ps.cpp
103
Userland/ps.cpp
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/ProcessStatisticsReader.h>
|
||||
#include <fcntl.h>
|
||||
|
@ -32,6 +33,13 @@
|
|||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (pledge("stdio rpath tty", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
String this_tty = ttyname(STDIN_FILENO);
|
||||
|
||||
if (pledge("stdio rpath", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
|
@ -49,10 +57,67 @@ int main(int argc, char** argv)
|
|||
|
||||
unveil(nullptr, nullptr);
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
enum class Alignment {
|
||||
Left,
|
||||
Right,
|
||||
};
|
||||
|
||||
printf("PID TPG PGP SID UID STATE PPID NSCHED FDS TTY NAME\n");
|
||||
struct Column {
|
||||
String title;
|
||||
Alignment alignment { Alignment::Left };
|
||||
int width { 0 };
|
||||
String buffer;
|
||||
};
|
||||
|
||||
bool every_process_flag = false;
|
||||
bool full_format_flag = false;
|
||||
|
||||
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.parse(argc, argv);
|
||||
|
||||
Vector<Column> columns;
|
||||
|
||||
int uid_column = -1;
|
||||
int pid_column = -1;
|
||||
int ppid_column = -1;
|
||||
int state_column = -1;
|
||||
int tty_column = -1;
|
||||
int cmd_column = -1;
|
||||
|
||||
auto add_column = [&](auto title, auto alignment, auto width) {
|
||||
columns.append({ title, alignment, width, {} });
|
||||
return columns.size() - 1;
|
||||
};
|
||||
|
||||
if (full_format_flag) {
|
||||
uid_column = add_column("UID", Alignment::Left, 8);
|
||||
pid_column = add_column("PID", Alignment::Right, 5);
|
||||
ppid_column = add_column("PPID", Alignment::Right, 5);
|
||||
state_column = add_column("STATE", Alignment::Left, 12);
|
||||
tty_column = add_column("TTY", Alignment::Left, 6);
|
||||
cmd_column = add_column("CMD", Alignment::Left, 0);
|
||||
} else {
|
||||
pid_column = add_column("PID", Alignment::Right, 5);
|
||||
tty_column = add_column("TTY", Alignment::Left, 6);
|
||||
cmd_column = add_column("CMD", Alignment::Left, 0);
|
||||
}
|
||||
|
||||
auto print_column = [](auto& column, auto& string) {
|
||||
if (!column.width) {
|
||||
printf("%s", string.characters());
|
||||
return;
|
||||
}
|
||||
if (column.alignment == Alignment::Right)
|
||||
printf("%*s ", column.width, string.characters());
|
||||
else
|
||||
printf("%-*s ", column.width, string.characters());
|
||||
};
|
||||
|
||||
for (auto& column : columns)
|
||||
print_column(column, column.title);
|
||||
printf("\n");
|
||||
|
||||
auto all_processes = Core::ProcessStatisticsReader::get_all();
|
||||
|
||||
|
@ -60,26 +125,32 @@ int main(int argc, char** argv)
|
|||
const auto& proc = it.value;
|
||||
auto tty = proc.tty;
|
||||
|
||||
if (!every_process_flag && tty != this_tty)
|
||||
continue;
|
||||
|
||||
if (tty.starts_with("/dev/"))
|
||||
tty = tty.characters() + 5;
|
||||
else
|
||||
tty = "n/a";
|
||||
|
||||
auto* state = proc.threads.is_empty() ? "Zombie" : proc.threads.first().state.characters();
|
||||
auto times_scheduled = proc.threads.is_empty() ? 0 : proc.threads.first().times_scheduled;
|
||||
|
||||
printf("%-3u %-3u %-3u %-3u %-3u %-11s %-3u %-9u %-3u %-5s %s\n",
|
||||
proc.pid,
|
||||
proc.pgid,
|
||||
proc.pgp,
|
||||
proc.sid,
|
||||
proc.uid,
|
||||
state,
|
||||
proc.ppid,
|
||||
times_scheduled,
|
||||
proc.nfds,
|
||||
tty.characters(),
|
||||
proc.name.characters());
|
||||
if (uid_column != -1)
|
||||
columns[uid_column].buffer = proc.username;
|
||||
if (pid_column != -1)
|
||||
columns[pid_column].buffer = String::number(proc.pid);
|
||||
if (ppid_column != -1)
|
||||
columns[ppid_column].buffer = String::number(proc.ppid);
|
||||
if (tty_column != -1)
|
||||
columns[tty_column].buffer = tty;
|
||||
if (state_column != -1)
|
||||
columns[state_column].buffer = state;
|
||||
if (cmd_column != -1)
|
||||
columns[cmd_column].buffer = proc.name;
|
||||
|
||||
for (auto& column : columns)
|
||||
print_column(column, column.buffer);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue