Note how to find list of BusyBox libc dependancies, the ide being to
[oweals/busybox.git] / console-tools / loadkmap.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini loadkmap implementation for busybox
4  *
5  * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 #include "internal.h"
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <linux/kd.h>
28 #include <linux/keyboard.h>
29 #include <sys/ioctl.h>
30
31
32 static const char loadkmap_usage[] = "loadkmap\n\n"
33         "Loads a binary keyboard translation table from standard input.\n";
34
35
36 int loadkmap_main(int argc, char **argv)
37 {
38         struct kbentry ke;
39         u_short *ibuff;
40         int i, j, fd, readsz, pos, ibuffsz = NR_KEYS * sizeof(u_short);
41         char flags[MAX_NR_KEYMAPS], magic[] = "bkeymap", buff[7];
42
43         if (argc>=2 && *argv[1]=='-') {
44                 usage(loadkmap_usage);
45         }
46
47         fd = open("/dev/tty0", O_RDWR);
48         if (fd < 0) {
49                 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
50                 exit(FALSE);
51         }
52
53         read(0, buff, 7);
54         if (0 != strncmp(buff, magic, 7)) {
55                 fprintf(stderr, "This is not a valid binary keymap.\n");
56                 exit(FALSE);
57         }
58
59         if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS)) {
60                 fprintf(stderr, "Error reading keymap flags: %s\n",
61                                 strerror(errno));
62                 exit(FALSE);
63         }
64
65         ibuff = (u_short *) malloc(ibuffsz);
66         if (!ibuff) {
67                 fprintf(stderr, "Out of memory.\n");
68                 exit(FALSE);
69         }
70
71         for (i = 0; i < MAX_NR_KEYMAPS; i++) {
72                 if (flags[i] == 1) {
73                         pos = 0;
74                         while (pos < ibuffsz) {
75                                 if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos))
76                                         < 0) {
77                                         fprintf(stderr, "Error reading keymap: %s\n",
78                                                         strerror(errno));
79                                         exit(FALSE);
80                                 }
81                                 pos += readsz;
82                         }
83                         for (j = 0; j < NR_KEYS; j++) {
84                                 ke.kb_index = j;
85                                 ke.kb_table = i;
86                                 ke.kb_value = ibuff[j];
87                                 ioctl(fd, KDSKBENT, &ke);
88                         }
89                 }
90         }
91         /* Don't bother to close files.  Exit does that 
92          * automagically, so we can save a few bytes */
93         /* close(fd); */
94         exit(TRUE);
95 }