xref: /third_party/toybox/toys/other/chvt.c (revision 0f66f451)
10f66f451Sopenharmony_ci/* chvt.c - switch virtual terminals
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright (C) 2008 David Anders <danders@amltd.com>
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_CHVT(NEWTOY(chvt, "<1", TOYFLAG_USR|TOYFLAG_BIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig CHVT
80f66f451Sopenharmony_ci  bool "chvt"
90f66f451Sopenharmony_ci  default y
100f66f451Sopenharmony_ci  help
110f66f451Sopenharmony_ci    usage: chvt N
120f66f451Sopenharmony_ci
130f66f451Sopenharmony_ci    Change to virtual terminal number N. (This only works in text mode.)
140f66f451Sopenharmony_ci
150f66f451Sopenharmony_ci    Virtual terminals are the Linux VGA text mode displays, ordinarily
160f66f451Sopenharmony_ci    switched between via alt-F1, alt-F2, etc. Use ctrl-alt-F1 to switch
170f66f451Sopenharmony_ci    from X to a virtual terminal, and alt-F6 (or F7, or F8) to get back.
180f66f451Sopenharmony_ci*/
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci#include "toys.h"
210f66f451Sopenharmony_ci
220f66f451Sopenharmony_civoid chvt_main(void)
230f66f451Sopenharmony_ci{
240f66f451Sopenharmony_ci  int vtnum, fd = fd;
250f66f451Sopenharmony_ci  char *consoles[]={"/dev/console", "/dev/vc/0", "/dev/tty", NULL}, **cc;
260f66f451Sopenharmony_ci
270f66f451Sopenharmony_ci  vtnum=atoi(*toys.optargs);
280f66f451Sopenharmony_ci  for (cc = consoles; *cc; cc++)
290f66f451Sopenharmony_ci    if (-1 != (fd = open(*cc, O_RDWR))) break;
300f66f451Sopenharmony_ci
310f66f451Sopenharmony_ci  // These numbers are VT_ACTIVATE and VT_WAITACTIVE from linux/vt.h
320f66f451Sopenharmony_ci  if (!*cc || fd < 0 || ioctl(fd, 0x5606, vtnum) || ioctl(fd, 0x5607, vtnum))
330f66f451Sopenharmony_ci    perror_exit(0);
340f66f451Sopenharmony_ci}
35