isatty() 原型如下:
[C++] 纯文本查看 复制代码 #include <unistd.h>
int isatty(int fd);
该函数判断一个打开了的文件描述符是否指向一个终端。
测试代码:
[C++] 纯文本查看 复制代码 #include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void isatty_printer(const int fd)
{
if (isatty(fd))
printf ("Yes, it is a tty\n");
else
printf ("No, it is not a tty\n");
}
int main()
{
int fd = 100;
isatty_printer(fd);
fd = STDIN_FILENO;
isatty_printer(fd);
fd = open("/dev/tty", O_RDONLY);
isatty_printer(fd);
return 0;
}
运行输出:./isatty
No, it is not a tty
Yes, it is a tty
Yes, it is a tty |