[SYSTEM] Apply patch from Debian.
authorAlexey Gladkov <legion@altlinux.org>
Tue, 30 Jan 2007 21:04:39 +0000 (00:04 +0300)
committerAlexey Gladkov <legion@altlinux.org>
Tue, 30 Jan 2007 21:04:39 +0000 (00:04 +0300)
Apply fgconsole.diff patch from Fedora distribution.
Add --help, --version and --next-available arguments.

Signed-off-by: Alexey Gladkov <legion@altlinux.org>
man/man1/fgconsole.1
src/fgconsole.c

index e743c48..de9652b 100644 (file)
@@ -5,13 +5,23 @@ fgconsole \- print the number of the active VT.
 
 .SH SYNOPSIS
 .B fgconsole
-
+.BI [ "--help" | "--version" | "--next-available" ]
 .SH DESCRIPTION
 If the active Virtual Terminal is
 .IR /dev/ttyN ,
 then prints
 .I N
 on standard output.
+
+If the console is a serial console, then 
+"serial" 
+is printed instead.
+.TP
+.I \-\-next\-available
+Will show the next unallocated virtual terminal. Normally 6 virtual
+terminals are allocated, with number 7 used for X; this will return
+"8" in this case.
+
 .SH NOTES
 Under 
 .IR devfs ,
index d91dcf9..d0079a2 100644 (file)
@@ -4,24 +4,81 @@
 #include <stdio.h>
 #include <stdlib.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"
+
+static void 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 this help text\n"
+"      -n --next-available  display next unallocated VT\n"),
+                       progname, VERSION, progname);
+       exit(1);
+}
 
 int
 main(int argc, char **argv){
-       int fd;
        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, LOCALEDIR);
        textdomain(PACKAGE);
 
+       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);
+         }
+       }
+       
        fd = getfd(NULL);
-       if (ioctl(fd, VT_GETSTATE, &vtstat)) {
-               perror("fgconsole: VT_GETSTATE");
-               exit(1);
+       if (show_vt) {
+         if ((ioctl(fd, VT_OPENQRY, &vtno) < 0) || vtno == -1) {
+            perror (_("Couldn't read VTNO: "));
+            exit(2);
+         }
+         printf ("%d\n", vtno);
+         exit(0);
+       }
+       
+       if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
+         printf ("serial\n");
+         exit (0);
        }
+       
+       if (ioctl(fd, VT_GETSTATE, &vtstat)) 
+         {
+           perror("fgconsole: VT_GETSTATE");
+           exit(1);
+         }
        printf("%d\n", vtstat.v_active);
        return 0;
 }