20 #include <my_global.h>
25 #include <mysql/get_password.h>
27 #if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
37 #include <sys/ioctl.h>
40 #define TERMIO struct termios
44 #define TERMIO struct termio
47 #define TERMIO struct sgttyb
50 #ifdef alpha_linux_port
51 #include <asm/ioctls.h>
52 #include <asm/termiobits.h>
59 #ifdef HAVE_GETPASSPHRASE
60 #define getpass(A) getpassphrase(A)
67 char *get_tty_password_ext(
const char *opt_message,
68 strdup_handler_t strdup_function)
71 char *pos=
to,*end=to+
sizeof(
to)-1;
73 DBUG_ENTER(
"get_tty_password_ext");
74 _cputs(opt_message ? opt_message :
"Enter password: ");
79 if (tmp ==
'\b' || (
int) tmp == 127)
88 if (tmp ==
'\n' || tmp ==
'\r' || tmp == 3)
90 if (iscntrl(tmp) || pos == end)
95 while (pos != to && isspace(pos[-1]) ==
' ')
99 DBUG_RETURN(strdup_function(to,MYF(MY_FAE)));
112 static void get_password(
char *to,uint length,
int fd, my_bool echo)
114 char *pos=
to,*end=to+length;
119 if (my_read(fd,&tmp,1,MYF(0)) != 1)
121 if (tmp ==
'\b' || (
int) tmp == 127)
127 fputs(
"\b \b",stderr);
134 if (tmp ==
'\n' || tmp ==
'\r' || tmp == 3)
136 if (iscntrl(tmp) || pos == end)
145 while (pos != to && isspace(pos[-1]) ==
' ')
154 char *get_tty_password_ext(
const char *opt_message,
155 strdup_handler_t strdup_function)
164 DBUG_ENTER(
"get_tty_password_ext");
167 passbuff = getpass(opt_message ? opt_message :
"Enter password: ");
170 strnmov(buff, passbuff,
sizeof(buff) - 1);
172 memset(passbuff, 0, _PASSWORD_LEN);
175 if (isatty(fileno(stderr)))
177 fputs(opt_message ? opt_message :
"Enter password: ",stderr);
180 #if defined(HAVE_TERMIOS_H)
181 tcgetattr(fileno(stdin), &org);
183 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
186 tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
187 get_password(buff,
sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
188 tcsetattr(fileno(stdin), TCSADRAIN, &org);
189 #elif defined(HAVE_TERMIO_H)
190 ioctl(fileno(stdin), (
int) TCGETA, &org);
192 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
195 ioctl(fileno(stdin),(
int) TCSETA, &tmp);
196 get_password(buff,
sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
197 ioctl(fileno(stdin),(
int) TCSETA, &org);
199 gtty(fileno(stdin), &org);
201 tmp.sg_flags &= ~ECHO;
203 stty(fileno(stdin), &tmp);
204 get_password(buff,
sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
205 stty(fileno(stdin), &org);
207 if (isatty(fileno(stderr)))
211 DBUG_RETURN(strdup_function(buff,MYF(MY_FAE)));
216 char *get_tty_password(
const char *opt_message)
218 return get_tty_password_ext(opt_message, my_strdup);