Password prompt in unix w/o \b
c/c++ newbie is trying to improve the c++ classes by, well, making them more c++ized. C seems easier to me but that's no reason to not learn c++.
Yes, I looked at the faqs & did a search (web & local) before posting this. Doing a print char erase char sequence for a password prompt, like in the faq just rubs me wrong. Here's what I've got but it's so very c & I was wondering if there is a more natural c++ized way of doing this.
Code:
//#############################################################################
// Password = Prompt for same (password is always 16 chars in class)
//#############################################################################
void DB::Password(char* pass) throw (TerminalError){
struct termios ttyinit; //Term info before noecho for password prompt
struct termios ttynew; //Used to set noecho for password prompt
FILE* ttyin; //Redirect input to the terminal
FILE* ttyout; //Redirect output to the terminal
//=========================================================================
// Need to redirect i/o to the terminal for this prompt
//=========================================================================
ttyin = fopen("/dev/tty", "r");
ttyout = fopen("/dev/tty", "w");
if (!ttyin || !ttyout){
if (ttyin) fclose(ttyin);
if (ttyout) fclose(ttyout);
throw TerminalError();
}
//=========================================================================
// Need to save old terminal settings & then set up NOECHO
//=========================================================================
tcgetattr(fileno(ttyin), &ttyinit);
ttynew = ttyinit;
ttynew.c_lflag &= ~ECHO;
//=========================================================================
// Now prompt for the SQL password
//=========================================================================
fprintf(ttyout, "Enter SQL password: ");
fflush(ttyout);
if (tcsetattr(fileno(ttyin), TCSAFLUSH, &ttynew)) throw TerminalError();
fgets(pass, 16, ttyin);
for (int i = 0; pass[i] && i < 16; i++) if (pass[i] == '\n') pass[i] = 0;
tcsetattr(fileno(ttyin), TCSANOW, &ttyinit);
fprintf(ttyout, "\n");
fclose(ttyin);
fclose(ttyout);
}
OK ignore what the code tags did to my comments