home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / shadow-2.pt3 / password.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  978b  |  53 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <termio.h>
  4. #include <fcntl.h>
  5.  
  6. /*
  7.  * password - prompt for password and return entry
  8.  *
  9.  *    Need to fake up getpass().  Returns TRUE if a password
  10.  *    was successfully input, and FALSE otherwise, including
  11.  *    EOF on input or ioctl() failure.  pass is not modified
  12.  *    on failure.
  13.  */
  14.  
  15. int    password (prompt, pass)
  16. char    *prompt;
  17. char    *pass;
  18. {
  19.     char    buf[BUFSIZ];
  20.     int    eof;
  21.     int    ttyopened = 0;
  22.     struct    termio    termio;
  23.     struct    termio    save;
  24.     FILE    *fp;
  25.  
  26.     if ((fp = fopen ("/dev/tty", "r")) == (FILE *) 0)
  27.         fp = stdin;
  28.     else
  29.         ttyopened = 1;
  30.  
  31.     if (ioctl (fileno (fp), TCGETA, &termio))
  32.         return (0);
  33.  
  34.     save = termio;
  35.     termio.c_lflag &= ~ECHO;
  36.     ioctl (fileno (fp), TCSETAF, &termio);
  37.  
  38.     fputs (prompt, stdout);
  39.     eof = gets (buf) == (char *) 0 || feof (fp) || ferror (fp);
  40.     putchar ('\n');
  41.  
  42.     ioctl (fileno (fp), TCSETAF, &save);
  43.  
  44.     if (! eof) {
  45.         buf[8] = '\0';
  46.         (void) strcpy (pass, buf);
  47.     }
  48.     if (ttyopened)
  49.         fclose (fp);
  50.  
  51.     return (! eof);
  52. }
  53.