summaryrefslogtreecommitdiffstats
path: root/toolbox/setkey.c
blob: 1ff27744c0f90c06560d63278c2a0e270e2e1bcc (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/kd.h>
#include <linux/vt.h>
#include <errno.h>

static void setkey_usage(char *argv[])
{
    fprintf(stderr, "%s [-t <table>] [-k <index>] [-v value] [-r] [-h]\n"
            "  -t <table> Select table\n"
            "  -k <index> Select key\n"
            "  -v <value> Set entry\n"
            "  -r         Read current entry\n"
            "  -h         Print help\n", argv[0]);
}

#define TTYDEV	"/dev/tty0"

int setkey_main(int argc, char *argv[])
{
    int fd;
    struct kbentry kbe;
    int did_something = 0;

    kbe.kb_table = 0;
    kbe.kb_index = -1;
    kbe.kb_value = 0;

    fd = open(TTYDEV, O_RDWR | O_SYNC);
    if (fd < 0) {
        fprintf(stderr, "open %s: %s\n", TTYDEV, strerror(errno));
        return 1;
    }

    do {
        int c, ret;

        c = getopt(argc, argv, "t:k:v:hr");
        if (c == EOF)
            break;

        switch (c) {
        case 't':
            kbe.kb_table = strtol(optarg, NULL, 0);
            break;
        case 'k':
            kbe.kb_index = strtol(optarg, NULL, 0);
            break;
        case 'v':
            kbe.kb_value = strtol(optarg, NULL, 0);
            ret = ioctl(fd, KDSKBENT, &kbe);
            if (ret < 0) {
                fprintf(stderr, "KDSKBENT %d %d %d failed: %s\n",
                        kbe.kb_table, kbe.kb_index, kbe.kb_value,
                        strerror(errno));
                return 1;
            }
            did_something = 1;
            break;
        case 'r':
            ret = ioctl(fd, KDGKBENT, &kbe);
            if (ret < 0) {
                fprintf(stderr, "KDGKBENT %d %d  failed: %s\n",
                        kbe.kb_table, kbe.kb_index, strerror(errno));
                return 1;
            }
            printf("0x%x 0x%x 0x%x\n",
                   kbe.kb_table, kbe.kb_index, kbe.kb_value);
            did_something = 1;
            break;
        case 'h':
            setkey_usage(argv);
            return 1;
        case '?':
            fprintf(stderr, "%s: invalid option -%c\n",
                argv[0], optopt);
            return 1;
        }
    } while (1);

    if(optind != argc || !did_something) {
        setkey_usage(argv);
        return 1;
    }

    return 0;
}