logo

utils-std

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

whoami.c (694B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "../lib/err.h"
  6. #include <errno.h>
  7. #include <pwd.h> // getpwuid
  8. #include <stdio.h>
  9. #include <string.h> // strerror
  10. #include <unistd.h> // geteuid
  11. const char *argv0 = "whoami";
  12. int
  13. main(void)
  14. {
  15. uid_t euid = geteuid();
  16. struct passwd *pw = getpwuid(euid);
  17. if(pw == NULL)
  18. {
  19. const char *errstr = strerror(errno);
  20. if(errno == 0) errstr = "Not Found";
  21. utils_errx(1, "Failed getting name for user ID %d: %s", euid, errstr);
  22. }
  23. puts(pw->pw_name);
  24. return 0;
  25. }