logo

utils-std

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

logname.c (628B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: 0BSD
  4. #define _POSIX_C_SOURCE 200809L
  5. #include <stdio.h> // fputs, perror
  6. #include <stdlib.h> // getenv
  7. int
  8. main(void)
  9. {
  10. // Allows to avoid utmp (glibc…), $LOGNAME also being required to match user's login name
  11. char *logname = getenv("LOGNAME");
  12. if(logname == NULL)
  13. {
  14. fputs("logname: error: $LOGNAME isn't set (didn't login?)\n", stderr);
  15. return 1;
  16. }
  17. if(puts(logname) < 0)
  18. {
  19. perror("logname: error: puts");
  20. return 1;
  21. }
  22. return 0;
  23. }