commit 641c7e79f3add3b6decdbacf51119a908d458a33
parent 5df65ecfd4a57fe891eb11e6ccddc6c1949635ca
Author: David Galos <galosd83@students.rowan.edu>
Date: Thu, 15 Aug 2013 12:54:40 +0100
Add chvt(1)
This is essentially cherry-picked from sbase with slight
modifications as we can now assume that it will be running on
Linux.
Diffstat:
M | Makefile | | | 1 | + |
A | chvt.1 | | | 10 | ++++++++++ |
A | chvt.c | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 66 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -15,6 +15,7 @@ LIB = \
util/tty.o
SRC = \
+ chvt.c \
df.c \
dmesg.c \
halt.c \
diff --git a/chvt.1 b/chvt.1
@@ -0,0 +1,10 @@
+.TH CHVT 1 ubase\-VERSION
+.SH NAME
+chvt \- change foreground virtual terminal
+.SH SYNOPSIS
+.B chvt
+.I N
+.SH DESCRIPTION
+.B chvt
+brings /dev/ttyN to the foreground. This has the
+same effect as Ctrl-Alt-FN.
diff --git a/chvt.c b/chvt.c
@@ -0,0 +1,55 @@
+#include <linux/vt.h>
+#include <linux/kd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include "util.h"
+
+char *vts[] = {
+ "/proc/self/fd/0",
+ "/dev/console",
+ "/dev/tty",
+ "/dev/tty0",
+};
+
+static void
+usage(void)
+{
+ eprintf("usage: chvt N\n");
+}
+
+int
+main(int argc, char **argv)
+{
+ int n, i, fd;
+ char c;
+
+ if(argc!=2 || strspn(argv[1], "1234567890") != strlen(argv[1]))
+ usage();
+
+ n = atoi(argv[1]);
+ for(i = 0; i < LEN(vts); i++) {
+ fd = open(vts[i], O_RDONLY);
+ if(fd < 1)
+ continue;
+ c = 0;
+ if(ioctl(fd, KDGKBTYPE, &c) == 0)
+ goto VTfound;
+ close(fd);
+ }
+
+ eprintf("chvt: couldn't find a console.\n");
+VTfound:
+ if(ioctl(fd, VT_ACTIVATE, n) == -1)
+ eprintf("chvt: VT_ACTIVATE '%d':", n);
+ if(ioctl(fd, VT_WAITACTIVE, n) == -1)
+ eprintf("chvt: VT_WAITACTIVE '%d':", n);
+ close(fd);
+
+ return 0;
+}
+