]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/ttyutils: introduce get_terminal_stdfd()
authorKarel Zak <kzak@redhat.com>
Mon, 27 May 2019 10:49:12 +0000 (12:49 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 27 May 2019 10:49:12 +0000 (12:49 +0200)
Let's use separate function to detect what is the current terminal.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/pathnames.h
include/ttyutils.h
lib/ttyutils.c

index bd29b640a5abb76b28ab9f85d01e219d4c245440..ee470ee4b0dbb8ac3512322db45835ee7a586f1b 100644 (file)
@@ -95,6 +95,8 @@
 #define _PATH_PROC_GIDMAP      "/proc/self/gid_map"
 #define _PATH_PROC_SETGROUPS   "/proc/self/setgroups"
 
+#define _PATH_PROC_FDDIR       "/proc/self/fd"
+
 #define _PATH_PROC_ATTR_CURRENT        "/proc/self/attr/current"
 #define _PATH_PROC_ATTR_EXEC   "/proc/self/attr/exec"
 #define _PATH_PROC_CAPLASTCAP  "/proc/sys/kernel/cap_last_cap"
index af979159aee79dd65cb08b05523d1f18e3a8d2fe..f164a580fb4f24c004f93d5cd8e35bc4826a53c3 100644 (file)
@@ -81,6 +81,7 @@ struct chardata {
 extern int get_terminal_dimension(int *cols, int *lines);
 extern int get_terminal_width(int default_width);
 extern int get_terminal_type(const char **type);
+extern int get_terminal_stdfd(void);
 extern int get_terminal_name(const char **path, const char **name,
                             const char **number);
 
index 00a7903caba5ac88ed99382adaf98edcda0cbcd1..166e49e11a1e0e975aa02d441856e52617ce9960 100644 (file)
@@ -69,6 +69,18 @@ int get_terminal_width(int default_width)
        return width > 0 ? width : default_width;
 }
 
+int get_terminal_stdfd(void)
+{
+       if (isatty(STDIN_FILENO))
+               return STDIN_FILENO;
+       else if (isatty(STDOUT_FILENO))
+               return STDOUT_FILENO;
+       else if (isatty(STDERR_FILENO))
+               return STDERR_FILENO;
+
+       return -EINVAL;
+}
+
 int get_terminal_name(const char **path,
                      const char **name,
                      const char **number)
@@ -85,21 +97,18 @@ int get_terminal_name(const char **path,
        if (number)
                *number = NULL;
 
-       if (isatty(STDIN_FILENO))
-               fd = STDIN_FILENO;
-       else if (isatty(STDOUT_FILENO))
-               fd = STDOUT_FILENO;
-       else if (isatty(STDERR_FILENO))
-               fd = STDERR_FILENO;
-       else
-               return -1;
+       fd = get_terminal_stdfd();
+       if (fd < 0)
+               return fd;      /* error */
 
        tty = ttyname(fd);
        if (!tty)
                return -1;
+
        if (path)
                *path = tty;
-       tty = strncmp(tty, "/dev/", 5) == 0 ? tty + 5 : tty;
+       if (name || number)
+               tty = strncmp(tty, "/dev/", 5) == 0 ? tty + 5 : tty;
        if (name)
                *name = tty;
        if (number) {