File: fgconsole.c

package info (click to toggle)
kbd 2.0.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 14,136 kB
  • sloc: sh: 12,838; ansic: 12,531; pascal: 472; lex: 466; makefile: 394; yacc: 356; perl: 126; sed: 16
file content (89 lines) | stat: -rw-r--r-- 2,105 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * fgconsole.c - aeb - 960123 - Print foreground console
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <linux/vt.h>
#include <linux/serial.h>
#include "getfd.h"
#include "nls.h"
#include "version.h"
#include "kbd_error.h"

static void __attribute__((noreturn))
usage(void)
{
	fprintf(stderr, _("%s version %s\n"
	                  "\n"
	                  "Usage: %s [options]\n"
	                  "\n"
	                  "Valid options are:\n"
	                  "\n"
	                  "	-h --help            display this help text\n"
	                  "	-V --version         display program version\n"
	                  "	-n --next-available  display number of next unallocated VT\n"),
	        progname, PACKAGE_VERSION, progname);
	exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
	struct vt_stat vtstat;
	int fd, vtno = -1, c, show_vt = 0;
	struct serial_struct sr;
	const struct option long_opts[] = {
		{ "help", no_argument, NULL, 'h' },
		{ "version", no_argument, NULL, 'V' },
		{ "next-available", no_argument, NULL, 'n' },
		{ NULL, 0, NULL, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE_NAME, LOCALEDIR);
	textdomain(PACKAGE_NAME);

	set_progname(argv[0]);
	while ((c = getopt_long(argc, argv, "Vhn", long_opts, NULL)) != EOF) {
		switch (c) {
			case 'h':
				usage();
				exit(0);
			case 'n':
				show_vt = 1;
				break;
			case 'V':
				print_version_and_exit();
				break;
			case '?':
				usage();
				exit(1);
		}
	}

	if ((fd = getfd(NULL)) < 0)
		kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console"));

	if (show_vt) {
		if ((ioctl(fd, VT_OPENQRY, &vtno) < 0) || vtno == -1) {
			kbd_error(2, errno, _("Couldn't read VTNO: "));
		}
		printf("%d\n", vtno);
		return EXIT_SUCCESS;
	}

	if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
		printf("serial\n");
		return EXIT_SUCCESS;
	}

	if (ioctl(fd, VT_GETSTATE, &vtstat)) {
		kbd_error(EXIT_FAILURE, errno, "fgconsole: VT_GETSTATE");
	}
	printf("%d\n", vtstat.v_active);
	return EXIT_SUCCESS;
}