[comp.os.minix] Bug in portio.c + fix

ajm@cit5.cit.oz (Tony Mcgregor) (01/15/90)

There is a bug in the 1.5.0 lib/other/portio.c that means port reads and 
writes can't be mixed within a single program.

This caused a problem with readclock.

A fix follows.


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	portio.c
#	portio.crc
# This archive created: Mon Jan 15 10:43:38 1990
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'portio.c'
then
	echo shar: "will not over-write existing file 'portio.c'"
else
cat << \SHAR_EOF > 'portio.c'
/* Port i/o functions using /dev/port.
 * Callers now ought to check the return values.
 * Calling either of these functions consumes a file descriptor.
 */

#include <lib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

#define P_READ 1
#define P_WRITE 2

PRIVATE int portfd = -1;
PRIVATE int mode_opened = 0;

int port_in(port, valuep)
unsigned port;
unsigned *valuep;
{
  unsigned char chvalue;

  if ( !( mode_opened & P_READ )) {
     if ( mode_opened & P_WRITE ) {
        close(portfd);
        portfd = open("/dev/port", O_RDWR);
      }
      else {
        portfd = open("/dev/port", O_RDONLY);
      }
      mode_opened |= P_READ ;
    }

  if (portfd < 0 ||
      lseek(portfd, (long) port, 0) < 0 ||
      read(portfd, (char *) &chvalue, (size_t) 1) != 1)
	return(*valuep = -1);
  return(*valuep = chvalue);
}

int port_out(port, value)
unsigned port;
unsigned value;
{
  unsigned char chvalue;

  chvalue = value;
  if ( !( mode_opened & P_WRITE )) {
     if ( mode_opened & P_READ ) {
        close(portfd);
        portfd = open("/dev/port", O_RDWR);
      }
      else {
        portfd = open("/dev/port", O_WRONLY);
      }
      mode_opened |= P_WRITE ;
    }

  if (portfd < 0 || lseek(portfd, (long) port, 0) < 0 ||
      write(portfd, (char *) &chvalue, (size_t) 1) != 1)
	return(-1);
  return(chvalue);
}
SHAR_EOF
fi
if test -f 'portio.crc'
then
	echo shar: "will not over-write existing file 'portio.crc'"
else
cat << \SHAR_EOF > 'portio.crc'

CRCS:
     61685	 1345    1.5.0_portio.c
     20209   1350    fixed_portio.c 



SHAR_EOF
fi
exit 0
#	End of shell archive

PS Please forgive me if I stuffed-up; this is my first posting.
----------------------------------------------------------------------------
Tony McGregor 
Department of Robotics and Digital Tech., Chisholm Institute of Technology
PO Box 197, Caulfield East, Vic 3145, Australia  
Phone: +61 3 5732014    Fax: +61 3 5732748    ACSNET: ajm@cit5.cit.oz
----------------------------------------------------------------------------