File: irw.c

package info (click to toggle)
lirc 0.7.1pre2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,852 kB
  • ctags: 2,924
  • sloc: ansic: 31,205; sh: 12,021; makefile: 631
file content (102 lines) | stat: -rw-r--r-- 2,279 bytes parent folder | download | duplicates (4)
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
/*      $Id: irw.c,v 5.4 2002/08/17 11:17:41 lirc Exp $      */

/****************************************************************************
 ** 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 <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>

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

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

	progname="irw " VERSION;

	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 */
		strcpy(addr.sun_path,argv[optind]);
	} 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)  {
		perror("connect");
		exit(errno);
	};

	for(;;)  {
		i=read(fd,buf,128);
		if(i==-1)  {
			perror("read");
			exit(errno);
		};
		if(!i)  exit(0);
		write(STDOUT_FILENO,buf,i);
	};
}