#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/ppdev.h>

int main()
{
  int fd, status;
  
  if (getuid() != 0)
  {
     printf("Please run this program as root.\n");
     exit (1);
  }
  
  fd = open("/dev/parport0", O_RDONLY | O_NOCTTY);
  if (fd < 0)
  {
     perror("Cannot open /dev/parport0");
     exit(1);
  }
  
  if (fork() == 0)
  {
    if (ioctl(fd, PPCLAIM))
    {
      perror("Child could not claim port");
      exit (1);
    }
    exit(0);
  }
  
  /*
   * Wait for the child to exit. The claimed port should have been
   * released when the child has exited.
   */
  wait(&status);

  if (ioctl(fd, PPCLAIM))
  {
    /* Despite the exit of the child, the port is not released. */
    perror("PARENT COULD NOT CLAIM PORT");
    printf("Kernel bug?\n");
    exit (1);
  }
  
  printf("Kernel seems to be ok.\n");
  exit(0);
}

