printer connections on Suns

Guy Harris auspex!guy at uunet.uu.net
Thu Mar 2 07:04:06 AEST 1989


>In my HP filter, I wait for the output to flush with this code:
>...
>	while(ioctl(fd, TIOCOUTQ, &outchars) == 0 && outchars > 0)

Wow.  That probably works (although I don't know that TIOCOUTQ will always
give the right answer), but under 3.x or 4.0 (or any V7/BSD-style tty
driver) the following should work better:

	struct sgtty sgttyb;

	(void) ioctl(fd, TIOCGETP, &sgttyb);
	(void) ioctl(fd, TIOCSETP, &sgttyb);

since, as it says in the 4.3-tahoe TTY(4):

	TIOCSETP
	     Set the parameters according to the pointed-to "sgttyb"
	     structure.  The interface delays until output is quiescent,
	     then throws away any unread characters, before changing the
	     modes.

Since you do a TIOCGETP followed immediately by a TIOCSETP, the modes
aren't changed; the only effects of the TIOCSETP are 1) it waits for
output to drain and 2) it flushes all input.  The latter is an unfortunate
side-effect, but it may not be a problem here.

I think all of the variants mentioned above, except 4.0, say much the same
thing.  4.0's TTCOMPAT(4M) basically says "TIOCSETP gets turned into a
TCSETSF", and 4.0's TERMIO(4) says about TCSETSF:

     TCSETSF        The argument is a pointer to a termios struc-
                    ture.   The  current  terminal parameters are
                    set from the values stored in that structure.
                    The change occurs after all characters queued
                    for output have been transmitted; all charac-
                    ters  queued for input are discarded and then
                    the change occurs.

which is basically the same set of side-effects as TIOCSETP.

In 4.0, though, and other systems that offer an S5-compatible tty driver,
there is an even better way; again from TERMIO(4):

     TCSBRK         The argument is an int value.  Wait  for  the
                    output  to drain.  If the argument is 0, then
                    send  a  break  (zero-valued  bits  for  0.25
                    seconds).

If the argument is *not* zero, no break is sent, so

	ioctl(fd, TCSBRK, 666);	/* insert your number here - beastly, eh? */

will just wait for output to drain.



More information about the Comp.sys.sun mailing list