home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tnlogin.zip / getpass.c < prev    next >
C/C++ Source or Header  |  1994-03-28  |  1KB  |  58 lines

  1. /* getpass.c */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5.  
  6. #define _PASSWORD_LEN 128
  7.  
  8. /* Issue prompt and read reply with echo turned off */
  9. char *getpass (const char *prompt)
  10. {
  11.   static char pbuf[_PASSWORD_LEN+1];
  12.   int c, i;
  13.  
  14.   fputs (prompt, stderr);
  15.   fflush (stderr);
  16.   i = 0;
  17.  
  18.   for (;;)
  19.   {
  20.     c = getch ();
  21.     if (c == '\r' || c == '\n')
  22.       break;
  23.     if (c == '\b' || c == 127)
  24.     {
  25.       if (i > 0) --i;
  26.     }
  27.     else if (c == 0x15 || c == 0x1b)
  28.       i = 0;
  29.     else if (i < sizeof (pbuf)-1)
  30.       pbuf[i++] = (char)c;
  31.   }
  32.  
  33.   pbuf[i] = 0;
  34.   fputs ("\r\n", stderr);
  35.   fflush (stderr);
  36.  
  37.   return pbuf;
  38. }
  39.  
  40. /* Issue prompt and read reply with echo turned on */
  41. char *getline(const char *prompt)
  42. {
  43.   static char buffer[256];
  44.   int c;
  45.  
  46.   fputs(prompt, stdout);
  47.   fflush(stdout);
  48.   fgets(buffer, sizeof(buffer), stdin);
  49.   c = strlen(buffer);
  50.  
  51.   if (buffer[c - 1] == '\n')
  52.     buffer[c - 1] = 0;
  53.  
  54.   return buffer;
  55. }
  56.  
  57. /* end of getpass.c */
  58.