use tcsetattr only if stdin is a tty

Mon, 09 Aug 2021 19:01:02 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 09 Aug 2021 19:01:02 +0200
changeset 737
1c75c0498520
parent 736
40be8db6fe45
child 738
b62ea7f31593

use tcsetattr only if stdin is a tty

libidav/utils.c file | annotate | diff | comparison | revisions
--- a/libidav/utils.c	Mon Aug 09 17:22:21 2021 +0200
+++ b/libidav/utils.c	Mon Aug 09 19:01:02 2021 +0200
@@ -1084,13 +1084,16 @@
 #ifndef _WIN32
     // hide terminal input
     struct termios oflags, nflags;
-    tcgetattr(fileno(stdin), &oflags);
-    nflags = oflags;
-    nflags.c_lflag &= ~ECHO;
-    nflags.c_lflag |= ECHONL;
-    if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
-        perror("tcsetattr");
+    if(isatty(fileno(stdin))) {
+        tcgetattr(fileno(stdin), &oflags);
+        nflags = oflags;
+        nflags.c_lflag &= ~ECHO;
+        nflags.c_lflag |= ECHONL;
+        if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
+            perror("tcsetattr");
+        }
     }
+    
 #endif
     
     // read password input
@@ -1107,7 +1110,7 @@
     
 #ifndef _WIN32
     // restore terminal settings
-    if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
+    if (isatty(fileno(stdin)) && tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
         perror("tcsetattr");
     }
 #endif

mercurial