* include/rpc/rpc.h: Declare thread variables with their correct
[glibc.git] / misc / getpass.c
blob52dab9439a3976b52403d43c98fbcd96f47e45d3
1 /* Copyright (C) 1992-1999, 2001, 2003 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <stdio.h>
20 #include <stdio_ext.h>
21 #include <termios.h>
22 #include <unistd.h>
24 #include <wchar.h>
25 #define flockfile(s) _IO_flockfile (s)
26 #define funlockfile(s) _IO_funlockfile (s)
27 #include <bits/libc-lock.h>
29 /* It is desirable to use this bit on systems that have it.
30 The only bit of terminal state we want to twiddle is echoing, which is
31 done in software; there is no need to change the state of the terminal
32 hardware. */
34 #ifndef TCSASOFT
35 #define TCSASOFT 0
36 #endif
38 static void
39 call_fclose (void *arg)
41 if (arg != NULL)
42 fclose (arg);
45 char *
46 getpass (prompt)
47 const char *prompt;
49 FILE *in, *out;
50 struct termios s, t;
51 int tty_changed;
52 static char *buf;
53 static size_t bufsize;
54 ssize_t nread;
56 /* Try to write to and read from the terminal if we can.
57 If we can't open the terminal, use stderr and stdin. */
59 in = fopen ("/dev/tty", "w+c");
60 if (in == NULL)
62 in = stdin;
63 out = stderr;
65 else
67 /* We do the locking ourselves. */
68 __fsetlocking (in, FSETLOCKING_BYCALLER);
70 out = in;
73 /* Make sure the stream we opened is closed even if the thread is
74 canceled. */
75 __libc_cleanup_push (call_fclose, in == out ? in : NULL);
77 flockfile (out);
79 /* Turn echoing off if it is on now. */
81 if (__tcgetattr (fileno (in), &t) == 0)
83 /* Save the old one. */
84 s = t;
85 /* Tricky, tricky. */
86 t.c_lflag &= ~(ECHO|ISIG);
87 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
89 else
90 tty_changed = 0;
92 /* Write the prompt. */
93 #ifdef USE_IN_LIBIO
94 if (_IO_fwide (out, 0) > 0)
95 __fwprintf (out, L"%s", prompt);
96 else
97 #endif
98 fputs_unlocked (prompt, out);
99 fflush_unlocked (out);
101 /* Read the password. */
102 nread = __getline (&buf, &bufsize, in);
103 if (buf != NULL)
105 if (nread < 0)
106 buf[0] = '\0';
107 else if (buf[nread - 1] == '\n')
109 /* Remove the newline. */
110 buf[nread - 1] = '\0';
111 if (tty_changed)
113 /* Write the newline that was not echoed. */
114 #ifdef USE_IN_LIBIO
115 if (_IO_fwide (out, 0) > 0)
116 putwc_unlocked (L'\n', out);
117 else
118 #endif
119 putc_unlocked ('\n', out);
124 /* Restore the original setting. */
125 if (tty_changed)
126 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
128 funlockfile (out);
130 __libc_cleanup_pop (0);
132 if (in != stdin)
133 /* We opened the terminal; now close it. */
134 fclose (in);
136 return buf;