2009-06-30 8 views

Odpowiedz

36

Zastosowanie isatty():

$ 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. 

Od stdout jest zawsze deskryptor pliku 1, można to zrobić:

if(isatty(1)) 
    // stdout is a terminal 
+0

I można wyjaśnić trochę głębiej? Co tak naprawdę sprawdza ta funkcja? Co tak naprawdę oznacza "podłączony do terminala"? – xolodec

+0

@PavelShvechikov Oznacza to, że deskryptor pliku jest powiązany z urządzeniem, które jest uważane za terminal. Zobacz http://en.wikipedia.org/wiki/POSIX_terminal_interface –

5
if (isatty (1)) 
    fprintf (stdout, "Outputting to a terminal."); 
else 
    fprintf (stdout, "Not outputting to a terminal.");