1 /* Copyright (C) 1992-2001, 2003-2007, 2009-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
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 along
16 with this program; if not, see <http://www.gnu.org/licenses/>. */
27 #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
31 # if HAVE_DECL___FSETLOCKING && HAVE___FSETLOCKING
33 # include <stdio_ext.h>
36 # define __fsetlocking(stream, type) /* empty */
44 # include "unlocked-io.h"
46 # if !HAVE_DECL_FFLUSH_UNLOCKED
47 # undef fflush_unlocked
48 # define fflush_unlocked(x) fflush (x)
50 # if !HAVE_DECL_FLOCKFILE
52 # define flockfile(x) ((void) 0)
54 # if !HAVE_DECL_FUNLOCKFILE
56 # define funlockfile(x) ((void) 0)
58 # if !HAVE_DECL_FPUTS_UNLOCKED
59 # undef fputs_unlocked
60 # define fputs_unlocked(str,stream) fputs (str, stream)
62 # if !HAVE_DECL_PUTC_UNLOCKED
64 # define putc_unlocked(c,stream) putc (c, stream)
68 /* It is desirable to use this bit on systems that have it.
69 The only bit of terminal state we want to twiddle is echoing, which is
70 done in software; there is no need to change the state of the terminal
78 call_fclose (void *arg
)
85 getpass (const char *prompt
)
92 bool tty_changed
= false;
94 static size_t bufsize
;
97 /* Try to write to and read from the terminal if we can.
98 If we can't open the terminal, use stderr and stdin. */
100 tty
= fopen ("/dev/tty", "w+");
108 /* We do the locking ourselves. */
109 __fsetlocking (tty
, FSETLOCKING_BYCALLER
);
116 /* Turn echoing off if it is on now. */
118 if (tcgetattr (fileno (in
), &t
) == 0)
120 /* Save the old one. */
122 /* Tricky, tricky. */
123 t
.c_lflag
&= ~(ECHO
| ISIG
);
124 tty_changed
= (tcsetattr (fileno (in
), TCSAFLUSH
| TCSASOFT
, &t
) == 0);
128 /* Write the prompt. */
129 fputs_unlocked (prompt
, out
);
130 fflush_unlocked (out
);
132 /* Read the password. */
133 nread
= getline (&buf
, &bufsize
, in
);
135 /* According to the C standard, input may not be followed by output
136 on the same stream without an intervening call to a file
137 positioning function. Suppose in == out; then without this fseek
138 call, on Solaris, HP-UX, AIX, OSF/1, the previous input gets
139 echoed, whereas on IRIX, the following newline is not output as
140 it should be. POSIX imposes similar restrictions if fileno (in)
141 == fileno (out). The POSIX restrictions are tricky and change
142 from POSIX version to POSIX version, so play it safe and invoke
143 fseek even if in != out. */
144 fseeko (out
, 0, SEEK_CUR
);
150 else if (buf
[nread
- 1] == '\n')
152 /* Remove the newline. */
153 buf
[nread
- 1] = '\0';
156 /* Write the newline that was not echoed. */
157 putc_unlocked ('\n', out
);
162 /* Restore the original setting. */
165 tcsetattr (fileno (in
), TCSAFLUSH
| TCSASOFT
, &s
);
175 #else /* W32 native */
177 /* Windows implementation by Martin Lambers <marlam@marlam.de>,
178 improved by Simon Josefsson. */
188 # define PASS_MAX 512
192 getpass (const char *prompt
)
194 char getpassbuf
[PASS_MAX
+ 1];
200 fputs (prompt
, stderr
);
209 getpassbuf
[i
] = '\0';
212 else if (i
< PASS_MAX
)
219 getpassbuf
[i
] = '\0';
226 fputs ("\r\n", stderr
);
230 return strdup (getpassbuf
);