|
Packit |
6c4009 |
/* Copyright (C) 1992-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdio_ext.h>
|
|
Packit |
6c4009 |
#include <string.h> /* For string function builtin redirect. */
|
|
Packit |
6c4009 |
#include <termios.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <wchar.h>
|
|
Packit |
6c4009 |
#define flockfile(s) _IO_flockfile (s)
|
|
Packit |
6c4009 |
#define funlockfile(s) _IO_funlockfile (s)
|
|
Packit |
6c4009 |
#include <libc-lock.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* It is desirable to use this bit on systems that have it.
|
|
Packit |
6c4009 |
The only bit of terminal state we want to twiddle is echoing, which is
|
|
Packit |
6c4009 |
done in software; there is no need to change the state of the terminal
|
|
Packit |
6c4009 |
hardware. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef TCSASOFT
|
|
Packit |
6c4009 |
#define TCSASOFT 0
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
call_fclose (void *arg)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (arg != NULL)
|
|
Packit |
6c4009 |
fclose (arg);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
char *
|
|
Packit |
6c4009 |
getpass (const char *prompt)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
FILE *in, *out;
|
|
Packit |
6c4009 |
struct termios s, t;
|
|
Packit |
6c4009 |
int tty_changed;
|
|
Packit |
6c4009 |
static char *buf;
|
|
Packit |
6c4009 |
static size_t bufsize;
|
|
Packit |
6c4009 |
ssize_t nread;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Try to write to and read from the terminal if we can.
|
|
Packit |
6c4009 |
If we can't open the terminal, use stderr and stdin. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
in = fopen ("/dev/tty", "w+ce");
|
|
Packit |
6c4009 |
if (in == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
in = stdin;
|
|
Packit |
6c4009 |
out = stderr;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We do the locking ourselves. */
|
|
Packit |
6c4009 |
__fsetlocking (in, FSETLOCKING_BYCALLER);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
out = in;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Make sure the stream we opened is closed even if the thread is
|
|
Packit |
6c4009 |
canceled. */
|
|
Packit |
6c4009 |
__libc_cleanup_push (call_fclose, in == out ? in : NULL);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
flockfile (out);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Turn echoing off if it is on now. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__tcgetattr (fileno (in), &t) == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Save the old one. */
|
|
Packit |
6c4009 |
s = t;
|
|
Packit |
6c4009 |
/* Tricky, tricky. */
|
|
Packit |
6c4009 |
t.c_lflag &= ~(ECHO|ISIG);
|
|
Packit |
6c4009 |
tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
tty_changed = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Write the prompt. */
|
|
Packit |
6c4009 |
__fxprintf (out, "%s", prompt);
|
|
Packit |
6c4009 |
__fflush_unlocked (out);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Read the password. */
|
|
Packit |
6c4009 |
nread = __getline (&buf, &bufsize, in);
|
|
Packit |
6c4009 |
if (buf != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (nread < 0)
|
|
Packit |
6c4009 |
buf[0] = '\0';
|
|
Packit |
6c4009 |
else if (buf[nread - 1] == '\n')
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Remove the newline. */
|
|
Packit |
6c4009 |
buf[nread - 1] = '\0';
|
|
Packit |
6c4009 |
if (tty_changed)
|
|
Packit |
6c4009 |
/* Write the newline that was not echoed. */
|
|
Packit |
6c4009 |
__fxprintf (out, "\n");
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Restore the original setting. */
|
|
Packit |
6c4009 |
if (tty_changed)
|
|
Packit |
6c4009 |
(void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
funlockfile (out);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__libc_cleanup_pop (0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (in != stdin)
|
|
Packit |
6c4009 |
/* We opened the terminal; now close it. */
|
|
Packit |
6c4009 |
fclose (in);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return buf;
|
|
Packit |
6c4009 |
}
|