tty.c 733 B

1234567891011121314151617181920212223242526
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. */
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
  10. #include "../common/common.h"
  11. int main(int argc, char *argv[]) {
  12. char *terminalname = ttyname(STDIN_FILENO);
  13. if (argc != 1) {} /* workaround... */
  14. if (errno == ENOTTY) {
  15. /* POSIX says that if the stdin isn't a tty then the error shall be
  16. * written to stdout, not stderr.
  17. */
  18. printf("not a tty\n");
  19. return 1;
  20. } else if (errno) return errprint(argv[0], NULL, errno);
  21. printf("%s\n", terminalname);
  22. return errprint(argv[0], NULL, errno);
  23. }