File: irw.cpp

package info (click to toggle)
lirc 0.10.2-0.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,268 kB
  • sloc: ansic: 26,981; cpp: 9,187; sh: 5,875; python: 4,507; makefile: 1,049; xml: 63
file content (110 lines) | stat: -rw-r--r-- 2,563 bytes parent folder | download | duplicates (5)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/****************************************************************************
** irw.c *******************************************************************
****************************************************************************
*
* irw - watch the codes as lircd recognize them
*
* Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
* Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
*
*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <errno.h>
#include <getopt.h>

#include "lirc_log.h"

static struct option long_options[] = {
	{ "help",    no_argument, NULL, 'h' },
	{ "version", no_argument, NULL, 'v' },
	{ 0,	     0,		  0,	0   }
};


void sigusr1(int sig)
{
	exit(0);
}



int main(int argc, char* argv[])
{
	struct sigaction act;
	int fd, i;
	char buf[128];
	struct sockaddr_un addr;
	int c;
	const char* progname;

	progname = "irw " VERSION;

	act.sa_handler = sigusr1;
	sigfillset(&act.sa_mask);
	act.sa_flags = SA_RESTART;      /* don't fiddle with EINTR */
	sigaction(SIGUSR1, &act, NULL);

	addr.sun_family = AF_UNIX;

	while ((c = getopt_long(argc, argv, "hv", long_options, NULL))
	       != EOF) {
		switch (c) {
		case 'h':
			printf("Usage: %s [socket]\n", argv[0]);
			printf("\t -h --help \t\tdisplay usage summary\n");
			printf("\t -v --version \t\tdisplay version\n");
			return EXIT_SUCCESS;
		case 'v':
			printf("%s\n", progname);
			return EXIT_SUCCESS;
		case '?':
			fprintf(stderr, "unrecognized option: -%c\n", optopt);
			fprintf(stderr, "Try `%s --help' for more information.\n", progname);
			return EXIT_FAILURE;
		}
	}
	if (argc == optind) {
		/* no arguments */
		strcpy(addr.sun_path, LIRCD);
	} else if (argc == optind + 1) {
		/* one argument */
		strncpy(addr.sun_path, argv[optind], sizeof(addr.sun_path) - 1);
	} else {
		fprintf(stderr, "%s: incorrect number of arguments.\n", progname);
		fprintf(stderr, "Try `%s --help' for more information.\n", progname);
		return EXIT_FAILURE;
	}
	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1) {
		perror("socket");
		exit(errno);
	}
	if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		perrorf("Cannot connect to socket %s", addr.sun_path);
		exit(errno);
	}
	for (;; ) {
		i = read(fd, buf, 128);
		if (i == -1) {
			perror("read");
			exit(errno);
		}
		if (!i)
			exit(0);
		chk_write(STDOUT_FILENO, buf, i);
	}
}