1 /* Copyright (C) 1992-1999,2001,2003,2004,2005 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
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.
8 
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.
13 
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/>.  */
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <unistd.h>
22 
23 #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
24 
25 
26 /* It is desirable to use this bit on systems that have it.
27    The only bit of terminal state we want to twiddle is echoing, which is
28    done in software; there is no need to change the state of the terminal
29    hardware.  */
30 
31 #ifndef TCSASOFT
32 #define TCSASOFT 0
33 #endif
34 #define PWD_BUFFER_SIZE 256
35 
getpass(const char * prompt)36 char * getpass (const char *prompt)
37 {
38   FILE *in, *out;
39   struct termios s, t;
40   int tty_changed;
41   static char buf[PWD_BUFFER_SIZE];
42   int nread;
43 
44   /* Try to write to and read from the terminal if we can.
45      If we can't open the terminal, use stderr and stdin.  */
46 
47   out = in = fopen ("/dev/tty", "r+");
48   if (in == NULL)
49     {
50       in = stdin;
51       out = stderr;
52     }
53   else
54     {
55       /* Disable buffering for read/write FILE to prevent problems with
56        * fseek and buffering for read/write auto-transitioning. */
57       setvbuf(in, NULL, _IONBF, 0);
58     }
59 
60   /* Turn echoing off if it is on now.  */
61 
62   tty_changed = 0;
63   if (tcgetattr (fileno (in), &t) == 0)
64     {
65       /* Save the old one. */
66       s = t;
67       /* Tricky, tricky. */
68       t.c_lflag &= ~(ECHO|ISIG);
69       tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
70     }
71 
72   /* Write the prompt.  */
73   fputs(prompt, out);
74   fflush(out);
75 
76   /* Read the password.  */
77   if (!fgets (buf, sizeof(buf), in))
78     buf[0] = '\0';
79   nread = strlen(buf);
80   if (nread > 0 && buf[nread - 1] == '\n')
81       /* Remove the newline.  */
82       buf[nread - 1] = '\0';
83 
84   if (tty_changed)
85     {
86       /* Write the newline that was not echoed.  */
87       putc('\n', out);
88       /* Restore the original setting.  */
89       (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
90     }
91 
92   if (in != stdin)
93     /* We opened the terminal; now close it.  */
94     fclose (in);
95 
96   return buf;
97 }
98 #endif
99