]> git.pond.sub.org Git - empserver/blobdiff - src/client/getpass.c
client: Make get_password() not echo with POSIX && !HAVE_GETPASS
[empserver] / src / client / getpass.c
index 517e37740bf27ba6370015de44f8cdca433dab8f..b04ec7e504cf66b6e3b81de123e162347bc8d46f 100644 (file)
 #include <string.h>
 #ifdef _WIN32
 #include <windows.h>
+#else
+#include <termios.h>
 #endif
 #endif
 #include "misc.h"
 
+#ifndef HAVE_GETPASS
+static int
+set_echo_if_tty(int on)
+{
+#ifdef _WIN32
+    DWORD mode;
+    HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
+
+    if (!GetConsoleMode(input_handle, &mode))
+       return 0;
+
+    if (on)
+       mode |= ENABLE_ECHO_INPUT;
+    else
+       mode &= ~ENABLE_ECHO_INPUT;
+
+    if (!SetConsoleMode(input_handle, mode))
+       return -1;
+    return 1;
+#else
+    struct termios tcattr;
+
+    if (tcgetattr(0, &tcattr) < 0)
+       return 0;
+
+    if (on)
+       tcattr.c_lflag |= ECHO;
+    else
+       tcattr.c_lflag &= ~ECHO;
+
+    if (tcsetattr(0, TCSAFLUSH, &tcattr) < 0)
+       return -1;
+    return 1;
+#endif
+}
+#endif /* !HAVE_GETPASS */
+
 char *
 get_password(const char *prompt)
 {
@@ -51,22 +90,19 @@ get_password(const char *prompt)
     static char buf[128];
     char *p;
     size_t len;
-#ifdef _WIN32
-    DWORD mode;
-    HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
+    int echo_set;
 
-    if (GetConsoleMode(input_handle, &mode))
-       SetConsoleMode(input_handle, mode & ~ENABLE_ECHO_INPUT);
-    else
-#endif
+    echo_set = set_echo_if_tty(0);
+    if (echo_set <= 0)
        printf("Note: your input is echoed to the screen\n");
+
     printf("%s", prompt);
     fflush(stdout);
     p = fgets(buf, sizeof(buf), stdin);
-#ifdef _WIN32
-    if (GetConsoleMode(input_handle, &mode))
-       SetConsoleMode(input_handle, mode | ENABLE_ECHO_INPUT);
-#endif
+
+    if (echo_set > 0)
+       set_echo_if_tty(1);
+
     if (!p)
        return NULL;
     len = strlen(p);