ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

chvt.c (1170B)


      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 <limits.h>
      7 #include <stdio.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 *vt[] = {
     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: %s num\n", argv0);
     29 }
     30 
     31 int
     32 main(int argc, char *argv[])
     33 {
     34 	unsigned int n, i;
     35 	int fd;
     36 	char c;
     37 
     38 	ARGBEGIN {
     39 	default:
     40 		usage();
     41 	} ARGEND;
     42 
     43 	if (argc != 1)
     44 		usage();
     45 
     46 	n = estrtonum(argv[0], 0, UINT_MAX);
     47 	for (i = 0; i < LEN(vt); i++) {
     48 		if ((fd = open(vt[i], O_RDONLY)) < 0)
     49 			continue;
     50 		c = 0;
     51 		if (ioctl(fd, KDGKBTYPE, &c) == 0)
     52 			goto found;
     53 		if (close(fd) < 0)
     54 			eprintf("close %s:", vt[i]);
     55 	}
     56 	eprintf("no console found\n");
     57 
     58 found:
     59 	if (ioctl(fd, VT_ACTIVATE, n) == -1)
     60 		eprintf("VT_ACTIVATE %u:", n);
     61 	if (ioctl(fd, VT_WAITACTIVE, n) == -1)
     62 		eprintf("VT_WAITACTIVE %u:", n);
     63 	if (close(fd) < 0)
     64 		eprintf("close %s:", vt[i]);
     65 
     66 	return 0;
     67 }