/[inetutils]/inetutils/libinetutils/getpass.c
ViewVC logotype

Diff of /inetutils/libinetutils/getpass.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by alainm, Tue Dec 25 18:36:07 2001 UTC revision 1.2 by jbailey, Wed Dec 11 13:19:52 2002 UTC
# Line 1  Line 1 
1  /* GNU mailutils - a suite of utilities for electronic mail  /* Copyright (C) 1992,93,94,95,96,97,98,99,2000, 2001 Free Software Foundation, Inc.
2     Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.     This file is part of the GNU C Library.
3    
4     This program is free software; you can redistribute it and/or modify     The GNU C Library is free software; you can redistribute it and/or
5     it under the terms of the GNU General Library Public License as published by     modify it under the terms of the GNU Library General Public License as
6     the Free Software Foundation; either version 2, or (at your option)     published by the Free Software Foundation; either version 2 of the
7     any later version.     License, or (at your option) any later version.
8    
9     This program is distributed in the hope that it will be useful,     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     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     GNU Library General Public License for more details.     Library General Public License for more details.
13    
14     You should have received a copy of the GNU Library General Public License     You should have received a copy of the GNU Library General Public
15     along with this program; if not, write to the Free Software     License along with the GNU C Library; see the file COPYING.LIB.  If not,
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */     write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17       Boston, MA 02111-1307, USA.  */
 #include  <stdio.h>  
 #include  <string.h>  
 #include  <stdlib.h>  
 #include  <termios.h>  
18    
19  /* Alain: Parts originally from GNU Lib C.  */  #if HAVE_CONFIG_H
20    # include <config.h>
21    #endif
22    
23  static void  #include <stdio.h>
24  echo_off(int fd, struct termios *stored_settings)  #include <termios.h>
25  {  #include <unistd.h>
26    struct termios new_settings;  #include "getline.h"
27    tcgetattr (fd, stored_settings);  #include "unlocked-io.h"
28    new_settings = *stored_settings;  
29    new_settings.c_lflag &= (~ECHO);  /* It is desirable to use this bit on systems that have it.
30    tcsetattr (fd, TCSANOW, &new_settings);     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.  */
33    
34  static void  #ifndef TCSASOFT
35  echo_on(int fd, struct termios *stored_settings)  # define TCSASOFT 0
36  {  #endif
   tcsetattr (fd, TCSANOW, stored_settings);  
 }  
37    
38  char *  char *
39  getpass (const char * prompt)  getpass (const char *prompt)
40  {  {
41    FILE *in, *out;    FILE *in, *out;
42    struct termios stored_settings;    struct termios s, t;
43      int tty_changed;
44    static char *buf;    static char *buf;
45    static size_t buf_size;    static size_t bufsize;
46    char *pbuf;    ssize_t nread;
47    
48    /* First pass initialize the buffer.  */    /* Try to write to and read from the terminal if we can.
49    if (buf_size == 0)       If we can't open the terminal, use stderr and stdin.  */
50    
51      in = fopen ("/dev/tty", "w+");
52      if (in == NULL)
53      {      {
54        buf_size = 256;        in = stdin;
55        buf = calloc (1, buf_size);        out = stderr;
       if (buf == NULL)  
         return NULL;  
56      }      }
57    else    else
58      memset (buf, '\0', buf_size);      out = in;
59    
60    /* Turn echoing off if it is on now.  */    /* Turn echoing off if it is on now.  */
61    echo_off (fileno (stdin), &stored_settings);  
62      if (tcgetattr (fileno (in), &t) == 0)
63        {
64          /* Save the old one. */
65          s = t;
66          /* Tricky, tricky. */
67          t.c_lflag &= ~(ECHO|ISIG);
68          tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
69        }
70      else
71        tty_changed = 0;
72    
73    /* Write the prompt.  */    /* Write the prompt.  */
74    fputs (prompt, stdout);    fputs (prompt, out);
75    fflush (stdout);    fflush (out);
76    
77    /* Read the password.  */    /* Read the password.  */
78    pbuf = fgets (buf, buf_size, stdin);    nread = getline (&buf, &bufsize, in);
79    if (pbuf)    if (buf != NULL)
80      {      {
81        size_t nread = strlen (pbuf);        if (nread < 0)
82        if (nread && pbuf[nread - 1] == '\n')          buf[0] = '\0';
83          {        else if (buf[nread - 1] == '\n')
84            /* Remove the newline.  */          {
85            pbuf[nread - 1] = '\0';            /* Remove the newline.  */
86            /* Write the newline that was not echoed.  */            buf[nread - 1] = '\0';
87            putc ('\n', stdout);            if (tty_changed)
88          }              /* Write the newline that was not echoed.  */
89                putc ('\n', out);
90            }
91      }      }
92    
93    /* Restore the original setting.  */    /* Restore the original setting.  */
94    echo_on (fileno (stdin), &stored_settings);    if (tty_changed)
95        (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
96    
97    return pbuf;    if (in != stdin)
98  }      /* We opened the terminal; now close it.  */
99        fclose (in);
100    
101  #ifdef _GETPASS_STANDALONE_TEST    return buf;
   
 int  
 main ()  
 {  
   char *p;  
   p = getpass ("my prompt: ");  
   if (p)  
     printf ("Passwd: %s\n", p);  
   return 0;  
102  }  }
 #endif  

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26