From 6a4d4a41a664e6a4c406a449ea847abd4a224bcf Mon Sep 17 00:00:00 2001 From: dzwdz Date: Fri, 22 Sep 2023 23:42:30 +0200 Subject: build: support single file commands --- src/cmd/ps.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/cmd/ps.c (limited to 'src/cmd/ps.c') diff --git a/src/cmd/ps.c b/src/cmd/ps.c new file mode 100644 index 0000000..c855251 --- /dev/null +++ b/src/cmd/ps.c @@ -0,0 +1,54 @@ +#include <_proc.h> +#include +#include +#include +#include +#include +#include + +void +usage(void) +{ + fprintf(stderr, "usage: ps [path]\n"); + exit(1); +} + +int +main(int argc, const char *argv[]) +{ + const char *path = argc >= 2 ? argv[1] : "/proc/"; + if (argc > 2) usage(); + + char *procbuf = malloc(4096); + DIR *dir = opendir(path); + if (!dir) { + err(1, "couldn't open %s", path); + } + + struct dirent *de; + while ((de = readdir(dir))) { + const char *name = de->d_name; + if (isdigit(name[0])) { + FILE *g; + sprintf(procbuf, "%s%smem", path, name); + g = fopen(procbuf, "r"); + if (!g) { + warn("couldn't open \"%s\"", procbuf); + strcpy(procbuf, "(can't peek)"); + } else { + fseek(g, (long)&_psdata_loc->desc, SEEK_SET); + if (fread(procbuf, 1, 128, g) <= 0) { + strcpy(procbuf, "(no psdata)"); + } + procbuf[128] = '\0'; + fclose(g); + } + *strchr(name, '/') = '\0'; + printf("%s\t%s\n", name, procbuf); + } + } + + free(procbuf); + closedir(dir); + return 0; +} -- cgit v1.2.3