Welcome to little lamb

Code » anopa » commit 3a13f4d

tty: Fix when multiple devices are listed as console

author Olivier Brunel
2016-01-18 14:01:03 UTC
committer Olivier Brunel
2016-01-18 14:01:03 UTC
parent d72e4dbe4d237c39ee09c91a56457a519883d0cb

tty: Fix when multiple devices are listed as console

We missed that /sys/class/tty/console/active is actually a list of
devices, the last one being the active one.

Thanks to insam; Fixes #3

src/utils/aa-tty.c +11 -4

diff --git a/src/utils/aa-tty.c b/src/utils/aa-tty.c
index 961fb06..8ec2d50 100644
--- a/src/utils/aa-tty.c
+++ b/src/utils/aa-tty.c
@@ -47,6 +47,7 @@ main (int argc, char * const argv[])
     char file[256];
     int max = sizeof (file) - sizeof (PREFIX) - sizeof (NAME) + 1;
     char name[max];
+    int skip;
     int r;
 
     for (;;)
@@ -90,24 +91,30 @@ main (int argc, char * const argv[])
     r = openreadnclose (file, name, max);
     if (r <= 0)
         aa_strerr_diefu2sys (2, "read ", file);
+    /* last entry is the active one */
+    skip = byte_rchr (name, r, ' ') + 1;
+    if (skip > r)
+        skip = 0;
 
     for (;;)
     {
-        int l = r;
+        const char *s = name + skip;
+        int l = r - skip;
 
-        byte_copy (file + sizeof (PREFIX) - 1, r, name);
-        byte_copy (file + sizeof (PREFIX) - 2 + r, sizeof (NAME), NAME);
+        byte_copy (file + sizeof (PREFIX) - 1, l, s);
+        byte_copy (file + sizeof (PREFIX) - 2 + l, sizeof (NAME), NAME);
         r = openreadnclose (file, name, max);
         if (r <= 0)
         {
             if (errno == ENOENT)
             {
                 aa_bs_noflush (AA_OUT, "/dev/");
-                aa_bb_flush (AA_OUT, name, l);
+                aa_bb_flush (AA_OUT, s, l);
                 return 0;
             }
             else
                 aa_strerr_diefu2sys (2, "read ", file);
         }
+        skip = 0;
     }
 }