/* 
 C 2001 UK  
 test for [ tcl-Bugs-219360 ] open does not notice EIO in check for tty

 running this on Linux System 1 gives

 uwe@hal:~ > uname -a 
 Linux hal 2.4.4-4GB #1 Wed May 16 00:37:55 GMT 2001 i686 unknown
 (SuSE 7.2)
 uwe@hal:~ > ./ttytest /dev/ttyS1
 open "/dev/ttyS1" returns 3 (errno == 0)
 write returns "10" errno is  0 Success
 isatty returns "1" errno is  0 Success
 uwe@hal:~ > ./ttytest /dev/ttyS6
 open "/dev/ttyS6" returns 3  (errno == 0)
 write returns "-1" errno is  5 Input/output error
 isatty returns "0" errno is  5 Input/output error
 uwe@hal:~ > 

 running this on Linux System 2 gives
 uwe@james:~ > uname -a
 Linux james 2.2.14 #2 Don Apr 13 15:47:10 CEST 2000 i586 unknown
 (SuSE 6.3)
 uwe@james:~ > ./ttytest /dev/ttyS1
 open "/dev/ttyS1" returns 3  (errno == 0)
 write returns "10" errno is  0 Sucess
 isatty returns "1" errno is  0 Success
 uwe@james:~ > ./ttytest /dev/ttyS6
 open "/dev/ttyS6" returns 3  (errno == 0)
 write returns "-1" errno is  5 Input/output error
 isatty returns "0" errno is  5 Input/output error
 uwe@james:~ > 
*/
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>


int
main(int argc,char**argv,char**arge)
{
    int fd = -1;
    int ret = 0;
    char *ttyname = "/dev/ttyS99";

    if ( argc > 1 ) 
	ttyname = argv[1];

    fd = open(ttyname,O_RDWR);
    fprintf(stderr,"open \"%s\" returns %d (errno == %d)\n",ttyname,fd,errno);

    ret = write(fd,ttyname,strlen(ttyname));
    fprintf(stderr,"write returns \"%d\" errno is %d ",ret,errno);
    perror("");
    ret = isatty(fd);
    fprintf(stderr,"isatty returns \"%d\" errno is %d ",ret,errno);
    perror("");

    exit;
}
