/dev/tty use question...

Conor P. Cahill cpcahil at virtech.uucp
Fri Jul 6 09:37:51 AEST 1990


In article <23800 at adm.BRL.MIL> swenson at nusc-wpn.arpa writes:
>	I am currently writing some code to multiplex some tty lines.  Question,
>how does one go about using /dev/tty's?  It seems that the man pages for tty,
>termio and ioctl are not very clear (at least to me).  Specifically, I want to
>open a tty (pseudo or otherwise) take input from a parent program and feed 
>the input through the tty, take output from the tty and send it to my parent
>(very much like tip).

This is one of the parts of UNIX that is very system dependent. Since you
didn't specify what system you are running on I will assume it is system V
based (because I feel like it).

Anyway, reguardless of which system you are on you need to do the following:

	1. open the port
	2. set up the termio structure as appropriate*
	3. use ioctl to set the port
	4. read and/or write to the port as appropriate**
	5. send and/or get data from the parent process
	6. when all done, close the port

* Sample termio structure settings for a system V system:


	if( (ttyfd=open(argv[1],O_RDWR)) == -1)
	{
		fatal("Unable to open tty device");
	}

	t.c_iflag =	  IGNBRK 		/* ignore breaks 	*/
			| IXON			/* xon/xoff output ctrl */
			| IXOFF;		/* xon/xoff input ctrl  */
	t.c_lflag = 0;				/* disable line discipline */
	t.c_oflag = 	  OPOST			/* enable post processing*/
			| ONLCR;		/* map nl to cr-nl	*/
	t.c_cflag =       CLOCAL		/* no modem control	*/
			| CS8			/* 8 data bits		*/
			| B9600			/* speed: 9600		*/
			| CREAD;		/* enable reciever	*/

	if( ioctl(ttyfd,TCSETA,&t) == -1 )	/* if set fails		*/
	{
		fatal("Unable to set up tty port characteristics");
	}


** To read and write to a port at the same time on system V, the best 
thing to do is fork a child process to do one of the jobs and you can 
do the other.  If you read from the parent and write to the port you can
choose the item from the parent that means that you need to die and kill
the child before exiting.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.unix.wizards mailing list