4 #include <netinet/in.h>
6 #define KDGKBTYPE 0x4B33
7 #define ARRAYSIZE(a) sizeof(a)/sizeof(a[0])
8 static int console_fd(int mode
) {
9 const char devs
[][16] = { "/dev/tty", "/dev/vc/0", "/dev/console" };
12 while(i
< ARRAYSIZE(devs
) && fd
== -1) {
13 fd
= open(devs
[i
++], mode
);
15 if(fd
!= -1 && ioctl(fd
, KDGKBTYPE
, &arg
)) {
24 unsigned char kb_table
;
25 unsigned char kb_index
;
26 unsigned short kb_value
;
29 #define KDGKBENT 0x4B46
31 #define MAX_NR_KEYMAPS 256
36 #include <ulz/stdio-repl.h>
39 #define noret __attribute__((noreturn))
41 noret
static void die_perror(const char* msg
) {
46 noret
static void usage(const char *arg0
) {
47 dprintf(2, "usage: %s > backup.kmap\n"
48 "dumps current keymap to stdout.\n"
49 "redirect to file as output contains binary data\n", arg0
);
53 int main(int argc
, char** argv
) {
55 if(argv
[1] || isatty(1)) usage(argv
[0]);
56 const char flags
[MAX_NR_KEYMAPS
] = {
57 [0] = 1, [1] = 1, [2] = 1,
58 [4] = 1, [5] = 1, [6] = 1,
59 [8] = 1, [9] = 1, [10] = 1,
62 int cfd
= console_fd(O_RDONLY
);
63 if(cfd
== -1) die_perror("could not get console fd");
64 write(1, "hcukmap", 7);
65 write(1, flags
, MAX_NR_KEYMAPS
);
67 for(;i
< 13; i
++) if(flags
[i
]) for(j
=0; j
< NR_KEYS
; j
++) {
68 struct kbentry ke
= { .kb_index
= j
, .kb_table
= i
};
69 if(ioctl(cfd
, KDGKBENT
, &ke
)) die_perror("failed to get key setting");
71 s
= htons(ke
.kb_value
);