/* fiforead: read from the aprsdigi fifo */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <errno.h>

int Verbose = 1;


static void die(char *s);

int
main(int argc, char **argv)
{
  struct sockaddr_un mysun,remsun;
  int size;
  int sock;
  u_char buf[1024];
  int n;
 
  if (argc != 2) {
    fprintf(stderr,"Usage: %s file\n", argv[0]);
    exit(1);
  }
  bzero(&mysun,sizeof(mysun));
  mysun.sun_family = AF_UNIX;
  strncpy(mysun.sun_path,argv[1],sizeof(mysun.sun_path));
  if (Verbose)
    fprintf(stderr,"opening unix socket on %s\n", mysun.sun_path);
  if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
    die("socket");
  }
  if (bind(sock, (struct sockaddr *)&mysun, sizeof(mysun)) < 0 
      && errno != EADDRINUSE) {
    die(mysun.sun_path);
  }
  size = sizeof(remsun);
  while ((n = recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&remsun,&size)) >= 0) {
    printf("read %d: %*.*s\n",n,n,n,buf);
    size = sizeof(remsun);
  }
}

static void
die(char *s)
{
  perror(s);
  exit(1);
}

