12

Hi can anyone tell me what is the paramter of isatty() in c. I have following code, but I don't understand the first output three number would be 1 and all the left is 0.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
        for(int i=0;i<100;i++){
                int t=isatty(i);
                printf("%d",t);
        }
        return 0;
}
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Hao9000
  • 149
  • 1
  • 1
  • 9

7 Answers7

16

A quick look at your man pages would reveal:

int isatty(int fildes);

DESCRIPTION
     The isatty() function tests whether  fildes,  an  open  file
     descriptor, is associated with a terminal device.

Further investigation would lead you to the discovery that file descriptors 0, 1 and 2 (aka STDIN_FILENO, STDOUT_FILENO and STDERR_FILENO) are by convention set up to point to your terminal when your program is running from a terminal.

Nickolay
  • 31,095
  • 13
  • 107
  • 185
Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
  • Thank you! Can you tell me how to find where to find which file descriptor each int is referring to? – Hao9000 Mar 28 '16 at 08:02
  • there isn't a standard way. – Tom Tanner Mar 28 '16 at 08:25
  • @Hao9000 If you want the filename referred to by the file descriptor, on Linux you may use `readlink`, see [this](https://stackoverflow.com/questions/1188757/getting-filename-from-file-descriptor-in-c) – Bilow Nov 05 '18 at 14:00
  • This answer doesn't give much more info than what can be found in the man page. However, I'm not quite sure how this would behave in different circumstances. For example, let's say I run a program from my terminal (tty), but redirect its standard output to a file. What would the program get if it ran `isatty(STDOUT_FILENO)`? Can we use this function to test whether the program is being run in a terminal? If so, how? In other words, some _practical_ examples for when this would be used would be nice. – domsson Nov 20 '20 at 20:19
  • 1
    the man page is probably the best answer. If your output is redirected to a file, STDOUT_FILENO refers to a file. Which is not a terminal. – Tom Tanner Feb 25 '21 at 13:10
  • Good enough Tom Tanner. Thanks! – coterobarros Aug 06 '22 at 16:31
  • I made a POC of the isatty() function to differentiate when a C utility is launched by cron or by a human user. It works even when the std output is redirected to a file. https://stackoverflow.com/a/73261754/8520235 – coterobarros Aug 06 '22 at 17:15
5

isatty() is a function that returns 1 if the fd - (file descriptor) refers to a terminal.

It comes under the #include

#include <unistd.h>
kralyk
  • 4,249
  • 1
  • 32
  • 34
Sumukh Bhandarkar
  • 386
  • 1
  • 5
  • 14
  • 3
    in what way does it not answer the question (in so far as its not significantly worse than some of the other answers here) – Tom Tanner Mar 28 '16 at 11:01
1

It tells whether the file descriptor is connected to a terminal or not.

You can read more about it here: http://linux.die.net/man/3/isatty

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
uSeemSurprised
  • 1,826
  • 2
  • 15
  • 18
1

Check the ref:

isatty - test whether a file descriptor refers to a terminal

gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

"But what is meaning of the parameter taken by isatty()?"

The parameter is an index into the standard I/O library's table of file descriptors. Indexes 0, 1 and 2 are reserved for stdin, stdout and stderr. All other indexes refer to file descriptors that can/have been opened by you.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

Isatty() is used for checking a fd (file descriptor) belongs to a terminal or command line prompt. This function gives you binary value 1 when it belongs to a terminal It uses in codes which contains unistd.h keywords in header file isatty() gives you 1 if fd is an open file descriptor referring to a terminal; or else 0 is returned

0

In case you are still interested, I came across the MSDOS implementation from a few years ago.

/*
** Return "true" if fd is a device, else "false"
*/
isatty(fd) int fd; {
  fd;               /* fetch handle */
  #asm
    push bx         ; save 2nd reg
    mov  bx,ax      ; place handle
    mov  ax,4400h   ; ioctl get info function
    int 21h         ; call BDOS
    pop  bx         ; restore 2nd reg
    mov  ax,dx      ; fetch info bits
    and  ax,80h     ; isdev bit
  #endasm
 }