logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

whoami.c (478B)


  1. #include "../lib/err.h"
  2. #include <errno.h>
  3. #include <pwd.h> // getpwuid
  4. #include <stdio.h>
  5. #include <string.h> // strerror
  6. #include <unistd.h> // geteuid
  7. const char *argv0 = "whoami";
  8. int
  9. main(void)
  10. {
  11. uid_t euid = geteuid();
  12. struct passwd *pw = getpwuid(euid);
  13. if(pw == NULL)
  14. {
  15. const char *errstr = strerror(errno);
  16. if(errno == 0) errstr = "Not Found";
  17. utils_errx(1, "Failed getting name for user ID %d: %s", euid, errstr);
  18. }
  19. puts(pw->pw_name);
  20. return 0;
  21. }