home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / radius_2.zip / getpass.c < prev    next >
C/C++ Source or Header  |  1996-05-28  |  3KB  |  100 lines

  1. /* Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  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. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <stdio.h>
  20. #include <termios.h>
  21. #include <unistd.h>
  22. #include <io.h>
  23. /*include <istream.h>*/
  24.  
  25. /* It is desireable to use this bit on systems that have it.
  26.    The only bit of terminal state we want to twiddle is echoing, which is
  27.    done in software; there is no need to change the state of the terminal
  28.    hardware.  */
  29.  
  30. #ifndef TCSASOFT
  31. #define TCSASOFT 0
  32. #endif
  33.  
  34. char *
  35. getpass (prompt)
  36.      const char *prompt;
  37. {
  38.   FILE *in, *out;
  39.   struct termios t;
  40.   int echo_off;
  41.   static char buf[20];
  42.   static size_t bufsize = 20;
  43.   ssize_t nread;
  44. /* char *s;*/
  45.  
  46.   /* Try to write to and read from the terminal if we can.
  47.      If we can't open the terminal, use stderr and stdin.  */
  48.  
  49.   in = fopen ("/dev/tty", "w+");
  50.   if (in == NULL)
  51.     {
  52.       in = stdin;
  53.       out = stderr;
  54.     }
  55.   else
  56.     out = in;
  57.  
  58.   /* Turn echoing off if it is on now.  */
  59.  
  60.   if (tcgetattr (fileno (in), &t) == 0)
  61.     {
  62.       if (t.c_lflag & ECHO)
  63.     {
  64.       t.c_lflag &= ~ECHO;
  65.       echo_off = tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0;
  66.       t.c_lflag |= ECHO;
  67.     }
  68.       else
  69.     echo_off = 0;
  70.     }
  71.   else
  72.     echo_off = 0;
  73.  
  74.   /* Write the prompt.  */
  75.   fputs (prompt, out);
  76.   fflush (out);
  77.  
  78.   /* Read the password.  */
  79.   //nread = __getline (&buf, &bufsize, in);
  80.   //nread = getline (&buf, &bufsize, in);
  81.   fgets(buf, bufsize, in);
  82.   if ( buf != NULL )
  83.       nread = strlen(buf);
  84.   if (nread < 0 && buf != NULL)
  85.     buf[0] = '\0';
  86.   else if (buf[nread - 1] == '\n')
  87.     /* Remove the newline.  */
  88.     buf[nread - 1] = '\0';
  89.  
  90.   /* Restore echoing.  */
  91.   if (echo_off)
  92.     (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t);
  93.  
  94.   if (in != stdin)
  95.     /* We opened the terminal; now close it.  */
  96.     fclose (in);
  97.  
  98.   return buf;
  99. }
  100.