loadkmap.c revision 1.11
1/*	$NetBSD: loadkmap.c,v 1.11 2015/08/12 17:53:03 tsutsui Exp $	*/
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <sys/ioctl.h>
6#include "../../dev/iteioctl.h"
7#include "../../dev/kbdmap.h"
8#include <stdio.h>
9#include <string.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <fcntl.h>
13
14static int load_kmap(const char *, int);
15static int dump_kmap(void);
16
17int
18main(int argc, char *argv[])
19{
20	int	set_sysmap = 0;
21	char	*mapfile;
22	int	rc = 0;
23
24	if (argc > 2) {
25		if ((argc == 3) && !strcmp(argv[1], "-f")) {
26			mapfile = argv[2];
27			set_sysmap = 1;
28		}
29		else {
30			fprintf(stderr, "%s [-f] keymap\n", argv[0]);
31			exit(1);
32		}
33	}
34	else mapfile = argv[1];
35
36	if (argc == 1)
37		rc = dump_kmap();
38	else rc = load_kmap(mapfile, set_sysmap);
39
40	exit (rc);
41}
42
43
44static int
45load_kmap(const char *file, int set_sysmap)
46{
47	int	fd;
48	char	buf[sizeof (struct kbdmap)];
49	int	ioc;
50
51	ioc = set_sysmap ? ITEIOCSSKMAP : ITEIOCSKMAP;
52
53	if ((fd = open (file, 0)) >= 0) {
54		if (read (fd, buf, sizeof (buf)) == sizeof (buf)) {
55			if (ioctl (0, ioc, buf) == 0) {
56				close(fd);
57				return 0;
58			}
59			else perror("ITEIOCSKMAP");
60		}
61		else perror("read kmap");
62
63		close(fd);
64	}
65	else perror("open kmap");
66	return 1;
67}
68
69static int
70dump_kmap(void)
71{
72	char buf[sizeof (struct kbdmap)];
73
74	if (ioctl (0, ITEIOCGKMAP, buf) == 0) {
75		write (1, buf, sizeof (buf));
76		return 0;
77	}
78	perror ("ITEIOCGKMAP");
79	return 1;
80}
81