chvt.c (1099B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <sys/ioctl.h> 3 #include <sys/types.h> 4 5 #include <fcntl.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <unistd.h> 10 11 #include "util.h" 12 13 #define KDGKBTYPE 0x4B33 /* get keyboard type */ 14 15 #define VT_ACTIVATE 0x5606 /* make vt active */ 16 #define VT_WAITACTIVE 0x5607 /* wait for vt active */ 17 18 static char *vts[] = { 19 "/proc/self/fd/0", 20 "/dev/console", 21 "/dev/tty", 22 "/dev/tty0", 23 }; 24 25 static void 26 usage(void) 27 { 28 eprintf("usage: chvt N\n"); 29 } 30 31 int 32 main(int argc, char *argv[]) 33 { 34 unsigned int n, i; 35 int fd; 36 char c; 37 38 if (argc != 2 || strspn(argv[1], "1234567890") != strlen(argv[1])) 39 usage(); 40 41 n = estrtol(argv[1], 10); 42 for (i = 0; i < LEN(vts); i++) { 43 fd = open(vts[i], O_RDONLY); 44 if (fd < 0) 45 continue; 46 c = 0; 47 if (ioctl(fd, KDGKBTYPE, &c) == 0) 48 goto VTfound; 49 close(fd); 50 } 51 52 eprintf("couldn't find a console.\n"); 53 VTfound: 54 if (ioctl(fd, VT_ACTIVATE, n) == -1) 55 eprintf("VT_ACTIVATE %d:", n); 56 if (ioctl(fd, VT_WAITACTIVE, n) == -1) 57 eprintf("VT_WAITACTIVE %d:", n); 58 close(fd); 59 60 return 0; 61 }