(INLINE_SYSCALL): Don't mark asm input operand as clobbered.
[glibc.git] / misc / getpass.c
blob04ac12a76dcff25f6572384eea65dbd67031145a
1 /* Copyright (C) 1992,93,94,95,96,97,98,99,2001 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 #ifdef USE_IN_LIBIO
25 # define flockfile(s) _IO_flockfile (s)
26 # define funlockfile(s) _IO_funlockfile (s)
27 #endif
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 char *
39 getpass (prompt)
40 const char *prompt;
42 FILE *in, *out;
43 struct termios s, t;
44 int tty_changed;
45 static char *buf;
46 static size_t bufsize;
47 ssize_t nread;
49 /* Try to write to and read from the terminal if we can.
50 If we can't open the terminal, use stderr and stdin. */
52 in = fopen ("/dev/tty", "w+");
53 if (in == NULL)
55 in = stdin;
56 out = stderr;
58 else
60 /* We do the locking ourselves. */
61 __fsetlocking (in, FSETLOCKING_BYCALLER);
63 out = in;
66 flockfile (out);
68 /* Turn echoing off if it is on now. */
70 if (__tcgetattr (fileno (in), &t) == 0)
72 /* Save the old one. */
73 s = t;
74 /* Tricky, tricky. */
75 t.c_lflag &= ~(ECHO|ISIG);
76 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
78 else
79 tty_changed = 0;
81 /* Write the prompt. */
82 fputs_unlocked (prompt, out);
83 fflush_unlocked (out);
85 /* Read the password. */
86 nread = __getline (&buf, &bufsize, in);
87 if (buf != NULL)
89 if (nread < 0)
90 buf[0] = '\0';
91 else if (buf[nread - 1] == '\n')
93 /* Remove the newline. */
94 buf[nread - 1] = '\0';
95 if (tty_changed)
96 /* Write the newline that was not echoed. */
97 putc_unlocked ('\n', out);
101 /* Restore the original setting. */
102 if (tty_changed)
103 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
105 funlockfile (out);
107 if (in != stdin)
108 /* We opened the terminal; now close it. */
109 fclose (in);
111 return buf;