/*---------------------------------------------------------------------*

Name            getpass - reads a password

Usage           char *getpass(const char *prompt);

Prototype in    conio.h

Description     getpass reads a password from the system console after
                prompting with the null-terminated string prompt and disabling
                the echo. A pointer is returned to a null-terminated string
                of up to eight characters at most (not counting the
                null-terminator).

Return value    The return value is a pointer to a static string
                which is overwritten with each call.

*---------------------------------------------------------------------*/
char * _FARFUNC getpass(const char *prompt)
{
        register char   *cp;
        register int    i;

        static  char    xc[9];

        /* Print the prompt message */
        fprintf(stderr, "%s", prompt);

        /* Flush the keyboard buffer */
        _KbdFlush();

        /* Read the password from keyboard without echo */
        for (cp = xc, i = 0; i < 8 ; i++, cp++)
                if ((*cp = bdos(7,0,0)) == '\r')
                        break;

        *cp = '\0';             /* Password is a NULL terminated string */
        bdos(0x02,'\r',0);      /* Display a new line                   */
        bdos(0x02,'\n',0);

        /* Read any remaining characters */
        _KbdFlush();
        return(xc);
}
Example #2
0
char * _RTLENTRY _EXPFUNC getpass(const char *prompt)
{
    char   *cp;
    int    i, c;

    /* Print the prompt message.
     */
    for (cp = (char *)prompt; *cp; cp++)
        putch(*cp);

    /* Flush the keyboard buffer
     */
    _KbdFlush();

    /* Read the password from keyboard without echo
     */
    cp = passbuf;
    i = 0;
    for (;;)
    {
        if ((c = getch()) == '\r')
            break;
        if (c == '\b' || c == 0x7f)     /* control-H or backspace */
        {
            if (i == 0)                 /* already at start of line */
                putch('\a');            /* ring bell */
            else
                i--;                    /* back up current position */
        }
        else                            /* normal character */
        {
            if (i < PASS_MAX)
                cp[i] = c;
            i++;
        }
    }

    cp[i > PASS_MAX ? PASS_MAX : i] = '\0'; /* Password is null terminated */
    putch('\r');                        /* Display a new line */
    putch('\n');

    /* Discard any remaining characters.
     */
    _KbdFlush();
    return(cp);
}