🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.chess
Article #4314

gnuchess-4.0.pl74 and ioctl's FIONREAD

#4314
From: Claus Riemann
Date: Mon, 26 Jun 1995 00:00
339 lines
10317 bytes
Hi,

I just wonder how many people doesn't use gnuchess, coz' they see
"FIONREAD: Operation not applicable
 You probably have a non-ANSI <ioctl.h>; see README ...."
when trying to create 'gnuchess.data'.

So I patched the ioctl-call for those machines. I use
the system call 'select' to determine if there is any input....
Use option '-DFIONREAD_BROKEN' to switch on my changes.

If my patch is useful to others I would like to see it in a
future version of gnuchess. (Whom should I contact?)

BTW: When compiling the code I received many warnings from my compiler
dealing with unused variables (Why does they exists?), unreachable 
statements (Hm, do I trust such code?) and things like
"unsigned short <variable> = -1" (I NEVER trust code like this.)
Any comments?

--cla

P.S.: Here comes the patch:
======SNIP--SNIP--SNIP--SNIP--SNIP--SNIP--SNIP--SNIP--======
diff -c src/Makefile ../../gnuchess-4.0.pl74/src/Makefile
*** src/Makefile	Wed Feb 22 01:48:26 1995
--- ../../gnuchess-4.0.pl74/src/Makefile	Mon Jun 26 09:21:09 1995
***************
*** 117,122 ****
--- 117,123 ----
  # -DLONG64 if you have 64bit longs
  # -DSYSV   if you are using SYSV
  # -DE4OPENING always open e4 if white and respond to e4 with e5 if black
+ # -DFIONREAD_BROKEN if you got 'FIONREAD: Operation not applicable' when creating 'gnuchess.data'
  #
  #    Some debug options
  # -DDEBUG8 dump board,movelist,input move to /tmp/DEBUG if illegal move
***************
*** 189,195 ****
  #INSTALL=install -s
  
  # The compiler used for compiling this software.
! # Use this for a plain C compiler 
  #CC= cc $(OPT)
  # Use this for DEC's ANSI C compiler on Ultrix
  #CC= c89 $(OPT)
--- 190,196 ----
  #INSTALL=install -s
  
  # The compiler used for compiling this software.
! # Use this for a plain C compiler and use this on INRIX 5.3 + IRIX 6.0.1
  #CC= cc $(OPT)
  # Use this for DEC's ANSI C compiler on Ultrix
  #CC= c89 $(OPT)
***************
*** 210,215 ****
--- 211,217 ----
  #CFLAGS= -O2 -D__alpha -D__LANGUAGE_C # gnu cc 2.00 on Flamingo
  #CFLAGS= -O2 -acpp -signed -Wp,-traditional # IRIX 5.1.*?
  #CFLAGS= -g -cckr -DNOMACROS -Dhuge=""  #Correct flags for gnuchess 4.0 with IRIX cc.
+ #CFLAGS= -O2 -signed #IRIX 5.3 + IRIX 6.0.1 with IRIX cc. (Dont' forget to use -DFIONREAD_BROKEN!!)
  
  all : gnuchess gnuchessr gnuchessn gnuchessx gnuchessc postprint gnuan game gnuchess.eco
  
