Files
my-os-project2/user/pctl/ls.c

66 lines
1.6 KiB
C

#include <stddef.h>
#include <stdint.h>
#include <ulib.h>
#include "macros.h"
struct {
char *specificproc;
int32_t pid;
} PCTL_LS_CONFIG = {
.specificproc = NULL,
.pid = -1,
};
static Arg ARGS[] = {
ARG("-proc", ARG_STRING, &PCTL_LS_CONFIG.specificproc),
ARG("-pid", ARG_INT, &PCTL_LS_CONFIG.pid),
ARG_END(),
};
void pctl_ls(void) {
int32_t ret;
if ((ret = parse_args(SUBCMD_ARGS(), SUBCMD_ARGSLEN(), ARGS)) < 0) {
uprintf("pctl ls: Could not parse args: %d\n", ret);
return;
}
static const char *states[] = {"embryo", "ready", "zombie", "waiting", "died"};
uint64_t procslen = processctl(-1, PCTL_PLS_SZ, 0, 0, 0);
char *namebuf = umalloc(34);
char *membuf = umalloc(20);
uprintf("%-30s %s %-15s %-8s\n", "NAME", "PID", "MEMORY", "STATE");
for (size_t i = 0; i < procslen; i++) {
ProcStat stat; ZERO(&stat);
string_memset(namebuf, 0, 34);
string_memset(membuf, 0, 20);
int32_t r = processctl(-1, PCTL_PLS_STAT, i, (uint64_t)&stat, 0);
if (r == E_OK) {
if (PCTL_LS_CONFIG.specificproc != NULL
&& string_strcmp(stat.name, PCTL_LS_CONFIG.specificproc) != 0) {
continue;
}
if (PCTL_LS_CONFIG.pid != -1 && stat.pid != PCTL_LS_CONFIG.pid) {
continue;
}
string_memcpy(namebuf, stat.name, 30);
namebuf[31] = namebuf[32] = namebuf[33] = '.';
// Too big format string causes a stack overflow. This is an issue with printf ;(
uprintf("%-30s %-3lu %-15s ",
namebuf,
stat.pid,
human_size(stat.usemem, membuf, 20)
);
uprintf("%-8s", states[stat.state]);
uprintf("\n");
}
}
}