Applications/login - Cleaned up input code
authorJohn Hodge <[email protected]>
Tue, 14 May 2013 03:38:32 +0000 (11:38 +0800)
committerJohn Hodge <[email protected]>
Tue, 14 May 2013 03:38:32 +0000 (11:38 +0800)
Usermode/Applications/login_src/main.c

index 81bd4ee..bc9013d 100644 (file)
@@ -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);
 }

UCC git Repository :: git.ucc.asn.au