ps: Show usernames of processes, using getpwuid()

This commit is contained in:
apio 2022-10-28 20:55:39 +02:00
parent 7d0e442cde
commit 477af66cdc

View File

@ -1,5 +1,6 @@
#include <errno.h> #include <errno.h>
#include <luna/pstat.h> #include <luna/pstat.h>
#include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -17,7 +18,9 @@ pid_t get_current_max_threads()
void display_process(struct pstat* pstatbuf) void display_process(struct pstat* pstatbuf)
{ {
printf("%d %d %ld %ld %s %s (%ld ms)\n", pstatbuf->pt_uid, pstatbuf->pt_gid, pstatbuf->pt_pid, pstatbuf->pt_ppid, struct passwd* pwd = getpwuid(pstatbuf->pt_uid);
if (!pwd && errno) perror("getpwuid");
printf("%s %ld %ld %s %s (%ld ms)\n", pwd ? pwd->pw_name : "???", pstatbuf->pt_pid, pstatbuf->pt_ppid,
pstatbuf->pt_name, pstatname(pstatbuf), pstatbuf->pt_time); pstatbuf->pt_name, pstatname(pstatbuf), pstatbuf->pt_time);
} }
@ -41,4 +44,5 @@ int main()
{ {
if (try_pstat(pid, &pst)) { display_process(&pst); } if (try_pstat(pid, &pst)) { display_process(&pst); }
} }
endpwent();
} }