2009-06-30 14 views

risposta

36

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

Dal stdout è sempre descrittore di file 1, si può fare:

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

E potrebbe spiegare un po 'più profondo? Cosa viene veramente controllato da questa funzione? Che cosa significa "connesso ad un terminale"? – xolodec

+0

@PavelShvechikov Significa che il descrittore del file è associato a un dispositivo considerato terminale. Vedi 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.");