#include #include #include #include #include #include #include #include #include #include #include "proc_parser.h" #include "human.h" static char c_flag; static int pscan(const pid_t pid, const char *prog_name) { struct mu_proc proc; if (mu_proc_parser("ps", pid, &proc)) return 1; if (prog_name != NULL && strncmp(proc.cmd, prog_name, strlen(prog_name))) return 1; /* Uid */ char *name = "unknow"; struct passwd *pw = getpwuid(proc.uid); if (pw != NULL) name = pw->pw_name; /* Time */ unsigned int rtime = (proc.utime + proc.stime) / sysconf(_SC_CLK_TCK); /* Print */ char virt[MU_HUMAN_BUF_SIZE + 1]; strcpy(virt, mu_humansize(proc.vsize, 1024)); char rss[MU_HUMAN_BUF_SIZE + 1]; strcpy(rss, mu_humansize(proc.vmrss * 1024, 1024)); char *prog = (c_flag) ? proc.cmdline : proc.cmd; if (prog[0] == '\0') return 0; 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, prog); return 0; } static char name_2_pid(const char *prog_name) { DIR *dp = opendir("/proc"); if (dp == NULL) { fprintf(stderr, "ps: /proc: %s\n", strerror(errno)); return 1; } int ret = 0; struct dirent *ep; while ((ep = readdir(dp)) != NULL) { pid_t pid = strtoul(ep->d_name, 0L, 10); if (pid) if (pscan(pid, prog_name)) ret = 1; } closedir(dp); return ret; } int main(int argc, char **argv) { int opt; while ((opt = getopt(argc, argv, "c")) != -1) { switch (opt) { case 'c': c_flag = 1; break; default: puts("ps [c] [PID]\n\t-c Print cmdline instead of program name\n"); return 0; } } argv += optind; argc -= optind; puts(" PID USER PRI NICE VIRT RSS S RTIME CMD"); if (argc == 0) return name_2_pid(NULL); else { int ret = 0; for (int i = 0; i < argc; i++) { pid_t pid = strtoul(argv[i], 0L, 10); if (pid) { if (pscan(pid, NULL)) ret = 1; } else if (name_2_pid(argv[i])) ret = 1; } return ret; } }