ubase/chvt.c

62 lines
1.1 KiB
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
2014-06-30 14:03:41 -04:00
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2014-06-30 14:03:41 -04:00
#include "util.h"
2013-08-19 11:27:21 -04:00
#define KDGKBTYPE 0x4B33 /* get keyboard type */
#define VT_ACTIVATE 0x5606 /* make vt active */
#define VT_WAITACTIVE 0x5607 /* wait for vt active */
2013-08-29 19:28:17 -04:00
static char *vts[] = {
"/proc/self/fd/0",
"/dev/console",
"/dev/tty",
"/dev/tty0",
};
static void
usage(void)
{
eprintf("usage: chvt N\n");
}
int
2013-10-07 15:41:30 -04:00
main(int argc, char *argv[])
{
unsigned int n, i;
int fd;
char c;
if (argc != 2 || strspn(argv[1], "1234567890") != strlen(argv[1]))
usage();
n = estrtol(argv[1], 10);
for (i = 0; i < LEN(vts); i++) {
fd = open(vts[i], O_RDONLY);
2014-06-30 11:55:00 -04:00
if (fd < 0)
continue;
c = 0;
if (ioctl(fd, KDGKBTYPE, &c) == 0)
goto VTfound;
close(fd);
}
eprintf("couldn't find a console.\n");
VTfound:
if (ioctl(fd, VT_ACTIVATE, n) == -1)
eprintf("VT_ACTIVATE %d:", n);
if (ioctl(fd, VT_WAITACTIVE, n) == -1)
eprintf("VT_WAITACTIVE %d:", n);
close(fd);
2014-10-02 18:45:25 -04:00
return 0;
}