Blame manual/examples/mygetpass.c

Packit 6c4009
/* Reading passphrases manually.
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program 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
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#include <termios.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
Packit 6c4009
ssize_t
Packit 6c4009
my_getpass (char **lineptr, size_t *n, FILE *stream)
Packit 6c4009
{
Packit 6c4009
  struct termios old, new;
Packit 6c4009
  int nread;
Packit 6c4009
Packit 6c4009
  /* Turn echoing off and fail if we can't.  */
Packit 6c4009
  if (tcgetattr (fileno (stream), &old) != 0)
Packit 6c4009
    return -1;
Packit 6c4009
  new = old;
Packit 6c4009
  new.c_lflag &= ~ECHO;
Packit 6c4009
  if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Read the passphrase  */
Packit 6c4009
  nread = getline (lineptr, n, stream);
Packit 6c4009
Packit 6c4009
  /* Restore terminal.  */
Packit 6c4009
  (void) tcsetattr (fileno (stream), TCSAFLUSH, &old;;
Packit 6c4009
Packit 6c4009
  return nread;
Packit 6c4009
}