总结
对于许多用例,POSIX 函数 isatty() 是检测标准输入是否连接到终端所需的全部内容。一个最小的例子:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (isatty(fileno(stdin)))
puts("stdin is connected to a terminal");
else
puts("stdin is NOT connected to a terminal");
return 0;
}
以下部分比较了在必须测试不同程度的交互性时可以使用的不同方法。
方法详解
有几种方法可以检测程序是否以交互方式运行。
下表显示了一个概览:
cmd\method ctermid open isatty fstat
―――――――――――――――――――――――――――――――――――――――――――――――― ――――――――――
./test /dev/tty OK 是 S_ISCHR
./test ≺ test.cc /dev/tty OK NO S_ISREG
猫测试.cc | ./test /dev/tty OK NO S_ISFIFO
回声./测试 |现在 /dev/tty FAIL NO S_ISREG
结果来自使用以下程序的 Ubuntu Linux 11.04 系统:
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main() {
char tty[L_ctermid+1] = {0};
ctermid(tty);
cout << "ID: " << tty << '\n';
int fd = ::open(tty, O_RDONLY);
if (fd < 0) perror("Could not open terminal");
else {
cout << "Opened terminal\n";
struct termios term;
int r = tcgetattr(fd, &term);
if (r < 0) perror("Could not get attributes");
else cout << "Got attributes\n";
}
if (isatty(fileno(stdin))) cout << "Is a terminal\n";
else cout << "Is not a terminal\n";
struct stat stats;
int r = fstat(fileno(stdin), &stats);
if (r < 0) perror("fstat failed");
else {
if (S_ISCHR(stats.st_mode)) cout << "S_ISCHR\n";
else if (S_ISFIFO(stats.st_mode)) cout << "S_ISFIFO\n";
else if (S_ISREG(stats.st_mode)) cout << "S_ISREG\n";
else cout << "unknown stat mode\n";
}
return 0;
}
终端设备
如果交互式会话需要某些功能,您可以打开
终端设备和(临时)设置您需要的终端属性
通过tcsetattr()。
Python 示例
Python code that decides whether the interpreter runs interactively 使用 isatty()。函数PyRun_AnyFileExFlags()
/* Parse input from a file and execute it */
int
PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
PyCompilerFlags *flags)
{
if (filename == NULL)
filename = "???";
if (Py_FdIsInteractive(fp, filename)) {
int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
致电Py_FdIsInteractive()
/*
* The file descriptor fd is considered ``interactive'' if either
* a) isatty(fd) is TRUE, or
* b) the -i flag was given, and the filename associated with
* the descriptor is NULL or "<stdin>" or "???".
*/
int
Py_FdIsInteractive(FILE *fp, const char *filename)
{
if (isatty((int)fileno(fp)))
return 1;
调用isatty()。
结论
有不同程度的交互性。用于检查 stdin 是否为
连接到管道/文件或真实终端isatty() 是一种自然的方法
这样做。