C Input Question?

Sjoerd Mullender sjoerd at tjalk.UUCP
Thu Mar 21 23:00:48 AEST 1985


In article <522 at lsuc.UUCP> dave at lsuc.UUCP (David Sherman) writes:
>In article <247 at faron.UUCP> meister at faron.UUCP (Philip W. Servita) writes:
>||>[text cut for brevity. Scott wants non-blocking I/O, that is "read if
>||>data, but don't wait for data.]
>||
>||   ioctl(0,FIONREAD,&charwaiting);
>
>That's fine for BSD (which was the system the questioner wanted it for).
>Anyone have an easy way of doing this for v7? I'm willing to do a
>limited amount of kernel hacking.

Here is a C routine that returns non-zero if there was any input pending.
This is for a V7 PDP 11 system.  The only requirement is that /dev/kmem
is readable (which it should NOT be for security).

#include <sys/param.h>
#include <sys/dir.h>
#include <sys/user.h>

inputpending()
{
	static int fd = -2;
	unsigned c;

	if (fd == -2)	/* uninitialized */
		if ((fd = open("/dev/kmem", 0)) >= 0) {
			/* If the open fails, fd will be -1, so we won't
			 * try again.
			 * Close the file descriptor on exec.
			 */
			ioctl(fd, FIOCLEX, (struct sgttyb *) 0);
			/* 0140000 is the beginning of the u area
			 * u_ttyp is the pointer to the tty struct
			 */
			lseek(fd, (long) &((struct user *) 0140000)->u_ttyp, 0);
			read(fd, &c, sizeof c);
			/* seek to the beginning of the tty struct */
			lseek(fd, (long) c, 0);
		}
	if (fd < 0)
		return 0;
	/* the first 2 byte of the tty struct is a pointer to the clist */
	if (read(fd, &c, sizeof c) < sizeof c)
		return 0;
	lseek(fd, - (long) sizeof c, 1);
	/* there is input when the pointer to the clist is != 0 */
	return c != 0;
}
-- 
			Sjoerd Mullender
			...!{decvax,philabs,seismo}!mcvax!vu44!sjoerd



More information about the Comp.lang.c mailing list