* man/Makefile (SOURCES): Remove all man pages but
[glibc.git] / misc / getpass.c
blob62e56866a13078646816724e703c7699cc98fe4b
1 /* Copyright (C) 1992-1999, 2001, 2003, 2004 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 <string.h> /* For string function builtin redirect. */
22 #include <termios.h>
23 #include <unistd.h>
25 #include <wchar.h>
26 #define flockfile(s) _IO_flockfile (s)
27 #define funlockfile(s) _IO_funlockfile (s)
28 #include <bits/libc-lock.h>
30 /* It is desirable to use this bit on systems that have it.
31 The only bit of terminal state we want to twiddle is echoing, which is
32 done in software; there is no need to change the state of the terminal
33 hardware. */
35 #ifndef TCSASOFT
36 #define TCSASOFT 0
37 #endif
39 static void
40 call_fclose (void *arg)
42 if (arg != NULL)
43 fclose (arg);
46 char *
47 getpass (prompt)
48 const char *prompt;
50 FILE *in, *out;
51 struct termios s, t;
52 int tty_changed;
53 static char *buf;
54 static size_t bufsize;
55 ssize_t nread;
57 /* Try to write to and read from the terminal if we can.
58 If we can't open the terminal, use stderr and stdin. */
60 in = fopen ("/dev/tty", "w+c");
61 if (in == NULL)
63 in = stdin;
64 out = stderr;
66 else
68 /* We do the locking ourselves. */
69 __fsetlocking (in, FSETLOCKING_BYCALLER);
71 out = in;
74 /* Make sure the stream we opened is closed even if the thread is
75 canceled. */
76 __libc_cleanup_push (call_fclose, in == out ? in : NULL);
78 flockfile (out);
80 /* Turn echoing off if it is on now. */
82 if (__tcgetattr (fileno (in), &t) == 0)
84 /* Save the old one. */
85 s = t;
86 /* Tricky, tricky. */
87 t.c_lflag &= ~(ECHO|ISIG);
88 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
90 else
91 tty_changed = 0;
93 /* Write the prompt. */
94 #ifdef USE_IN_LIBIO
95 if (_IO_fwide (out, 0) > 0)
96 __fwprintf (out, L"%s", prompt);
97 else
98 #endif
99 fputs_unlocked (prompt, out);
100 fflush_unlocked (out);
102 /* Read the password. */
103 nread = __getline (&buf, &bufsize, in);
104 if (buf != NULL)
106 if (nread < 0)
107 buf[0] = '\0';
108 else if (buf[nread - 1] == '\n')
110 /* Remove the newline. */
111 buf[nread - 1] = '\0';
112 if (tty_changed)
114 /* Write the newline that was not echoed. */
115 #ifdef USE_IN_LIBIO
116 if (_IO_fwide (out, 0) > 0)
117 putwc_unlocked (L'\n', out);
118 else
119 #endif
120 putc_unlocked ('\n', out);
125 /* Restore the original setting. */
126 if (tty_changed)
127 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
129 funlockfile (out);
131 __libc_cleanup_pop (0);
133 if (in != stdin)
134 /* We opened the terminal; now close it. */
135 fclose (in);
137 return buf;