isatty.c 638 B

1234567891011121314151617181920212223242526
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include "test_helpers.h"
  6. int main(void) {
  7. int status = isatty(STDOUT_FILENO);
  8. if (status == 1) {
  9. puts("'Tis a tty :D");
  10. } else if (status == 0) {
  11. if (errno == ENOTTY) {
  12. // I wouldn't consider stdout not being a TTY an error
  13. // (CI runners, etc.)
  14. puts("Whatever a tty is, it's not me");
  15. } else {
  16. perror("isatty");
  17. exit(EXIT_FAILURE);
  18. }
  19. } else {
  20. printf("isatty returned %d, unexpected result\n", status);
  21. exit(EXIT_FAILURE);
  22. }
  23. }