Imported Upstream version 2.0.90
[platform/upstream/kbd.git] / src / fgconsole.c
1 /*
2  * fgconsole.c - aeb - 960123 - Print foreground console
3  */
4 #include "config.h"
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <sys/ioctl.h>
10 #include <getopt.h>
11 #include <sysexits.h>
12 #include <linux/vt.h>
13 #include <linux/serial.h>
14
15 #include "libcommon.h"
16
17 static void __attribute__((noreturn))
18 usage(int rc)
19 {
20         const char *progname = get_progname();
21         fprintf(stderr, _("%s version %s\n"
22                           "\n"
23                           "Usage: %s [options]\n"
24                           "\n"
25                           "Valid options are:\n"
26                           "\n"
27                           "     -h --help            display this help text\n"
28                           "     -V --version         display program version\n"
29                           "     -n --next-available  display number of next unallocated VT\n"),
30                 progname, PACKAGE_VERSION, progname);
31         exit(rc);
32 }
33
34 int main(int argc, char **argv)
35 {
36         struct vt_stat vtstat;
37         int fd, vtno = -1, c, show_vt = 0;
38         struct serial_struct sr;
39         const struct option long_opts[] = {
40                 { "help", no_argument, NULL, 'h' },
41                 { "version", no_argument, NULL, 'V' },
42                 { "next-available", no_argument, NULL, 'n' },
43                 { NULL, 0, NULL, 0 }
44         };
45
46         set_progname(argv[0]);
47         setuplocale();
48
49         while ((c = getopt_long(argc, argv, "Vhn", long_opts, NULL)) != EOF) {
50                 switch (c) {
51                         case 'n':
52                                 show_vt = 1;
53                                 break;
54                         case 'V':
55                                 print_version_and_exit();
56                                 break;
57                         case 'h':
58                                 usage(EXIT_SUCCESS);
59                                 break;
60                         case '?':
61                                 usage(EX_USAGE);
62                                 break;
63                 }
64         }
65
66         if ((fd = getfd(NULL)) < 0)
67                 kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console"));
68
69         if (show_vt) {
70                 if ((ioctl(fd, VT_OPENQRY, &vtno) < 0) || vtno == -1) {
71                         kbd_error(2, errno, _("Couldn't read VTNO: "));
72                 }
73                 printf("%d\n", vtno);
74                 return EXIT_SUCCESS;
75         }
76
77         if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
78                 printf("serial\n");
79                 return EXIT_SUCCESS;
80         }
81
82         if (ioctl(fd, VT_GETSTATE, &vtstat)) {
83                 kbd_error(EXIT_FAILURE, errno, "fgconsole: VT_GETSTATE");
84         }
85         printf("%d\n", vtstat.v_active);
86         return EXIT_SUCCESS;
87 }