/* You  compile  with -O or -O2 or similar. The functions are
   defined as inline macros, and will not be  substituted  in
   without  optimization  enabled,  causing unresolved refer­
   ences at link time.
*/

#include <stdlib.h>
#include <stdio.h>
#include <sys/io.h>
#include <unistd.h>

#define DEBUG 1

/* Base addresses of parallel ports */
#define LPT1 0x378
#define LPT2 0x278
#define LPT3 0x3BC

void usage(void)
{
  printf("Syntax : getport <portnum>\n\n");
  printf("portnum: 1, 2 or 3 for LPT1, LPT2 or LPT3.\n");
  printf("returns: (inb(STATUSPORT) ^ 0x80) >> 3)\n");
  printf("         or 255 if an error occurs\n\n");
  return(255);
}

int main(int argc, char *argv[])
  {
  int status = 0;
  int BASEPORT = LPT1;
  if (argc != 2) usage();      /* falsche Parameteranzahl */
  switch (argv[1][0])
    {
    case '1': BASEPORT = LPT1; break;
    case '2': BASEPORT = LPT2; break;
    case '3': BASEPORT = LPT3; break;
    default: usage();
    }
  /* Get access to the ports */
  if (ioperm(BASEPORT, 3, 1))
    { perror("Error: cannot access ioport"); exit(255); }
  /* Get status port */
  status = ((inb(BASEPORT + 1) ^ 0x80) >> 3);
#ifdef DEBUG
   printf("Debug: Reading from PORT %X.\n", BASEPORT);
   printf("Debug: Port shows 0x%X.\n", status);
#endif
  /* We don't need the ports anymore */
  if (ioperm(BASEPORT, 3, 0))
    { perror("Error: cannot access ioport"); exit(255); }
  exit(status);
  }

