Add functions to check diacr/func existance
[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 __attribute__ ((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         setlocale(LC_ALL, "");
42         bindtextdomain(PACKAGE_NAME, LOCALEDIR);
43         textdomain(PACKAGE_NAME);
44
45         set_progname(argv[0]);
46         while (( c = getopt_long (argc, argv, "Vhn", long_opts, NULL)) != EOF) {
47           switch (c) {
48           case 'h':
49             usage();
50             exit (0);
51           case 'n':
52             show_vt = 1;
53             break;
54           case 'V':
55             print_version_and_exit();
56             break;
57           case '?':
58             usage();
59             exit(1);
60           }
61         }
62         
63         fd = getfd(NULL);
64         if (show_vt) {
65           if ((ioctl(fd, VT_OPENQRY, &vtno) < 0) || vtno == -1) {
66              perror (_("Couldn't read VTNO: "));
67              exit(2);
68           }
69           printf ("%d\n", vtno);
70           exit(0);
71         }
72         
73         if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
74           printf ("serial\n");
75           exit (0);
76         }
77         
78         if (ioctl(fd, VT_GETSTATE, &vtstat)) 
79           {
80             perror("fgconsole: VT_GETSTATE");
81             exit(1);
82           }
83         printf("%d\n", vtstat.v_active);
84         return 0;
85 }