isatty

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
posted @ 2019-03-20 10:33  wangchangruei  阅读(263)  评论(0编辑  收藏  举报