db_bridge/console.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 59 a7ba10b68915
parent 23 7acfc5eda7ed
child 65 76514757b0d6
permissions -rw-r--r--
getpwnam_error.c: * The memory hole can be fixed with two different approaches 1) Change /etc/nsswitch.conf: passwd: compat to passwd: files 2) LD_PRELOAD=/lib/libnss_compat.so.2 valgrind ./getpwnam_error GLIBC loads libnss_compat on the fly and unloads it. Thanks to telexicon for reporting this... committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/db_bridge/console.cpp
 * Copyright (C) Markus Broeker
 */

#include <console.h>
#include <cstdio>

#ifdef WIN32
#include <conio.h>

int Console::getch ()
{
    int ch = -1;

    while (!kbhit ()) {
        ch =::getch ();
    }

    return ch;
}
#else
#include <termios.h>

int Console::getch ()
{
    int ch = -1, fd = 0;
    struct termios neu, alt;

    fd = fileno (stdin);
    tcgetattr (fd, &alt);
    neu = alt;
    neu.c_lflag &= ~(ICANON | ECHO);
    tcsetattr (fd, TCSANOW, &neu);
    ch = getchar ();
    tcsetattr (fd, TCSANOW, &alt);
    return ch;
}
#endif

void Console::getpass (std::string & password)
{
    int i = 0;
    int c;
    char buffer[17];

    while (((c = Console::getch ()) != '\n') && i < 17) {
        if (c != BS) {
            buffer[i++] = c;
            std::cout << "*";
        } else if (i > 0) {
            i--;
            std::cout << "\b \b";
        }
    }
    buffer[i] = 0;
    std::cout << std::endl;

    password = std::string (buffer);
}