getpass: don't suck in stdio
authorAlan Cox <alan@linux.intel.com>
Sat, 27 Dec 2014 15:48:46 +0000 (15:48 +0000)
committerAlan Cox <alan@linux.intel.com>
Sat, 27 Dec 2014 15:48:46 +0000 (15:48 +0000)
Library/libs/getpass.c

index 07cc276..0b24fe9 100644 (file)
@@ -2,10 +2,18 @@
  */
 #include <unistd.h>
 #include <stdlib.h>
-#include <stdio.h>
+#include <string.h>
 #include <termios.h>
 #include <sys/ioctl.h>
 
+#define EOF    (-1)
+
+static int _getchar(void)
+{
+       static char ch;
+       return (read(0, &ch, 1) == 1) ? ch : EOF;
+}
+
 static char *_gets(char *buf, int len)
 {
        int ch, i = 0;
@@ -13,16 +21,6 @@ static char *_gets(char *buf, int len)
        while (i < len) {
                if ((ch = _getchar()) == EOF && i == 0)
                        return NULL;
-#if 0
-               if (ch >= ' ')
-                       _putchar(ch);
-               else {
-                       _putchar('^');
-                       _putchar(ch + '@');
-               }
-#endif
-               if ((ch == 'C' & 037) || (ch == 'Z' & 037))
-                       return NULL;
                if (ch == '\n' || ch == '\r')
                        break;
                buf[i++] = ch;
@@ -40,8 +38,7 @@ char *getpass(char *prompt)
        int tv;
 
        /* display the prompt */
-       fputs(prompt, stdout);
-       fflush(stdout);
+       write(2, prompt, strlen(prompt));
 
        tv = tcgetattr(0, &t);
        ol = t.c_lflag;