home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / GETPASS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.4 KB  |  83 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - getpass.c
  3.  *
  4.  * function(s)
  5.  *        _KbdFlush - flushes the keyboard buffer
  6.  *        getpass   - reads a password
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #include <conio.h>
  19. #include <stdio.h>
  20. #include <dos.h>
  21.  
  22. /*---------------------------------------------------------------------*
  23.  
  24. Name            _KbdFlush - flushes the keyboard buffer
  25.  
  26. Usage           static void pascal _KbdFlush(void);
  27.  
  28. Description     flushes the keyboard buffer
  29.  
  30. Return value    nothing
  31.  
  32. *---------------------------------------------------------------------*/
  33. static void pascal _KbdFlush(void)
  34. {
  35.         bdos(0x0c, 0x00, 0x02);
  36. }
  37.  
  38.  
  39. /*---------------------------------------------------------------------*
  40.  
  41. Name            getpass - reads a password
  42.  
  43. Usage           char *getpass(const char *prompt);
  44.  
  45. Prototype in    conio.h
  46.  
  47. Description     getpass reads a password from the system console after
  48.                 prompting with the null-terminated string prompt and disabling
  49.                 the echo. A pointer is returned to a null-terminated string
  50.                 of up to eight characters at most (not counting the
  51.                 null-terminator).
  52.  
  53. Return value    The return value is a pointer to a static string
  54.                 which is overwritten with each call.
  55.  
  56. *---------------------------------------------------------------------*/
  57. char * _FARFUNC getpass(const char *prompt)
  58. {
  59.         register char   *cp;
  60.         register int    i;
  61.  
  62.         static  char    xc[9];
  63.  
  64.         /* Print the prompt message */
  65.         fprintf(stderr, "%s", prompt);
  66.  
  67.         /* Flush the keyboard buffer */
  68.         _KbdFlush();
  69.  
  70.         /* Read the password from keyboard without echo */
  71.         for (cp = xc, i = 0; i < 8 ; i++, cp++)
  72.                 if ((*cp = bdos(7,0,0)) == '\r')
  73.                         break;
  74.  
  75.         *cp = '\0';             /* Password is a NULL terminated string */
  76.         bdos(0x02,'\r',0);      /* Display a new line                   */
  77.         bdos(0x02,'\n',0);
  78.  
  79.         /* Read any remaining characters */
  80.         _KbdFlush();
  81.         return(xc);
  82. }
  83.