8a993d3e059b5633974f10f011a83d1e5fd1a10a
[gnulib.git] / lib / getpass.c
1 /* Copyright (C) 1992-2001, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #if _LIBC
23 # define HAVE_STDIO_EXT_H 1
24 #endif
25
26 #include <stdbool.h>
27
28 #include <stdio.h>
29 #if HAVE_STDIO_EXT_H
30 # include <stdio_ext.h>
31 #else
32 # define __fsetlocking(stream, type) /* empty */
33 #endif
34 #if !_LIBC
35 # include "getline.h"
36 #endif
37
38 #include <termios.h>
39 #include <unistd.h>
40
41 #if _LIBC
42 # include <wchar.h>
43 #endif
44
45 #if _LIBC
46 # define NOTCANCEL_MODE "c"
47 #else
48 # define NOTCANCEL_MODE
49 #endif
50
51 #if _LIBC
52 # define flockfile(s) _IO_flockfile (s)
53 # define funlockfile(s) _IO_funlockfile (s)
54 #else
55 # include "unlocked-io.h"
56 #endif
57
58 #if _LIBC
59 # include <bits/libc-lock.h>
60 #else
61 # define __libc_cleanup_push(function, arg) /* empty */
62 # define __libc_cleanup_pop(execute) /* empty */
63 #endif
64
65 #if !_LIBC
66 # define __getline getline
67 # define __tcgetattr tcgetattr
68 #endif
69
70 /* It is desirable to use this bit on systems that have it.
71    The only bit of terminal state we want to twiddle is echoing, which is
72    done in software; there is no need to change the state of the terminal
73    hardware.  */
74
75 #ifndef TCSASOFT
76 # define TCSASOFT 0
77 #endif
78
79 static void
80 call_fclose (void *arg)
81 {
82   if (arg != NULL)
83     fclose (arg);
84 }
85
86 char *
87 getpass (const char *prompt)
88 {
89   FILE *tty;
90   FILE *in, *out;
91   struct termios s, t;
92   bool tty_changed;
93   static char *buf;
94   static size_t bufsize;
95   ssize_t nread;
96
97   /* Try to write to and read from the terminal if we can.
98      If we can't open the terminal, use stderr and stdin.  */
99
100   tty = fopen ("/dev/tty", "w+" NOTCANCEL_MODE);
101   if (tty == NULL)
102     {
103       in = stdin;
104       out = stderr;
105     }
106   else
107     {
108       /* We do the locking ourselves.  */
109       __fsetlocking (tty, FSETLOCKING_BYCALLER);
110
111       out = in = tty;
112     }
113
114   /* Make sure the stream we opened is closed even if the thread is
115      canceled.  */
116   __libc_cleanup_push (call_fclose, tty);
117
118   flockfile (out);
119
120   /* Turn echoing off if it is on now.  */
121
122   if (__tcgetattr (fileno (in), &t) == 0)
123     {
124       /* Save the old one. */
125       s = t;
126       /* Tricky, tricky. */
127       t.c_lflag &= ~(ECHO|ISIG);
128       tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
129     }
130   else
131     tty_changed = false;
132
133   /* Write the prompt.  */
134 #ifdef USE_IN_LIBIO
135   if (_IO_fwide (out, 0) > 0)
136     __fwprintf (out, L"%s", prompt);
137   else
138 #endif
139     fputs_unlocked (prompt, out);
140   fflush_unlocked (out);
141
142   /* Read the password.  */
143   nread = __getline (&buf, &bufsize, in);
144
145 #if !_LIBC
146   /* As far as is known, glibc doesn't need this no-op fseek.  */
147
148   /* According to the C standard, input may not be followed by output
149      on the same stream without an intervening call to a file
150      positioning function.  Suppose in == out; then without this fseek
151      call, on Solaris, HP-UX, AIX, OSF/1, the previous input gets
152      echoed, whereas on IRIX, the following newline is not output as
153      it should be.  POSIX imposes similar restrictions if fileno (in)
154      == fileno (out).  The POSIX restrictions are tricky and change
155      from POSIX version to POSIX version, so play it safe and invoke
156      fseek even if in != out.  */
157   fseek (out, 0, SEEK_CUR);
158 #endif
159
160   if (buf != NULL)
161     {
162       if (nread < 0)
163         buf[0] = '\0';
164       else if (buf[nread - 1] == '\n')
165         {
166           /* Remove the newline.  */
167           buf[nread - 1] = '\0';
168           if (tty_changed)
169             {
170               /* Write the newline that was not echoed.  */
171 #ifdef USE_IN_LIBIO
172               if (_IO_fwide (out, 0) > 0)
173                 putwc_unlocked (L'\n', out);
174               else
175 #endif
176                 putc_unlocked ('\n', out);
177             }
178         }
179     }
180
181   /* Restore the original setting.  */
182   if (tty_changed)
183     (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
184
185   funlockfile (out);
186
187   __libc_cleanup_pop (0);
188
189   call_fclose (tty);
190
191   return buf;
192 }