1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
** Ask for a password from tty
22
** This is an own file to avoid conflicts with curses
24
#include <drizzled/global.h>
25
#include "libdrizzle.h"
26
#include "get_password.h"
34
#include <sys/ioctl.h>
35
#ifdef HAVE_TERMIOS_H /* For tty-password */
37
# define TERMIO struct termios
39
# ifdef HAVE_TERMIO_H /* For tty-password */
41
# define TERMIO struct termio
44
# define TERMIO struct sgttyb
49
Can't use fgets, because readline will get confused
50
length is max number of chars in to, not counting \0
51
to will not include the eol characters.
54
static void get_password(char *to, uint32_t length,int fd, bool echo)
56
char *pos=to,*end=to+length;
61
if (read(fd,&tmp,1) != 1)
63
if (tmp == '\b' || (int) tmp == 127)
69
fputs("\b \b",stdout);
76
if (tmp == '\n' || tmp == '\r' || tmp == 3)
78
if (iscntrl(tmp) || pos == end)
87
while (pos != to && isspace(pos[-1]) == ' ')
88
pos--; /* Allow dummy space at end */
94
char *drizzleclient_get_tty_password(const char *opt_message)
99
if (isatty(fileno(stdout)))
101
fputs(opt_message ? opt_message : "Enter password: ",stdout);
104
# if defined(HAVE_TERMIOS_H)
105
tcgetattr(fileno(stdin), &org);
107
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
110
tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
111
get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
112
tcsetattr(fileno(stdin), TCSADRAIN, &org);
113
# elif defined(HAVE_TERMIO_H)
114
ioctl(fileno(stdin), (int) TCGETA, &org);
116
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
119
ioctl(fileno(stdin),(int) TCSETA, &tmp);
120
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
121
ioctl(fileno(stdin),(int) TCSETA, &org);
123
gtty(fileno(stdin), &org);
125
tmp.sg_flags &= ~ECHO;
127
stty(fileno(stdin), &tmp);
128
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
129
stty(fileno(stdin), &org);
131
if (isatty(fileno(stdout)))