blob: 9afb059b9205f12d7434648eeb679603735f3f16 [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001/* vi: set sw=4 ts=4:
2 *
Rob Landley5c4a2172008-01-26 23:18:08 -06003 * chvt.c - switch virtual terminals
4 *
Rob Landleydaa7fe62008-01-06 16:01:11 -06005 * Copyright (C) 2008 David Anders <danders@amltd.com>
6 *
Rob Landley28964802008-01-19 17:08:39 -06007 * Not in SUSv3.
8
Rob Landley55928b12008-01-19 17:43:27 -06009USE_CHVT(NEWTOY(chvt, "<1", TOYFLAG_USR|TOYFLAG_SBIN))
10
Rob Landley28964802008-01-19 17:08:39 -060011config CHVT
12 bool "chvt"
13 default y
14 help
15 usage: chvt N
16
17 Change to virtual terminal number N. (This only works in text mode.)
18
19 Virtual terminals are the Linux VGA text mode displays, ordinarily
20 switched between via alt-F1, alt-F2, etc. Use ctrl-alt-F1 to switch
21 from X to a virtual terminal, and alt-F6 (or F7, or F8) to get back.
22*/
Rob Landleydaa7fe62008-01-06 16:01:11 -060023
24#include "toys.h"
25
Rob Landley5c4a2172008-01-26 23:18:08 -060026/* Note: get_console_fb() will need to be moved into a seperate lib section */
Rob Landleydaa7fe62008-01-06 16:01:11 -060027int get_console_fd()
28{
Rob Landley5c4a2172008-01-26 23:18:08 -060029 int fd;
Rob Landley839fb0b2008-10-24 23:19:38 -050030 char *consoles[]={"/dev/console", "/dev/vc/0", "/dev/tty", NULL}, **cc;
Rob Landleydaa7fe62008-01-06 16:01:11 -060031
Rob Landley839fb0b2008-10-24 23:19:38 -050032 cc = consoles;
33 while (*cc) {
34 fd = open(*cc++, O_RDWR);
35 if (fd >= 0) return fd;
36 }
Rob Landleydaa7fe62008-01-06 16:01:11 -060037
Rob Landley5c4a2172008-01-26 23:18:08 -060038 return -1;
Rob Landleydaa7fe62008-01-06 16:01:11 -060039}
40
41void chvt_main(void)
42{
Rob Landley5c4a2172008-01-26 23:18:08 -060043 int vtnum, fd;
Rob Landleydaa7fe62008-01-06 16:01:11 -060044
Rob Landley5c4a2172008-01-26 23:18:08 -060045 vtnum=atoi(*toys.optargs);
Rob Landleydaa7fe62008-01-06 16:01:11 -060046
Rob Landley5c4a2172008-01-26 23:18:08 -060047 fd=get_console_fd();
Rob Landley839fb0b2008-10-24 23:19:38 -050048 // These numbers are VT_ACTIVATE and VT_WAITACTIVE from linux/vt.h
49 if (fd < 0 || ioctl(fd, 0x5606, vtnum) || ioctl(fd, 0x5607, vtnum))
Rob Landley651e2532008-01-27 15:26:32 -060050 perror_exit(NULL);
Rob Landleydaa7fe62008-01-06 16:01:11 -060051}