OS XとLinux用のCプログラムを作成していますが、端末に接続するかどうかで出力を調整したいと思います。シェルスクリプトでこれを行う方法については、私が知っているところです。ここに:端末に出力する場合はCで検出する
Detecting the output stream type of a shell script
しかし、どのように私はCプログラムでそれを行うのですか?
OS XとLinux用のCプログラムを作成していますが、端末に接続するかどうかで出力を調整したいと思います。シェルスクリプトでこれを行う方法については、私が知っているところです。ここに:端末に出力する場合はCで検出する
Detecting the output stream type of a shell script
しかし、どのように私はCプログラムでそれを行うのですか?
使用isatty()
:stdout
以来
$ man isatty
ISATTY(3) Linux Programmer's Manual ISATTY(3)
NAME
isatty - does this descriptor refer to a terminal
SYNOPSIS
#include <unistd.h>
int isatty(int desc);
DESCRIPTION
returns 1 if desc is an open file descriptor connected to a terminal
and 0 otherwise.
は、記述子1をファイル、あなたが行うことができ、常に次のとおりです。
if(isatty(1))
// stdout is a terminal
if (isatty (1))
fprintf (stdout, "Outputting to a terminal.");
else
fprintf (stdout, "Not outputting to a terminal.");
そして、あなたは少し深く説明できますか? この機能で実際に何が確認されていますか? 実際には、端末に接続されていることを意味しますか? – xolodec
@PavelShvechikovこれは、ファイル記述子が端末と見なされるデバイスに関連付けられていることを意味します。 http://en.wikipedia.org/wiki/POSIX_terminal_interfaceを参照してください。 –