libdl: first execute all destructors, then munmap library
[uclibc-ng.git] / libc / unistd / getpass.c
blobc46d3d442026c6431f0279748897b6ecf55f272c
1 /* Copyright (C) 1992-1999,2001,2003,2004,2005 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <stdio.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <unistd.h>
22 #include <malloc.h>
24 #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
27 /* It is desirable to use this bit on systems that have it.
28 The only bit of terminal state we want to twiddle is echoing, which is
29 done in software; there is no need to change the state of the terminal
30 hardware. */
32 #ifndef TCSASOFT
33 #define TCSASOFT 0
34 #endif
35 #define PWD_BUFFER_SIZE 256
37 char * getpass (const char *prompt)
39 FILE *in, *out;
40 struct termios s, t;
41 int tty_changed;
42 static char *buf = NULL;
43 int nread;
45 if (buf == NULL)
46 buf = (char *)__uc_malloc(PWD_BUFFER_SIZE);
48 /* Try to write to and read from the terminal if we can.
49 If we can't open the terminal, use stderr and stdin. */
51 out = in = fopen ("/dev/tty", "r+");
52 if (in == NULL)
54 in = stdin;
55 out = stderr;
57 else
59 /* Disable buffering for read/write FILE to prevent problems with
60 * fseek and buffering for read/write auto-transitioning. */
61 setvbuf(in, NULL, _IONBF, 0);
64 /* Turn echoing off if it is on now. */
66 tty_changed = 0;
67 if (tcgetattr (fileno (in), &t) == 0)
69 /* Save the old one. */
70 s = t;
71 /* Tricky, tricky. */
72 t.c_lflag &= ~(ECHO|ISIG);
73 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
76 /* Write the prompt. */
77 fputs(prompt, out);
78 fflush(out);
80 /* Read the password. */
81 if (!fgets (buf, PWD_BUFFER_SIZE, in))
82 buf[0] = '\0';
83 nread = strlen(buf);
84 if (nread > 0 && buf[nread - 1] == '\n')
85 /* Remove the newline. */
86 buf[nread - 1] = '\0';
88 if (tty_changed)
90 /* Write the newline that was not echoed. */
91 putc('\n', out);
92 /* Restore the original setting. */
93 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
96 if (in != stdin)
97 /* We opened the terminal; now close it. */
98 fclose (in);
100 return buf;
102 #endif