1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <fcntl.h> 4 #include <string.h> 5 #include <linux/kd.h> 6 #include <linux/vt.h> 7 #include <errno.h> 8 9 static void setkey_usage(char *argv[]) 10 { 11 fprintf(stderr, "%s [-t <table>] [-k <index>] [-v value] [-r] [-h]\n" 12 " -t <table> Select table\n" 13 " -k <index> Select key\n" 14 " -v <value> Set entry\n" 15 " -r Read current entry\n" 16 " -h Print help\n", argv[0]); 17 } 18 19 #define TTYDEV "/dev/tty0" 20 21 int setkey_main(int argc, char *argv[]) 22 { 23 int fd; 24 struct kbentry kbe; 25 int did_something = 0; 26 27 kbe.kb_table = 0; 28 kbe.kb_index = -1; 29 kbe.kb_value = 0; 30 31 fd = open(TTYDEV, O_RDWR | O_SYNC); 32 if (fd < 0) { 33 fprintf(stderr, "open %s: %s\n", TTYDEV, strerror(errno)); 34 return 1; 35 } 36 37 do { 38 int c, ret; 39 40 c = getopt(argc, argv, "t:k:v:hr"); 41 if (c == EOF) 42 break; 43 44 switch (c) { 45 case 't': 46 kbe.kb_table = strtol(optarg, NULL, 0); 47 break; 48 case 'k': 49 kbe.kb_index = strtol(optarg, NULL, 0); 50 break; 51 case 'v': 52 kbe.kb_value = strtol(optarg, NULL, 0); 53 ret = ioctl(fd, KDSKBENT, &kbe); 54 if (ret < 0) { 55 fprintf(stderr, "KDSKBENT %d %d %d failed: %s\n", 56 kbe.kb_table, kbe.kb_index, kbe.kb_value, 57 strerror(errno)); 58 return 1; 59 } 60 did_something = 1; 61 break; 62 case 'r': 63 ret = ioctl(fd, KDGKBENT, &kbe); 64 if (ret < 0) { 65 fprintf(stderr, "KDGKBENT %d %d failed: %s\n", 66 kbe.kb_table, kbe.kb_index, strerror(errno)); 67 return 1; 68 } 69 printf("0x%x 0x%x 0x%x\n", 70 kbe.kb_table, kbe.kb_index, kbe.kb_value); 71 did_something = 1; 72 break; 73 case 'h': 74 setkey_usage(argv); 75 return 1; 76 case '?': 77 fprintf(stderr, "%s: invalid option -%c\n", 78 argv[0], optopt); 79 return 1; 80 } 81 } while (1); 82 83 if(optind != argc || !did_something) { 84 setkey_usage(argv); 85 return 1; 86 } 87 88 return 0; 89 } 90