isatty - test whether a file descriptor refers to a terminal

#include <stdio.h>
#include <unistd.h>

int isatty(int fd);

int main(int argc, char *argv[]) {
  printf("isatty : %d \n", isatty(STDIN_FILENO));
  return 0;
}
gcc test.c

a.out 的 stdin 來自 terminal,所以 isatty(STDIN_FILENO) 為 1

$ ./a.out
isatty : 1

a.out 的 stdin 是來自 pipe,不是來自 terminal,所以 isatty(STDIN_FILENO) 為 0

$ echo "123" | ./a.out
isatty : 0

相关文章:

猜你喜欢
  • 2022-01-27
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案