File: fiforead.c

package info (click to toggle)
aprsdigi 3.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 420 kB
  • sloc: ansic: 3,690; sh: 656; makefile: 24
file content (60 lines) | stat: -rw-r--r-- 1,250 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* 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);

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);
}