#include "includes.h"
#ifdef REPLACE_GETPASS
#ifdef SYSV_TERMIO
static struct termio t;
#define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
#define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
#define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
#ifndef TCSAFLUSH
#define TCSAFLUSH 1
#endif
#ifndef TCSANOW
#define TCSANOW 0
#endif
static int tcgetattr(int fd, struct termio *t)
{
return ioctl(fd, TCGETA, t);
}
static int tcsetattr(int fd, int flags, struct termio *t)
{
if(flags & TCSAFLUSH)
ioctl(fd, TCFLSH, TCIOFLUSH);
return ioctl(fd, TCSETS, t);
}
#elif !defined(TCSAFLUSH)
static struct sgttyb t;
#define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
#define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
#define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
#define TCSAFLUSH 1
#define TCSANOW 0
static int tcgetattr(int fd, struct sgttyb *t)
{
return ioctl(fd, TIOCGETP, (char *)t);
}
static int tcsetattr(int fd, int flags, struct sgttyb *t)
{
return ioctl(fd, TIOCSETP, (char *)t);
}
#else
#define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
#define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
#define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
static struct termios t;
#endif
char *getsmbpass(char *prompt)
{
FILE *in, *out;
int echo_off;
static char buf[256];
static size_t bufsize = sizeof(buf);
size_t nread;
CatchSignal(SIGINT, SIGNAL_CAST SIG_IGN);
in = fopen ("/dev/tty", "w+");
if (in == NULL)
{
in = stdin;
out = stderr;
}
else
out = in;
setvbuf(in, NULL, _IONBF, 0);
if (tcgetattr (fileno (in), &t) == 0)
{
if (ECHO_IS_ON(t))
{
TURN_ECHO_OFF(t);
echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
TURN_ECHO_ON(t);
}
else
echo_off = 0;
}
else
echo_off = 0;
fputs (prompt, out);
fflush (out);
buf[0] = 0;
fgets(buf, bufsize, in);
nread = strlen(buf);
if (buf[nread - 1] == '\n')
buf[nread - 1] = '\0';
if (echo_off)
(void) tcsetattr (fileno (in), TCSANOW, &t);
if (in != stdin)
fclose (in);
CatchSignal(SIGINT, SIGNAL_CAST SIG_DFL);
printf("\n");
return buf;
}
#else
void getsmbpasswd_dummy(void);
void getsmbpasswd_dummy(void) {;}
#endif