1 /* Copyright (C) 1992-2001, 2003, 2004 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. */
27 # define HAVE_STDIO_EXT_H 1
34 # include <stdio_ext.h>
36 # define __fsetlocking(stream, type) /* empty */
50 # define NOTCANCEL_MODE "c"
52 # define NOTCANCEL_MODE
56 # define flockfile(s) _IO_flockfile (s)
57 # define funlockfile(s) _IO_funlockfile (s)
59 # include "unlocked-io.h"
61 # undef fflush_unlocked
62 # define fflush_unlocked(x) fflush (x)
64 # define flockfile(x) ((void) 0)
66 # define funlockfile(x) ((void) 0)
67 # undef fputs_unlocked
68 # define fputs_unlocked(str,stream) fputs (str, stream)
70 # define putc_unlocked(c,stream) putc (c, stream)
74 # include <bits/libc-lock.h>
76 # define __libc_cleanup_push(function, arg) /* empty */
77 # define __libc_cleanup_pop(execute) /* empty */
81 # define __getline getline
82 # define __tcgetattr tcgetattr
85 /* It is desirable to use this bit on systems that have it.
86 The only bit of terminal state we want to twiddle is echoing, which is
87 done in software; there is no need to change the state of the terminal
95 call_fclose (void *arg)
102 getpass (const char *prompt)
109 static size_t bufsize;
112 /* Try to write to and read from the terminal if we can.
113 If we can't open the terminal, use stderr and stdin. */
115 tty = fopen ("/dev/tty", "w+" NOTCANCEL_MODE);
123 /* We do the locking ourselves. */
124 __fsetlocking (tty, FSETLOCKING_BYCALLER);
129 /* Make sure the stream we opened is closed even if the thread is
131 __libc_cleanup_push (call_fclose, tty);
135 /* Turn echoing off if it is on now. */
137 if (__tcgetattr (fileno (in), &t) == 0)
139 /* Save the old one. */
141 /* Tricky, tricky. */
142 t.c_lflag &= ~(ECHO|ISIG);
143 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
148 /* Write the prompt. */
150 if (_IO_fwide (out, 0) > 0)
151 __fwprintf (out, L"%s", prompt);
154 fputs_unlocked (prompt, out);
155 fflush_unlocked (out);
157 /* Read the password. */
158 nread = __getline (&buf, &bufsize, in);
161 /* As far as is known, glibc doesn't need this no-op fseek. */
163 /* According to the C standard, input may not be followed by output
164 on the same stream without an intervening call to a file
165 positioning function. Suppose in == out; then without this fseek
166 call, on Solaris, HP-UX, AIX, OSF/1, the previous input gets
167 echoed, whereas on IRIX, the following newline is not output as
168 it should be. POSIX imposes similar restrictions if fileno (in)
169 == fileno (out). The POSIX restrictions are tricky and change
170 from POSIX version to POSIX version, so play it safe and invoke
171 fseek even if in != out. */
172 fseek (out, 0, SEEK_CUR);
179 else if (buf[nread - 1] == '\n')
181 /* Remove the newline. */
182 buf[nread - 1] = '\0';
185 /* Write the newline that was not echoed. */
187 if (_IO_fwide (out, 0) > 0)
188 putwc_unlocked (L'\n', out);
191 putc_unlocked ('\n', out);
196 /* Restore the original setting. */
198 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
202 __libc_cleanup_pop (0);