1 /* Copyright (C) 1992-2001, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 # define HAVE_STDIO_EXT_H 1
30 # include <stdio_ext.h>
32 # define __fsetlocking(stream, type) /* empty */
46 # define NOTCANCEL_MODE "c"
48 # define NOTCANCEL_MODE
52 # define flockfile(s) _IO_flockfile (s)
53 # define funlockfile(s) _IO_funlockfile (s)
55 # include "unlocked-io.h"
59 # include <bits/libc-lock.h>
61 # define __libc_cleanup_push(function, arg) /* empty */
62 # define __libc_cleanup_pop(execute) /* empty */
66 # define __getline getline
67 # define __tcgetattr tcgetattr
70 /* It is desirable to use this bit on systems that have it.
71 The only bit of terminal state we want to twiddle is echoing, which is
72 done in software; there is no need to change the state of the terminal
80 call_fclose (void *arg
)
87 getpass (const char *prompt
)
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+" NOTCANCEL_MODE
);
108 /* We do the locking ourselves. */
109 __fsetlocking (tty
, FSETLOCKING_BYCALLER
);
114 /* Make sure the stream we opened is closed even if the thread is
116 __libc_cleanup_push (call_fclose
, tty
);
120 /* Turn echoing off if it is on now. */
122 if (__tcgetattr (fileno (in
), &t
) == 0)
124 /* Save the old one. */
126 /* Tricky, tricky. */
127 t
.c_lflag
&= ~(ECHO
|ISIG
);
128 tty_changed
= (tcsetattr (fileno (in
), TCSAFLUSH
|TCSASOFT
, &t
) == 0);
133 /* Write the prompt. */
135 if (_IO_fwide (out
, 0) > 0)
136 __fwprintf (out
, L
"%s", prompt
);
139 fputs_unlocked (prompt
, out
);
140 fflush_unlocked (out
);
142 /* Read the password. */
143 nread
= __getline (&buf
, &bufsize
, in
);
146 /* As far as is known, glibc doesn't need this no-op fseek. */
148 /* According to the C standard, input may not be followed by output
149 on the same stream without an intervening call to a file
150 positioning function. Suppose in == out; then without this fseek
151 call, on Solaris, HP-UX, AIX, OSF/1, the previous input gets
152 echoed, whereas on IRIX, the following newline is not output as
153 it should be. POSIX imposes similar restrictions if fileno (in)
154 == fileno (out). The POSIX restrictions are tricky and change
155 from POSIX version to POSIX version, so play it safe and invoke
156 fseek even if in != out. */
157 fseek (out
, 0, SEEK_CUR
);
164 else if (buf
[nread
- 1] == '\n')
166 /* Remove the newline. */
167 buf
[nread
- 1] = '\0';
170 /* Write the newline that was not echoed. */
172 if (_IO_fwide (out
, 0) > 0)
173 putwc_unlocked (L
'\n', out
);
176 putc_unlocked ('\n', out
);
181 /* Restore the original setting. */
183 (void) tcsetattr (fileno (in
), TCSAFLUSH
|TCSASOFT
, &s
);
187 __libc_cleanup_pop (0);