diff -c src/dspcom.c ../../gnuchess-4.0.pl74/src/dspcom.c
*** src/dspcom.c	Sat Feb 11 05:04:13 1995
--- ../../gnuchess-4.0.pl74/src/dspcom.c	Fri Jun 23 18:00:34 1995
***************
*** 1430,1449 ****
  {
  struct timeval tv;
  #ifndef MSDOS
-   int nchar;
    extern int errno;
    int i;
  #ifdef FIONREAD
    if (i = ioctl ((int) 0, FIONREAD, &nchar))
      {
        perror ("FIONREAD");
        fprintf (stderr,
!         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  	i, errno, FIONREAD);
        exit (1);
      }
  
    if (nchar)
      {
        if (!flag.timeout)
  	flag.back = true;
--- 1430,1463 ----
  {
  struct timeval tv;
  #ifndef MSDOS
    extern int errno;
    int i;
  #ifdef FIONREAD
+ #ifdef FIONREAD_BROKEN
+   /* On some machines the 'ioctl((int) 0, FIONREAD, &nchar))' is broken! --cla */
+   extern fd_set readfds;
+   extern struct timeval to;
+   if ((i = select(2, &readfds, NULL, NULL, &to)) == -1){
+       perror ("select");
+       fprintf (stderr,
+         "I tried to use 'select' rather than 'ioctl' and got errno=%d!\n",
+         errno);
+       exit (1);
+     }
+   if ((i != 0) && FD_ISSET(1, &readfds))
+ #else
+   int nchar;
    if (i = ioctl ((int) 0, FIONREAD, &nchar))
      {
        perror ("FIONREAD");
        fprintf (stderr,
!         "You probably have a non-ANSI <ioctl.h>; see README or try to compile with option '-DFIONREAD_BROKEN'. %d %d %x\n",
  	i, errno, FIONREAD);
        exit (1);
      }
  
    if (nchar)
+ #endif /*FIONREAD_BROKEN*/
      {
        if (!flag.timeout)
  	flag.back = true;
***************
*** 1496,1515 ****
  
  {
  #ifndef MSDOS
-   int nchar;
    extern int errno;
    int i;
  #ifdef FIONREAD
    if (i = ioctl ((int) 0, FIONREAD, &nchar))
      {
        perror ("FIONREAD");
        fprintf (stderr,
!         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  	i, errno, FIONREAD);
        exit (1);
      }
  
    if (nchar)
      {
        if (!flag.timeout)
  	flag.back = true;
--- 1510,1543 ----
  
  {
  #ifndef MSDOS
    extern int errno;
    int i;
  #ifdef FIONREAD
+ #ifdef FIONREAD_BROKEN
+   /* On some machines the 'ioctl((int) 0, FIONREAD, &nchar))' is broken! --cla */
+   extern fd_set readfds;
+   extern struct timeval to;
+   if ((i = select(2, &readfds, NULL, NULL, &to)) == -1){
+       perror ("select");
+       fprintf (stderr,
+         "I tried to use 'select' rather than 'ioctl' and got errno=%d!\n",
+         errno);
+       exit (1);
+     }
+   if ((i != 0) && FD_ISSET(1, &readfds))
+ #else
+   int nchar;
    if (i = ioctl ((int) 0, FIONREAD, &nchar))
      {
        perror ("FIONREAD");
        fprintf (stderr,
!         "You probably have a non-ANSI <ioctl.h>; see README or try to compile with option '-DFIONREAD_BROKEN'. %d %d %x\n",
  	i, errno, FIONREAD);
        exit (1);
      }
  
    if (nchar)
+ #endif /*FIONREAD_BROKEN*/
      {
        if (!flag.timeout)
  	flag.back = true;
diff -c src/gnuan.c ../../gnuchess-4.0.pl74/src/gnuan.c
*** src/gnuan.c	Mon Sep 19 12:23:06 1994
--- ../../gnuchess-4.0.pl74/src/gnuan.c	Fri Jun 23 18:16:38 1995
***************
*** 623,642 ****
      struct timeval tv;
  
  #ifndef MSDOS
-     int nchar;
      extern int errno;
      int i;
- 
  #ifdef FIONREAD
      if (i = ioctl ((int) 0, FIONREAD, &nchar))
        {
  	  perror ("FIONREAD");
  	  fprintf (stderr,
! 	   "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  		   i, errno, FIONREAD);
  	  exit (1);
        }
      if (nchar)
        {
  	  if (!flag.timeout)
  	      flag.back = true;
--- 623,655 ----
      struct timeval tv;
  
  #ifndef MSDOS
      extern int errno;
      int i;
  #ifdef FIONREAD
+ #ifdef FIONREAD_BROKEN
+  /* On some machines the 'ioctl((int) 0, FIONREAD, &nchar))' is broken! --cla */
+   extern fd_set readfds;
+   extern struct timeval to;
+   if ((i = select(2, &readfds, NULL, NULL, &to)) == -1){
+       perror ("select");
+       fprintf (stderr,
+         "I tried to use 'select' rather than 'ioctl' and got errno=%d!\n",
+         errno);
+       exit (1);
+     }
+   if ((i != 0) && FD_ISSET(1, &readfds))
+ #else
+   int nchar;
      if (i = ioctl ((int) 0, FIONREAD, &nchar))
        {
  	  perror ("FIONREAD");
  	  fprintf (stderr,
! 		   "You probably have a non-ANSI <ioctl.h>; see README or try to compile with option '-DFIONREAD_BROKEN'. %d %d %x\n",
  		   i, errno, FIONREAD);
  	  exit (1);
        }
      if (nchar)
+ #endif /*FIONREAD_BROKEN*/
        {
  	  if (!flag.timeout)
  	      flag.back = true;
***************
*** 680,699 ****
  
  {
  #ifndef MSDOS
-     int nchar;
      extern int errno;
      int i;
  
  #ifdef FIONREAD
      if (i = ioctl ((int) 0, FIONREAD, &nchar))
        {
  	  perror ("FIONREAD");
  	  fprintf (stderr,
! 	   "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  		   i, errno, FIONREAD);
  	  exit (1);
        }
      if (nchar)
        {
  	  if (!flag.timeout)
  	      flag.back = true;
--- 693,726 ----
  
  {
  #ifndef MSDOS
      extern int errno;
      int i;
  
  #ifdef FIONREAD
+ #ifdef FIONREAD_BROKEN
+  /* On some machines the 'ioctl((int) 0, FIONREAD, &nchar))' is broken! --cla */
+   extern fd_set readfds;
+   extern struct timeval to;
+   if ((i = select(2, &readfds, NULL, NULL, &to)) == -1){
+       perror ("select");
+       fprintf (stderr,
+         "I tried to use 'select' rather than 'ioctl' and got errno=%d!\n",
+         errno);
+       exit (1);
+     }
+   if ((i != 0) && FD_ISSET(1, &readfds))
+ #else
+   int nchar;
      if (i = ioctl ((int) 0, FIONREAD, &nchar))
        {
  	  perror ("FIONREAD");
  	  fprintf (stderr,
! 	   "You probably have a non-ANSI <ioctl.h>; see README or try to compile with option '-DFIONREAD_BROKEN'. %d %d %x\n",
  		   i, errno, FIONREAD);
  	  exit (1);
        }
      if (nchar)
+ #endif /*FIONREAD_BROKEN*/
        {
  	  if (!flag.timeout)
  	      flag.back = true;
diff -c src/main.c ../../gnuchess-4.0.pl74/src/main.c
*** src/main.c	Sun Feb 12 03:13:19 1995
--- ../../gnuchess-4.0.pl74/src/main.c	Fri Jun 23 18:14:54 1995
***************
*** 107,112 ****
--- 107,119 ----
  SHORT ahead = true, hash = true;
  SHORT PCRASH, PCENTER;
  
+ #ifdef FIONREAD_BROKEN
+ #include <sys/time.h>
+ fd_set readfds;    /* for avoiding the bug with ioctl and */
+ struct timeval to; /* FIONREAD in dspcom.c and gnuan.c */
+ #endif
+ 
+ 
  #if defined CHESSTOOL || defined XBOARD
  void
  TerminateChess (int sig)
***************
*** 394,399 ****
--- 401,412 ----
      }
  #ifdef Think_C	/* Allocate memory for "huge" static arrays */
    Initialize_mem();
+ #endif
+ #ifdef FIONREAD_BROKEN
+   FD_ZERO(&readfds);   /* for avoiding the bug with ioctl and */
+   FD_SET(0, &readfds); /* FIONREAD in dspcom.c and gnuan.c */
+   to.tv_usec = 0;
+   to.tv_sec = 0;
  #endif
    Initialize ();
    Initialize_dist ();

======SNIP--SNIP--SNIP--SNIP--SNIP--SNIP--SNIP--SNIP--======

**********************************************************
German National Research Center for Information Technology 
Claus Riemann (VMSD)             Phone: (+49 2241) 14-2865
Schloss Birlinghoven             Fax:   (+49 2241) 14-2040
53754 Sankt Augustin              Don't dream it - Be it!
Technical Details
Message-ID: <3sloe8$bek@omega.gmd.de>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d71d,f14cce6030e09634,start
X-Google-Attributes: gid10d71d,public
From: Claus.Riemann@gmd.de (Claus Riemann)
Subject: gnuchess-4.0.pl74 and ioctl's FIONREAD
Date: 1995/06/26
Message-ID: <3sloe8$bek@omega.gmd.de>
X-Deja-AN: 105056740
organization: GMD, Sankt Augustin, Germany
newsgroups: gnu.chess