logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git

tty.c (569B)


  1. // Collection of Unix tools, comparable to coreutils
  2. // SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
  4. #include <errno.h> // errno
  5. #include <stdio.h> // puts()
  6. #include <unistd.h> // ttyname()
  7. int
  8. main(void)
  9. {
  10. char *name = ttyname(STDIN_FILENO);
  11. if(!name)
  12. {
  13. if(errno == ENOTTY)
  14. {
  15. if(puts("not a tty") < 0)
  16. {
  17. return 2;
  18. }
  19. return 1;
  20. }
  21. else
  22. {
  23. perror("ttyname");
  24. return 2;
  25. }
  26. }
  27. if(puts(name) < 0)
  28. {
  29. return 2;
  30. }
  31. return 0;
  32. }