#ifndef	lint
static char sccsid[] = "@(#) helper_idle.c 1.7 89/08/21";
#endif

#include "cbratp.h"
#include <sys/time.h>

extern fd_set	lseek_fds;
extern fd_set	stopped_fds;
extern int	rs232_fd;

static fd_set	outfds;
static int	lastfd;

idle_writer()
{
    u_char buf[QBUFSIZE];
    register int fd;
    register int i;

    if (rs232ready() > 0)
	return 0;

    outfds = read_fds;
    FD_SET(rs232_fd, &outfds);

    if (maximum_fd && select(maximum_fd, &outfds, (fd_set *)NULL,
				(fd_set *)NULL, (struct timeval *)NULL) > 0) {
	for (fd = lastfd; fd < maximum_fd; fd++) {
	    if (FD_ISSET(fd, &outfds) && !FD_ISSET(fd, &lseek_fds) &&
		    !FD_ISSET(fd, &stopped_fds) && fd != rs232_fd) {
		if ((i = read(fd, (char *) buf, QBUFSIZE)) < 0)
		    i = 0;
		enqueue(rd_chan[fd], buf, i);
		break;
	    }
	}
	lastfd = fd < maximum_fd ? fd + 1: 0;
    }

    return(out_queue.size > 0);
}
