Thread: How to make password input in C ?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    How to make password input in C ?

    Hi everybody,
    I have a problem : When I press password using scanf() function, how to hide string input ?
    It's impossible with scanf() function on Linux Terminal ?

    Thanks,

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by tqnst View Post
    Hi everybody,
    I have a problem : When I press password using scanf() function, how to hide string input ?
    It's impossible with scanf() function on Linux Terminal ?

    Thanks,
    You have to set the termios attributes do disable ECHO. See "man tcsetattr" and this example snippet, which will get a string without echoing it back:

    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    
    int main(void)
    {
        struct termios term, term_orig;
        tcgetattr(STDIN_FILENO, &term);
        term_orig = term;
        term.c_lflag &= ~ECHO;
        tcsetattr(STDIN_FILENO, TCSANOW, &term);
    
        char buf[1000];
        scanf("%s", buf);
        printf("Read: %s\n", buf);
    
        /* Remember to set back, or your commands won't echo! */
        tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Want input in password format
    By Lokesh in forum C Programming
    Replies: 3
    Last Post: 10-30-2007, 02:18 PM
  2. Input a password and replace with *s
    By Abda92 in forum C Programming
    Replies: 45
    Last Post: 10-06-2007, 03:52 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM