Welcome to little lamb

Code » anopa » commit ca5c8cc

Add aa-tty

author Olivier Brunel
2015-04-21 19:22:59 UTC
committer Olivier Brunel
2015-04-21 19:22:59 UTC
parent 999535f0a1fbe42d96a43e73e9bca9d7776ba76d

Add aa-tty

doc/aa-tty.pod +27 -0
package/modes +1 -0
package/targets.mak +2 -0
src/utils/aa-tty.c +87 -0
src/utils/deps-exe/aa-tty +2 -0

diff --git a/doc/aa-tty.pod b/doc/aa-tty.pod
new file mode 100644
index 0000000..ee8d16d
--- /dev/null
+++ b/doc/aa-tty.pod
@@ -0,0 +1,27 @@
+=head1 NAME
+
+aa-tty - Prints the device name of the active tty
+
+=head1 SYNOPSIS
+
+B<aa-tty>
+
+=head1 OPTIONS
+
+=over
+
+=item B<-h, --help>
+
+Show help screen and exit.
+
+=item B<-V, --version>
+
+Show version information and exit.
+
+=back
+
+=head1 DESCRIPTION
+
+B<aa-tty>(1) is a small tool that will print to device name of the active tty.
+It determines it by reading I<active> files in I</sys/class/tty> starting with
+I</sys/class/tty/console/active> and "going up" as much as possible.
diff --git a/package/modes b/package/modes
index 7310234..fba6848 100644
--- a/package/modes
+++ b/package/modes
@@ -22,4 +22,5 @@ aa-stop                 0755
 aa-sync                 0755
 aa-terminate            0755
 aa-test                 0755
+aa-tty                  0755
 aa-umount               0755
diff --git a/package/targets.mak b/package/targets.mak
index ef9ec30..bee3147 100644
--- a/package/targets.mak
+++ b/package/targets.mak
@@ -17,6 +17,7 @@ aa-stop \
 aa-sync \
 aa-terminate \
 aa-test \
+aa-tty \
 aa-umount
 
 BIN_SCRIPTS_TARGET := \
@@ -55,6 +56,7 @@ aa-stop.1 \
 aa-sync.1 \
 aa-terminate.1 \
 aa-test.1 \
+aa-tty.1 \
 aa-umount.1
 
 ifdef DO_ALLSTATIC
diff --git a/src/utils/aa-tty.c b/src/utils/aa-tty.c
new file mode 100644
index 0000000..e2975b3
--- /dev/null
+++ b/src/utils/aa-tty.c
@@ -0,0 +1,87 @@
+/*
+ * anopa - Copyright (C) 2015 Olivier Brunel
+ *
+ * aa-tty.c
+ * Copyright (C) 2015 Olivier Brunel <jjk@jjacky.com>
+ *
+ * This file is part of anopa.
+ *
+ * anopa is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * anopa is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * anopa. If not, see http://www.gnu.org/licenses/
+ */
+
+#include <errno.h>
+#include <skalibs/bytestr.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/strerr2.h>
+#include <anopa/output.h>
+#include <anopa/common.h>
+
+#define PREFIX  "/sys/class/tty/"
+#define NAME    "/active"
+
+static void
+dieusage (int rc)
+{
+    aa_die_usage (rc, "",
+            " -h, --help                    Show this help screen and exit\n"
+            " -V, --version                 Show version information and exit\n"
+            );
+}
+
+int
+main (int argc, char * const argv[])
+{
+    PROG = "aa-tty";
+    char file[256];
+    int max = sizeof (file) - sizeof (PREFIX) - sizeof (NAME) + 1;
+    char name[max];
+    int r;
+
+    if (argc == 2)
+    {
+        if (str_equal (argv[1], "-h") || str_equal (argv[1], "--help"))
+            dieusage (0);
+        else if (str_equal (argv[1], "-V") || str_equal (argv[1], "--version"))
+            aa_die_version ();
+        else
+            dieusage (1);
+    }
+    else if (argc != 1)
+        dieusage (1);
+
+    byte_copy (file, sizeof (PREFIX) - 1, PREFIX);
+    byte_copy (file + sizeof (PREFIX) - 1, 7, "console");
+    byte_copy (file + sizeof (PREFIX) + 6, sizeof (NAME), NAME);
+    r = openreadnclose (file, name, max);
+    if (r <= 0)
+        strerr_diefu2sys (2, "read ", file);
+
+    for (;;)
+    {
+        byte_copy (file + sizeof (PREFIX) - 1, r, name);
+        byte_copy (file + sizeof (PREFIX) - 2 + r, sizeof (NAME), NAME);
+        r = openreadnclose (file, name, max);
+        if (r <= 0)
+        {
+            if (errno == ENOENT)
+            {
+                aa_bs_noflush (AA_OUT, "/dev/");
+                aa_bs_flush (AA_OUT, name);
+                return 0;
+            }
+            else
+                strerr_diefu2sys (2, "read ", file);
+        }
+    }
+}
diff --git a/src/utils/deps-exe/aa-tty b/src/utils/deps-exe/aa-tty
new file mode 100644
index 0000000..30987b4
--- /dev/null
+++ b/src/utils/deps-exe/aa-tty
@@ -0,0 +1,2 @@
+${LIBANOPA}
+-lskarnet