home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / pcnfsd2.zip / getpass.c next >
C/C++ Source or Header  |  1994-03-16  |  664b  |  34 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #define _PASSWORD_LEN 128
  5.  
  6. /* Issue prompt and read reply with echo turned off */
  7. char *getpass (const char *prompt)
  8. {
  9.   static char pbuf[_PASSWORD_LEN+1];
  10.   int c, i;
  11.  
  12.   fputs (prompt, stderr);
  13.   fflush (stderr);
  14.   i = 0;
  15.   for (;;)
  16.     {
  17.       c = getch ();
  18.       if (c == '\r' || c == '\n')
  19.         break;
  20.       if (c == '\b' || c == 127)
  21.         {
  22.           if (i > 0) --i;
  23.         }
  24.       else if (c == 0x15 || c == 0x1b)
  25.         i = 0;
  26.       else if (i < sizeof (pbuf)-1)
  27.         pbuf[i++] = (char)c;
  28.     }
  29.   pbuf[i] = 0;
  30.   fputs ("\r\n", stderr);
  31.   fflush (stderr);
  32.   return (pbuf);
  33. }
  34.