1 /* Copyright (C) 1992-2001, 2003-2007, 2009-2019 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 <https://www.gnu.org/licenses/>. */
26 #if !(defined _WIN32 && !defined __CYGWIN__)
30 # if HAVE_DECL___FSETLOCKING && HAVE___FSETLOCKING
32 # include <stdio_ext.h>
35 # define __fsetlocking(stream, type) /* empty */
43 # include "unlocked-io.h"
45 # if !HAVE_DECL_FFLUSH_UNLOCKED
46 # undef fflush_unlocked
47 # define fflush_unlocked(x) fflush (x)
49 # if !HAVE_DECL_FLOCKFILE
51 # define flockfile(x) ((void) 0)
53 # if !HAVE_DECL_FUNLOCKFILE
55 # define funlockfile(x) ((void) 0)
57 # if !HAVE_DECL_FPUTS_UNLOCKED
58 # undef fputs_unlocked
59 # define fputs_unlocked(str,stream) fputs (str, stream)
61 # if !HAVE_DECL_PUTC_UNLOCKED
63 # define putc_unlocked(c,stream) putc (c, stream)
67 /* It is desirable to use this bit on systems that have it.
68 The only bit of terminal state we want to twiddle is echoing, which is
69 done in software; there is no need to change the state of the terminal
77 call_fclose (void *arg
)
84 getpass (const char *prompt
)
91 bool tty_changed
= false;
93 static size_t bufsize
;
96 /* Try to write to and read from the terminal if we can.
97 If we can't open the terminal, use stderr and stdin. */
99 tty
= fopen ("/dev/tty", "w+");
107 /* We do the locking ourselves. */
108 __fsetlocking (tty
, FSETLOCKING_BYCALLER
);
115 /* Turn echoing off if it is on now. */
117 if (tcgetattr (fileno (in
), &t
) == 0)
119 /* Save the old one. */
121 /* Tricky, tricky. */
122 t
.c_lflag
&= ~(ECHO
| ISIG
);
123 tty_changed
= (tcsetattr (fileno (in
), TCSAFLUSH
| TCSASOFT
, &t
) == 0);
127 /* Write the prompt. */
128 fputs_unlocked (prompt
, out
);
129 fflush_unlocked (out
);
131 /* Read the password. */
132 nread
= getline (&buf
, &bufsize
, in
);
134 /* According to the C standard, input may not be followed by output
135 on the same stream without an intervening call to a file
136 positioning function. Suppose in == out; then without this fseek
137 call, on Solaris, HP-UX, AIX, OSF/1, the previous input gets
138 echoed, whereas on IRIX, the following newline is not output as
139 it should be. POSIX imposes similar restrictions if fileno (in)
140 == fileno (out). The POSIX restrictions are tricky and change
141 from POSIX version to POSIX version, so play it safe and invoke
142 fseek even if in != out. */
143 fseeko (out
, 0, SEEK_CUR
);
149 else if (buf
[nread
- 1] == '\n')
151 /* Remove the newline. */
152 buf
[nread
- 1] = '\0';
155 /* Write the newline that was not echoed. */
156 putc_unlocked ('\n', out
);
161 /* Restore the original setting. */
164 tcsetattr (fileno (in
), TCSAFLUSH
| TCSASOFT
, &s
);
174 #else /* W32 native */
176 /* Windows implementation by Martin Lambers <marlam@marlam.de>,
177 improved by Simon Josefsson. */
187 # define PASS_MAX 512
191 getpass (const char *prompt
)
193 char getpassbuf
[PASS_MAX
+ 1];
199 fputs (prompt
, stderr
);
208 getpassbuf
[i
] = '\0';
211 else if (i
< PASS_MAX
)
218 getpassbuf
[i
] = '\0';
225 fputs ("\r\n", stderr
);
229 return strdup (getpassbuf
);