Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How can I read a password without echoing it in C?

I'm trying to figure out how to disable the echo flag in C. I want to do this because I am coding a C program that must ask for a users password, so I would like to disable Echo, let the user enter his password, then re-enable Echo.


Dave's Answer:

Before I show you the solution in C, I want to start by demonstrating how to do this in a shell script, because they both demonstrate how to solve the problem and they both show that the solution is to learn just a bit about the keyboard device driver.

Specifically, we Unix old-timers refer to it as the tty, quite literally teletypewriter or teletype device. Think late 60's clattering printer / keyboard hooked up via a thick wire to a computational device the size of a small washing machine and you'd be on the money.

This device name is still around, and in fact the keyboard driver is still called the "tty". Try it, type tty on the command line in Linux:

$ tty
/dev/ttys000

Amazing, isn't it? Thirty years later...

On the Linux command line, the utility that lets you change the settings of this "tty" command is stty, and here's exactly how you could use this to turn off echo temporarily in a shell script to ask for a password:

save_state=$(stty -g)

/bin/echo -n "Account: "
read acct
/bin/echo -n "Password: "
stty -echo
read password # this won't echo
stty "$save_state"

echo ""
echo account = $acct and password = $password

When run, it looks like this:

Account: test
Password:
account = test and password = demo

Now, finally, let's convert that to a C program.

Fortunately, there's a very useful function in C that's widely available called getpasswd() which does all the "stty" heavy lifting. It makes things easy:

#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>

main()
{
  char acct[80], password[80];

  printf("Account: ");
  fgets(acct, 80, stdin);

  acct[strlen(acct)-1] = 0; /* remove carriage return */

  strncpy(password, getpass("Password: "), 80);
  printf("You entered acct %s and pass %s\n", acct, password);
}


If you want to do something more sophisticated where you output an asterisk each time they enter a character, you can check out some of the example code on the Ubuntu forums discussion.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

If you use Bash, the read command has the -s option to not echo characters to the terminal, and the -p option to show a prompt:

read -s -p "Password: "

So the example could be written as:

read -p "Account: " acct
read -s -p "Password: " password # this won't echo
echo
echo account = $acct and password = $password

Thanks for the C sample code.

Posted by: Fjor at March 25, 2009 10:54 AM

Great help! Thanks very much :)

Posted by: Ashish at July 23, 2009 4:43 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement


Uniblue: Free Virus Scan

Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Linux Journal: Free Issue!

Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.