1 /* Copyright (C) 1992-1999,2001,2003,2004,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
24 #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
27 /* It is desirable to use this bit on systems that have it.
28 The only bit of terminal state we want to twiddle is echoing, which is
29 done in software; there is no need to change the state of the terminal
35 #define PWD_BUFFER_SIZE 256
37 char * getpass (const char *prompt
)
42 static char *buf
= NULL
;
46 buf
= (char *)__uc_malloc(PWD_BUFFER_SIZE
);
48 /* Try to write to and read from the terminal if we can.
49 If we can't open the terminal, use stderr and stdin. */
51 out
= in
= fopen ("/dev/tty", "r+");
59 /* Disable buffering for read/write FILE to prevent problems with
60 * fseek and buffering for read/write auto-transitioning. */
61 setvbuf(in
, NULL
, _IONBF
, 0);
64 /* Turn echoing off if it is on now. */
67 if (tcgetattr (fileno (in
), &t
) == 0)
69 /* Save the old one. */
72 t
.c_lflag
&= ~(ECHO
|ISIG
);
73 tty_changed
= (tcsetattr (fileno (in
), TCSAFLUSH
|TCSASOFT
, &t
) == 0);
76 /* Write the prompt. */
80 /* Read the password. */
81 if (!fgets (buf
, PWD_BUFFER_SIZE
, in
))
84 if (nread
> 0 && buf
[nread
- 1] == '\n')
85 /* Remove the newline. */
86 buf
[nread
- 1] = '\0';
90 /* Write the newline that was not echoed. */
92 /* Restore the original setting. */
93 (void) tcsetattr (fileno (in
), TCSAFLUSH
|TCSASOFT
, &s
);
97 /* We opened the terminal; now close it. */