
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. 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); }
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Unix and Linux Help
(Article 7931)
Tagged: c programming, linux device drivers, shell script programming Previous: How can I stop people stealing my blog images and photos? Next: Can I have Mac Preview sort thumbnails by creation date? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.
Rather amazingly, there are no comments on this article yet.
I have a lot to say, but ...
I do have a comment, now that you mention it!
|
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!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|