(sem_timedwait): Fix a typo.
[glibc/pb-stable.git] / misc / getpass.c
blobe5483f9e50c2b26bb2afcd550de6a164ac2cc00a
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 # include <wchar.h>
26 # define flockfile(s) _IO_flockfile (s)
27 # define funlockfile(s) _IO_funlockfile (s)
28 #endif
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 char *
40 getpass (prompt)
41 const char *prompt;
43 FILE *in, *out;
44 struct termios s, t;
45 int tty_changed;
46 static char *buf;
47 static size_t bufsize;
48 ssize_t nread;
50 /* Try to write to and read from the terminal if we can.
51 If we can't open the terminal, use stderr and stdin. */
53 in = fopen ("/dev/tty", "w+");
54 if (in == NULL)
56 in = stdin;
57 out = stderr;
59 else
61 /* We do the locking ourselves. */
62 __fsetlocking (in, FSETLOCKING_BYCALLER);
64 out = in;
67 flockfile (out);
69 /* Turn echoing off if it is on now. */
71 if (__tcgetattr (fileno (in), &t) == 0)
73 /* Save the old one. */
74 s = t;
75 /* Tricky, tricky. */
76 t.c_lflag &= ~(ECHO|ISIG);
77 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
79 else
80 tty_changed = 0;
82 /* Write the prompt. */
83 #ifdef USE_IN_LIBIO
84 if (_IO_fwide (out, 0) > 0)
85 __fwprintf (out, L"%s", prompt);
86 else
87 #endif
88 fputs_unlocked (prompt, out);
89 fflush_unlocked (out);
91 /* Read the password. */
92 nread = __getline (&buf, &bufsize, in);
93 if (buf != NULL)
95 if (nread < 0)
96 buf[0] = '\0';
97 else if (buf[nread - 1] == '\n')
99 /* Remove the newline. */
100 buf[nread - 1] = '\0';
101 if (tty_changed)
103 /* Write the newline that was not echoed. */
104 #ifdef USE_IN_LIBIO
105 if (_IO_fwide (out, 0) > 0)
106 putwc_unlocked (L'\n', out);
107 else
108 #endif
109 putc_unlocked ('\n', out);
114 /* Restore the original setting. */
115 if (tty_changed)
116 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
118 funlockfile (out);
120 if (in != stdin)
121 /* We opened the terminal; now close it. */
122 fclose (in);
124 return buf;