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