Back to index

glibc  2.9
getpass.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992-1999,2001,2003,2004,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdio.h>
00020 #include <stdio_ext.h>
00021 #include <string.h>         /* For string function builtin redirect.  */
00022 #include <termios.h>
00023 #include <unistd.h>
00024 
00025 #include <wchar.h>
00026 #define flockfile(s) _IO_flockfile (s)
00027 #define funlockfile(s) _IO_funlockfile (s)
00028 #include <bits/libc-lock.h>
00029 
00030 /* It is desirable to use this bit on systems that have it.
00031    The only bit of terminal state we want to twiddle is echoing, which is
00032    done in software; there is no need to change the state of the terminal
00033    hardware.  */
00034 
00035 #ifndef TCSASOFT
00036 #define TCSASOFT 0
00037 #endif
00038 
00039 static void
00040 call_fclose (void *arg)
00041 {
00042   if (arg != NULL)
00043     fclose (arg);
00044 }
00045 
00046 char *
00047 getpass (prompt)
00048      const char *prompt;
00049 {
00050   FILE *in, *out;
00051   struct termios s, t;
00052   int tty_changed;
00053   static char *buf;
00054   static size_t bufsize;
00055   ssize_t nread;
00056 
00057   /* Try to write to and read from the terminal if we can.
00058      If we can't open the terminal, use stderr and stdin.  */
00059 
00060   in = fopen ("/dev/tty", "w+c");
00061   if (in == NULL)
00062     {
00063       in = stdin;
00064       out = stderr;
00065     }
00066   else
00067     {
00068       /* We do the locking ourselves.  */
00069       __fsetlocking (in, FSETLOCKING_BYCALLER);
00070 
00071       out = in;
00072     }
00073 
00074   /* Make sure the stream we opened is closed even if the thread is
00075      canceled.  */
00076   __libc_cleanup_push (call_fclose, in == out ? in : NULL);
00077 
00078   flockfile (out);
00079 
00080   /* Turn echoing off if it is on now.  */
00081 
00082   if (__tcgetattr (fileno (in), &t) == 0)
00083     {
00084       /* Save the old one. */
00085       s = t;
00086       /* Tricky, tricky. */
00087       t.c_lflag &= ~(ECHO|ISIG);
00088       tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
00089     }
00090   else
00091     tty_changed = 0;
00092 
00093   /* Write the prompt.  */
00094   __fxprintf (out, "%s", prompt);
00095   fflush_unlocked (out);
00096 
00097   /* Read the password.  */
00098   nread = __getline (&buf, &bufsize, in);
00099   if (buf != NULL)
00100     {
00101       if (nread < 0)
00102        buf[0] = '\0';
00103       else if (buf[nread - 1] == '\n')
00104        {
00105          /* Remove the newline.  */
00106          buf[nread - 1] = '\0';
00107          if (tty_changed)
00108            /* Write the newline that was not echoed.  */
00109            __fxprintf (out, "\n");
00110        }
00111     }
00112 
00113   /* Restore the original setting.  */
00114   if (tty_changed)
00115     (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
00116 
00117   funlockfile (out);
00118 
00119   __libc_cleanup_pop (0);
00120 
00121   if (in != stdin)
00122     /* We opened the terminal; now close it.  */
00123     fclose (in);
00124 
00125   return buf;
00126 }