[net.sources] Testing a port from Franz Lisp

ccc (12/03/82)

As part of a project in Franz Lisp (opus 36), I recently needed a
function that would tell me if there were any characters waiting to be
read from an arbitrary port.  The "tyipeek" function won't work,
because if there is nothing there but we aren't at the end of file (as
in a pipe, for instance), it will wait for a character to become
available, precisely what I was trying to avoid.  With the help of Greg
Ordy (our system administrator), who allowed me to look through some
of the source files to Franz,  I came up with the following solution,
and am posting it in the hope that it will be useful to someone else in
similar circumstances.  There are two parts; the first is a function
written in C that does the appropriate poking around and ioctl calls,
and the second is a Lisp function to interface to it.


				Clayton Elwell
				{usenet}!decvax!cwruecmp!ccc

-----------------------------------------------------------------------
                       emptyp.c follows:
-----------------------------------------------------------------------
#include <stdio.h>
#include <sys/ioctl.h>

emptyp(fptr)
FILE ***fptr;
{
	int count;

	ioctl((**fptr)->_file,FIONREAD,&count);
	if ((count > 0) || ((**fptr)->_cnt > 0))
		{ return(0); }
	else	{ return(1); };
}
-----------------------------------------------------------------------
                       emptyp.l follows:
-----------------------------------------------------------------------
(let (($ldprint nil))
  (cfasl 'empty.o '_emptyp 'cemptyp "integer-function"))
;
;  (cemptyp takes a pointer to a port and returns 1 or 0.)
;

;    emptyp takes a port and returns t if the port is empty and nil if
;    the port has something in it:

(defun emptyp (port)
  (eq (emptyp (maknum port)) 1))
-----------------------------------------------------------------------