/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* a sample program that checks if the file is a
terminal device. */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
    int fildes;
    char *ttynam;

    ttynam = ttyname(0); /* 0 is for stdin, 1 for stdout, 2 for stderr */
    printf("ttyname(0) = %s\n", ttynam);

    /* open file for reading only */
    if((fildes = open(ttynam,O_RDONLY)) == -1){ 
      printf("cannot open file\n");
      exit(1);
    }

    if(isatty(fildes))
      printf("terminal's name is %s\n",ttyname(fildes));
    else
      printf("%d is not associated with a terminal device\n");
    if(close(fildes)) printf("error in closing file\n");
    return (0);
}

