All about programming in GNU/LINUX

Illustration of ‘isatty’ system call in GNU/LINUX

/* Hackintosh rao , codes for GNU/LINUX*/
/* int isatty(fd);
   fd is the file descriptor of a open file
   if the terminal is associated with the file whose Fd is given then ,
   isatty returns 1 , else
   it returns 0
   this system call is used to check whether any open file  with a file descriptor
   is connected to a terminal or not */
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
    if(!(isatty(fileno(stdout))))  /*fileno function returns the filedescriptor*/
    {
        write(2,”\n\nstandard o/p not associated with control terminal\n\n”,60);
        /*cannot write into standard output with File descriptor 1 , when the
        terminal is not associated with it , that is when standard o/p is redirected
        using ‘>’ operator when the program is invoked, so the o/p is written into
        standard error with file descriptor 2*/
        exit(0);
    }
    else

    {
        printf(“\no/p is associated with the control terminal\n\n”);
    }

    return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s