From 536d7d5d3374c4d13afe7d5d099fd15a526d63fd Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 14 May 2013 11:38:32 +0800 Subject: [PATCH] Applications/login - Cleaned up input code --- Usermode/Applications/login_src/main.c | 73 +++++++++++++------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/Usermode/Applications/login_src/main.c b/Usermode/Applications/login_src/main.c index 81bd4ee3..bc9013d4 100644 --- a/Usermode/Applications/login_src/main.c +++ b/Usermode/Applications/login_src/main.c @@ -27,10 +27,13 @@ int main(int argc, char *argv[]) for(;;) { sUsername = GetUsername(); + if(!sUsername) continue; sPassword = GetPassword(); + if(!sPassword) continue; if( (uid = ValidateUser(sUsername, sPassword)) == -1 ) { printf("\nInvalid username or password\n"); + _SysDebug("Auth failure: '%s':'%s'", sUsername, sPassword); free(sUsername); free(sPassword); } @@ -61,38 +64,42 @@ int main(int argc, char *argv[]) return 0; } -/** - * \fn char *GetUsername() - */ -char *GetUsername() +char *_GetString(int bEcho) { - char ret[BUFLEN] = {0}; + char ret[BUFLEN]; int pos = 0; char ch; - // Prompt the user - printf("Username: "); - fflush(stdout); - // Read in text while( (ch = fgetc(stdin)) != -1 && ch != '\n' ) { + // Handle backspace if(ch == '\b') { if( pos <= 0 ) continue; pos --; ret[pos] = '\0'; } + // Ctrl-C : Cancel + else if( ch == 'c'-'a'+1) + pos = 0; + // Ctrl-U : Clear + else if( ch == 'u'-'a'+1) + pos = 0; + // Ignore \r + else if( ch == '\r' ) + continue; else ret[pos++] = ch; - // Echo out to the screen - fputc(ch, stdout); - fflush(stdout); + // Don't echo out to the screen + if( bEcho ) { + fputc(ch, stdout); + fflush(stdout); + } if(pos == BUFLEN-1) break; } - // Finish String ret[pos] = '\0'; printf("\n"); @@ -100,37 +107,29 @@ char *GetUsername() } /** - * \fn char *GetPassword() + * \fn char *GetUsername() */ -char *GetPassword() +char *GetUsername() { - char ret[BUFLEN]; + char ret[BUFLEN] = {0}; int pos = 0; char ch; // Prompt the user - printf("Password: "); + printf("Username: "); fflush(stdout); - // Read in text - while( (ch = fgetc(stdin)) != -1 && ch != '\n' ) - { - if(ch == '\b') { - if( pos <= 0 ) continue; - pos --; - ret[pos] = '\0'; - } - else - ret[pos++] = ch; - - // Don't echo out to the screen - //fputc(stdout, ch); - - if(pos == BUFLEN-1) break; - } - - ret[pos] = '\0'; + return _GetString(1); +} + +/** + * \fn char *GetPassword() + */ +char *GetPassword() +{ + // Prompt the user + printf("Password: "); + fflush(stdout); - printf("\n"); - return strdup(ret); + return _GetString(0); } -- 2.20.1