6363#endif
6464
6565#if defined( _WIN32 ) || defined(OS2 )
66- /* were just going to fake it here and get input from the keyboard */
66+ /* {{{ statuc char* get_password */
67+ /*
68+ reads password from tty/console
69+
70+ SYNOPSIS
71+ get_password()
72+ buffer input buffer
73+ length length of input buffer
6774
68- char * get_tty_password (char * opt_message )
75+ DESCRIPTION
76+ reads a password from console (Windows) or tty without echoing
77+ it's characters. Input buffer must be allocated by calling function.
78+
79+ RETURNS
80+ buffer pointer to input buffer
81+ */
82+ char * get_tty_password (char * prompt , char * buffer , int length )
6983{
70- char to [80 ];
71- char * pos = to ,* end = to + sizeof (to )- 1 ;
72- int i = 0 ;
73- DBUG_ENTER ("get_tty_password" );
74- fprintf (stdout ,opt_message ? opt_message : "Enter password: " );
75- for (;;)
84+ #ifdef _WIN32
85+ DWORD SaveState ;
86+ HANDLE Hdl ;
87+ int Offset = 0 ;
88+ DWORD CharsProcessed = 0 ;
89+ char inChar ;
90+
91+ ZeroMemory (buffer , length );
92+
93+ if (!(Hdl = CreateFile ("CONIN$" ,
94+ GENERIC_READ | GENERIC_WRITE ,
95+ FILE_SHARE_READ ,
96+ NULL ,
97+ OPEN_EXISTING , 0 , NULL )))
7698 {
77- char tmp ;
78- tmp = _getch ();
79- if (tmp == '\b' || (int ) tmp == 127 )
80- {
81- if (pos != to )
99+ /* todo: provide a graphical dialog */
100+ return buffer ;
101+ }
102+ /* Save ConsoleMode and set ENABLE_PROCESSED_INPUT:
103+ CTRL+C is processed by the system and is not placed in the input buffer */
104+ GetConsoleMode (Hdl , & SaveState );
105+ SetConsoleMode (Hdl , ENABLE_PROCESSED_INPUT );
106+
107+ do
108+ {
109+ if (!ReadConsole (Hdl , & inChar , 1 , & CharsProcessed , NULL ) ||
110+ !CharsProcessed )
111+ break ;
112+
113+ switch (inChar ) {
114+ case '\b' : /* backslash */
115+ if (Offset )
82116 {
83- _cputs ("\b \b" );
84- pos -- ;
85- continue ;
117+ /* cursor is always at the end */
118+ Offset -- ;
119+ buffer [Offset ]= 0 ;
120+ _cputs ("\b \b" );
86121 }
87- }
88- if (tmp == '\n' || tmp == '\r' || tmp == 3 )
89122 break ;
90- if (iscntrl (tmp ) || pos == end )
91- continue ;
92- _cputs ("*" );
93- * (pos ++ ) = tmp ;
94- }
95- while (pos != to && isspace (pos [-1 ]) == ' ' )
96- pos -- ; /* Allow dummy space at end */
97- * pos = 0 ;
98- _cputs ("\n" );
99- DBUG_RETURN (my_strdup (to ,MYF (MY_FAE )));
100- }
123+ case '\n' :
124+ case '\r' :
125+ break ;
126+ default :
127+ buffer [Offset ]= inChar ;
128+ if (Offset < length - 2 )
129+ Offset ++ ;
130+ _cputs ("*" );
131+ break ;
132+ }
133+ } while (CharsProcessed && inChar != '\n' && inChar != '\r' );
134+ SetConsoleMode (Hdl , SaveState );
135+ CloseHandle (Hdl );
136+ return buffer ;
101137
102138#else
139+ #endif
140+ }
141+ /* }}} */
142+ #else
103143
104144
105145#ifndef HAVE_GETPASS
@@ -150,22 +190,21 @@ static void get_password(char *to,uint length,int fd,bool echo)
150190#endif /* ! HAVE_GETPASS */
151191
152192
153- char * get_tty_password (char * opt_message )
193+ char * get_tty_password (char * opt_message , char * buff , int bufflen )
154194{
155195#ifdef HAVE_GETPASS
156196 char * passbuff ;
157197#else /* ! HAVE_GETPASS */
158198 TERMIO org ,tmp ;
159199#endif /* HAVE_GETPASS */
160- char buff [80 ];
161200
162201 DBUG_ENTER ("get_tty_password" );
163202
164203#ifdef HAVE_GETPASS
165204 passbuff = getpass (opt_message ? opt_message : "Enter password: " );
166205
167206 /* copy the password to buff and clear original (static) buffer */
168- strnmov (buff , passbuff , sizeof ( buff ) - 1 );
207+ strnmov (buff , passbuff , bufflen - 1 );
169208#ifdef _PASSWORD_LEN
170209 memset (passbuff , 0 , _PASSWORD_LEN );
171210#endif
@@ -182,7 +221,7 @@ char *get_tty_password(char *opt_message)
182221 tmp .c_cc [VMIN ] = 1 ;
183222 tmp .c_cc [VTIME ] = 0 ;
184223 tcsetattr (fileno (stdin ), TCSADRAIN , & tmp );
185- get_password (buff , sizeof ( buff ) - 1 , fileno (stdin ), isatty (fileno (stdout )));
224+ get_password (buff , bufflen - 1 , fileno (stdin ), isatty (fileno (stdout )));
186225 tcsetattr (fileno (stdin ), TCSADRAIN , & org );
187226#elif defined(HAVE_TERMIO_H )
188227 ioctl (fileno (stdin ), (int ) TCGETA , & org );
@@ -191,21 +230,21 @@ char *get_tty_password(char *opt_message)
191230 tmp .c_cc [VMIN ] = 1 ;
192231 tmp .c_cc [VTIME ]= 0 ;
193232 ioctl (fileno (stdin ),(int ) TCSETA , & tmp );
194- get_password (buff ,sizeof ( buff ) - 1 ,fileno (stdin ),isatty (fileno (stdout )));
233+ get_password (buff ,bufflen - 1 ,fileno (stdin ),isatty (fileno (stdout )));
195234 ioctl (fileno (stdin ),(int ) TCSETA , & org );
196235#else
197236 gtty (fileno (stdin ), & org );
198237 tmp = org ;
199238 tmp .sg_flags &= ~ECHO ;
200239 tmp .sg_flags |= RAW ;
201240 stty (fileno (stdin ), & tmp );
202- get_password (buff ,sizeof ( buff ) - 1 ,fileno (stdin ),isatty (fileno (stdout )));
241+ get_password (buff ,bufflen - 1 ,fileno (stdin ),isatty (fileno (stdout )));
203242 stty (fileno (stdin ), & org );
204243#endif
205244 if (isatty (fileno (stdout )))
206245 fputc ('\n' ,stdout );
207246#endif /* HAVE_GETPASS */
208247
209- DBUG_RETURN (my_strdup ( buff , MYF ( MY_FAE )) );
248+ DBUG_RETURN (buff );
210249}
211250#endif /*_WIN32*/
0 commit comments