Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn 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 Simpy.

Subscribe!

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

Comments
Rather amazingly, there are no comments on this article yet.

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

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.









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


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
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 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]