xref: /minix3/minix/commands/loadkeys/loadkeys.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc /*	loadkeys - load national keyboard map		Author: Marcus Hampel
2*433d6423SLionel Sambuc  */
3*433d6423SLionel Sambuc #include <sys/types.h>
4*433d6423SLionel Sambuc #include <sys/ioctl.h>
5*433d6423SLionel Sambuc #include <minix/keymap.h>
6*433d6423SLionel Sambuc #include <fcntl.h>
7*433d6423SLionel Sambuc #include <unistd.h>
8*433d6423SLionel Sambuc #include <stdlib.h>
9*433d6423SLionel Sambuc #include <stdio.h>
10*433d6423SLionel Sambuc #include <string.h>
11*433d6423SLionel Sambuc #include <errno.h>
12*433d6423SLionel Sambuc 
13*433d6423SLionel Sambuc #define KBD_DEVICE	"/dev/console"
14*433d6423SLionel Sambuc 
fatal(char * say)15*433d6423SLionel Sambuc void fatal(char *say)
16*433d6423SLionel Sambuc {
17*433d6423SLionel Sambuc   fprintf(stderr, "loadkeys: %s: %s\n", say, strerror(errno));
18*433d6423SLionel Sambuc   exit(EXIT_FAILURE);
19*433d6423SLionel Sambuc }
20*433d6423SLionel Sambuc 
21*433d6423SLionel Sambuc 
usage(void)22*433d6423SLionel Sambuc void usage(void)
23*433d6423SLionel Sambuc {
24*433d6423SLionel Sambuc   fprintf(stderr, "usage: loadkeys <mapfile>\n");
25*433d6423SLionel Sambuc   exit(EXIT_FAILURE);
26*433d6423SLionel Sambuc }
27*433d6423SLionel Sambuc 
28*433d6423SLionel Sambuc 
main(int argc,char * argv[])29*433d6423SLionel Sambuc int main(int argc, char *argv[])
30*433d6423SLionel Sambuc {
31*433d6423SLionel Sambuc   char sig[4];
32*433d6423SLionel Sambuc   keymap_t keymap;
33*433d6423SLionel Sambuc   int fd;
34*433d6423SLionel Sambuc 
35*433d6423SLionel Sambuc   if (argc != 2)
36*433d6423SLionel Sambuc 	usage();
37*433d6423SLionel Sambuc 
38*433d6423SLionel Sambuc   if ((fd = open(argv[1], O_RDONLY)) < 0) fatal(argv[1]);
39*433d6423SLionel Sambuc 
40*433d6423SLionel Sambuc   if (read(fd, sig, sizeof(sig)) < sizeof(sig)) fatal(argv[1]);
41*433d6423SLionel Sambuc 
42*433d6423SLionel Sambuc   if (memcmp(sig, KEY_MAGIC, sizeof(sig)) != 0) {
43*433d6423SLionel Sambuc 	fprintf(stderr, "loadkeys: %s: not a keymap file\n", argv[1]);
44*433d6423SLionel Sambuc 	return EXIT_FAILURE;
45*433d6423SLionel Sambuc   }
46*433d6423SLionel Sambuc 
47*433d6423SLionel Sambuc   if (read(fd, keymap, sizeof(keymap)) < sizeof(keymap)) fatal(argv[1]);
48*433d6423SLionel Sambuc 
49*433d6423SLionel Sambuc   close(fd);
50*433d6423SLionel Sambuc 
51*433d6423SLionel Sambuc   if ((fd = open(KBD_DEVICE, O_WRONLY)) < 0) fatal(KBD_DEVICE);
52*433d6423SLionel Sambuc 
53*433d6423SLionel Sambuc   if (ioctl(fd, KIOCSMAP, keymap) < 0) fatal(KBD_DEVICE);
54*433d6423SLionel Sambuc 
55*433d6423SLionel Sambuc   return EXIT_SUCCESS;
56*433d6423SLionel Sambuc }
57