Results 1 to 6 of 6

Thread: How can I implement the isatty() in C language

  1. #1
    Join Date
    Dec 2009
    Posts
    26

    How can I implement the isatty() in C language

    Hi, I am the student of the Programming Field. I had learned the C language. I can also have the knowledge of the functions. But I could not know about the isatty() function of C language. So that I would like to know about use of the isatty() function of C language. I also would like to know how can isatty() function can be implemented in the C program. If you have the answer for me then reply me.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    The isatty() in C language

    To determine that if the file descriptor fields can be associated with a terminal, the isatty() function can be used in the C language. The following can be the general syntax of the isatty() function :
    Code:
    #include <unistd.h>
    int isatty( int fs );

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    How can I implement the isatty() in C language

    The following code of lines can demonstrates you about the working of the isatty() function in the C language :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    void main()
      {
        exit( isatty( 3 ) ? EXIT_SUCCESS : EXIT_FAILURE );
      }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    The isatty() in C language

    PROGRAM :
    Code:
    #if defined(LIBC_SCCS) && !defined(lint)
    static char *rcid = "$Id: isatty.c,v 1.1.1.1 2000/02/10 01:00:58 sasha Exp $";
    #endif 
    #include <pthread.h>
    #ifdef snos4
    #include <sys/termio.h>
    #else
    #include <termios.h>
    #endif
    #include <unistd.h>
    #ifndef TICGETA
    #ifdef TGETATTR
    #define TICGETA    TGETATTR
    #else
    #ifndef TGETA
    #include <termio.h>
    #endif 
    #ifndef TICGETA
    #define TICGETA    TGETA
    #endif
    #endif 
    #endif
    int isatty_basic(int f)
    {
    #ifdef snos4
    	struct termi v;
    #else 
    	struct termo v;
    #endif 
    	return (machdep_sys_ioctl(f,
    #ifdef snos4
    				  TGETA,
    #else 
    				  TICGETA,
    #endif 
    				  &v) ? 0 : 1);
    }
    int isatty(int f)
    {
    	int rt;
    	if ((rt = f_lock(f, FD_READ, NULL)) == OK) 
    {
    		rt = isatty_basic(f_table[f]->f.i);
    		f_unlock(f, FD_READ);
    	} else 
    {
    		rt = 0;
    	}
    	return(rt);
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How can I implement the isatty() in C language

    The following program can states you the working of the isatty() function :
    Code:
    #define _POSIX_SOURCE
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    void checkfd(int d)
     {
      printf("fd %d is ", d);
      if (!isatty(d))
        printf("NOT ");
      puts("a tty");
    }
    main() 
    {
      int q[2], d;
      char n[]="temp.file";
      if (pipe(q) != 0)
        perror("pipe() error");
      else 
    {
        if ((d = creat(n, SIWUSR)) < 0)
          perror("creat() error");
        else 
    {
          checkfd(0);
          checkfd(fileno(stderr));
          checkfd(q[1]);
          checkfd(d);
          close(d);
          unlink(n);
        }
        close(q[0]);
        close(q[1]);
      }
    }
    Output :
    Code:
    fd 0 is a tty
    fd 2 is a tty
    fd 4 is NOT a tty
    fd 5 is NOT a tty

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Implement the isatty()

    The isatty() function can only be works in an environment where either a controlling terminal exists. The isatty() function can not work in a TSO environment. The isatty() function can returns zero if the given file descriptor can not be a terminal. The isatty() function can returns one if the given file descriptor can be a terminal.

Similar Threads

  1. Replies: 8
    Last Post: 19-04-2012, 08:12 PM
  2. Replies: 3
    Last Post: 31-01-2011, 11:57 PM
  3. Replies: 7
    Last Post: 08-12-2010, 02:20 AM
  4. Replies: 5
    Last Post: 25-02-2010, 03:35 PM
  5. Vista ultimate Language Pack for Greek language
    By ph_oratis in forum Windows Update
    Replies: 3
    Last Post: 29-11-2009, 03:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,583,342.29179 seconds with 17 queries