Compare commits

...

3 Commits

Author SHA1 Message Date
e01d8d5e1a pctl ls List procs by PID 2025-09-19 23:06:37 +02:00
44b5aa305c ulib Add ARG_INT type to argument parser 2025-09-19 23:06:08 +02:00
c94ef4d990 pctl List procs by name 2025-09-19 22:50:34 +02:00
4 changed files with 48 additions and 1 deletions

View File

@ -22,6 +22,10 @@ int32_t parse_args(char **argv, size_t argc, Arg *defs) {
case ARG_BOOL:
*((bool *)def->ptr) = string_strcmp(argv[i+1], "yes") == 0;
break;
case ARG_INT: {
char *end;
*((int32_t *)def->ptr) = string_conv_strtol(argv[i+1], &end, 10);
} break;
}
i++;
}

View File

@ -10,7 +10,11 @@ size_t argslen(void);
typedef struct {
char *shortname;
enum { ARG_STRING, ARG_BOOL } expected_value;
enum {
ARG_STRING,
ARG_BOOL,
ARG_INT,
} expected_value;
void *ptr;
bool end;
} Arg;

View File

@ -7,6 +7,23 @@
#include <uprintf.h>
#include <string/string.h>
#include <dlmalloc/malloc.h>
#include <args/args.h>
#include "ls.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(),
};
const char *human_size(uint64_t bytes, char *buf, size_t bufsize) {
static const char *units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };
@ -41,6 +58,12 @@ const char *human_size(uint64_t bytes, char *buf, size_t bufsize) {
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);
@ -57,6 +80,15 @@ void pctl_ls(void) {
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] = '.';

7
user/pctl/macros.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef PCTL_MACROS_H_
#define PCTL_MACROS_H_
#define SUBCMD_ARGS() (args()+1)
#define SUBCMD_ARGSLEN() (argslen()-1)
#endif // PCTL_MACROS_H_