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