summaryrefslogtreecommitdiffstats
path: root/lib/ttyutils.c
diff options
context:
space:
mode:
authorKarel Zak2012-11-22 14:36:17 +0100
committerKarel Zak2012-11-22 14:40:14 +0100
commit1ef28920f802fd076280ce81efd299bd58ad0e17 (patch)
tree1ba5d3b1919d5e906e6ef486e5ccd6a418191ece /lib/ttyutils.c
parentlib/ttyutils: add test program (diff)
downloadkernel-qcow2-util-linux-1ef28920f802fd076280ce81efd299bd58ad0e17.tar.gz
kernel-qcow2-util-linux-1ef28920f802fd076280ce81efd299bd58ad0e17.tar.xz
kernel-qcow2-util-linux-1ef28920f802fd076280ce81efd299bd58ad0e17.zip
lib/ttyutils: add get_terminal_name()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/ttyutils.c')
-rw-r--r--lib/ttyutils.c45
1 files changed, 43 insertions, 2 deletions
diff --git a/lib/ttyutils.c b/lib/ttyutils.c
index 39d0516ca..15d538946 100644
--- a/lib/ttyutils.c
+++ b/lib/ttyutils.c
@@ -37,12 +37,53 @@ int get_terminal_width(void)
return 0;
}
+int get_terminal_name(const char **path,
+ const char **name,
+ const char **number)
+{
+ const char *tty;
+ const char *p;
+
+ if (name)
+ *name = NULL;
+ if (path)
+ *path = NULL;
+ if (number)
+ *number = NULL;
+
+ tty = ttyname(STDERR_FILENO);
+ if (!tty)
+ return -1;
+ if (path)
+ *path = tty;
+ tty = strncmp(tty, "/dev/", 5) == 0 ? tty + 5 : tty;
+ if (name)
+ *name = tty;
+ if (number) {
+ for (p = tty; p && *p; p++) {
+ if (isdigit(*p)) {
+ *number = p;
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+
#ifdef TEST_PROGRAM
# include <stdlib.h>
-
int main(void)
{
- fprintf(stderr, "tty width: %d\n", get_terminal_width());
+ const char *path, *name, *num;
+
+ if (get_terminal_name(&path, &name, &num) == 0) {
+ fprintf(stderr, "tty path: %s\n", path);
+ fprintf(stderr, "tty name: %s\n", name);
+ fprintf(stderr, "tty number: %s\n", num);
+ }
+ fprintf(stderr, "tty width: %d\n", get_terminal_width());
+
return EXIT_SUCCESS;
}
#endif