micro-utils/src/ps.c

78 lines
1.6 KiB
C
Raw Normal View History

2024-07-01 10:23:00 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
2024-07-05 16:54:49 +00:00
#include <pwd.h>
2024-07-10 13:59:36 +00:00
#include <time.h>
#include "proc_parser.h"
#include "human.h"
2024-07-01 10:23:00 +00:00
2024-07-10 13:59:36 +00:00
static int pscan(const pid_t pid) {
struct mu_proc proc;
if (mu_proc_parser("ps", pid, &proc))
2024-07-05 16:54:49 +00:00
return 1;
2024-07-10 13:59:36 +00:00
/* Uid */
char *name = "unknow";
struct passwd *pw = getpwuid(proc.uid);
if (pw != NULL)
name = pw->pw_name;
2024-07-05 16:54:49 +00:00
2024-07-10 13:59:36 +00:00
/* Time */
unsigned int rtime = (proc.utime + proc.stime) / sysconf(_SC_CLK_TCK);
2024-07-01 10:23:00 +00:00
2024-07-10 13:59:36 +00:00
/* Print */
char virt[MU_HUMAN_BUF_SIZE + 1];
strcpy(virt, mu_humansize((off_t)proc.vsize, 1024));
2024-07-05 16:54:49 +00:00
2024-07-10 13:59:36 +00:00
char rss[MU_HUMAN_BUF_SIZE + 1];
strcpy(rss, mu_humansize((off_t)proc.vmrss * 1024, 1024));
2024-07-05 16:54:49 +00:00
2024-07-10 13:59:36 +00:00
printf("%6d %8s %4ld %4ld %8s %8s %2c %02um:%02us %2s\n", proc.pid, name, proc.priority, proc.nice, virt, rss, proc.state, rtime / 60, rtime % 60, proc.prog);
2024-07-01 10:23:00 +00:00
return 0;
}
int main(int argc, char **argv) {
2024-07-10 13:59:36 +00:00
while (getopt(argc, argv, "") != -1) {
puts("ps [a] [PID]\n\t-a Print all processes\n");
2024-07-05 16:54:49 +00:00
return 0;
2024-07-01 10:23:00 +00:00
}
2024-07-05 16:54:49 +00:00
argv += optind;
argc -= optind;
2024-07-01 10:23:00 +00:00
2024-07-10 13:59:36 +00:00
puts(" PID USER PRI NICE VIRT RSS S RTIME CMD");
2024-07-01 10:23:00 +00:00
int ret = 0;
2024-07-05 16:54:49 +00:00
if (argc == 0) {
DIR *dp = opendir("/proc");
if (dp == NULL) {
fprintf(stderr, "ps: /proc: %s\n", strerror(errno));
return 1;
}
struct dirent *ep;
2024-07-10 13:59:36 +00:00
while ((ep = readdir(dp)) != NULL) {
pid_t pid = strtoul(ep->d_name, 0L, 10);
if (pid)
if (pscan(pid))
2024-07-05 16:54:49 +00:00
ret = 1;
2024-07-10 13:59:36 +00:00
}
2024-07-05 16:54:49 +00:00
closedir(dp);
}
else {
2024-07-10 13:59:36 +00:00
for (int i = 0; i < argc; i++) {
pid_t pid = strtoul(argv[i], 0L, 10);
if (pid)
if (pscan(pid))
2024-07-05 16:54:49 +00:00
ret = 1;
2024-07-10 13:59:36 +00:00
}
2024-07-05 16:54:49 +00:00
}
2024-07-01 10:23:00 +00:00
return ret;
}