Listing 3. wandtext.c

/* wandtest.c rudimentary test of barcode device driver */
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>

void sig_int(void);

char *dev_name = "/dev/wand";
int fd;

main()
{
      int video;
      char buf[1];

      fd = open(dev_name, O_RDONLY);
      if ( fd == -1 ) { /* open failed */
           printf("cannot open %s\n",dev_name);
           return;
      } else
           printf(" %s opened for reading\n",dev_name);


      /* catch control-c */
      signal(SIGINT,(void *)sig_int);

      while ( 1 ) {
           video = read(fd,buf,1);
           if ( video == 0x00 ) printf(" WHITE\r");
           if ( video == 0x80 ) printf(" BLACK\r");
      }
}

void sig_int(void)
{
      int err;

      err = close(fd);
      printf("\nwand %d closed, err = %d\n",fd,err);
      signal(SIGINT,SIG_DFL);
}