How to hide the typed characters

Posted on 2001-09-03
Last Modified: 2013-12-03
I want to accept the password,so don't want to display the characters.How do I hide this ,I am using C ang Gcc to compile.
Using getch() gives a peculiar error.Says two other library functions are undefined.
Question by:rasaraja
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 6453629
why don't you use pam?
LVL 51

Expert Comment

ID: 6456131
see ioctl()

Author Comment

ID: 6459249
Would you kindly be a bit more descriptive?What's pam ?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6468955
I assume you're reading the password from a regular tty (terminal; virtual console; X pseudo-tty; whatever).  What causes what you type to display on the terminal is the tty line discipline (basically, a device driver) in the kernel.  You can change this default setting with a command 'stty -echo" (and view the current settings with 'stty -a').  Do a 'man stty' and play around with it.

Stty turns echoing on and off by issuing a TCSETATTR ioctl, via the tcsetattr() library function.  Man tcsetattr (or better, read the GNU C library user's guide) for details.  Your program can turn echoing off with a tcsetattr(), read the password, then turn echoing back on.

As for why getch() doesn't work for you, that's another question and you need to provide more details.  It works for me.

I know little about pam -- I have found it only to be a pain in the butt.  However, it provides library functions that handle the turning off of echoing and many other things.  If it's not too complicated for what you're doing, it might be the right way to go and garboua can give you more information.


Author Comment

ID: 6469299
HI bryanh,

It would be great if u can tell me how I can use getch.I am ready tp provide all the details you require.

Expert Comment

ID: 6471233
What library does the compiler (or whatever) complain about being undefined?  Could you post the exact error messages?

Accepted Solution

acarna earned 50 total points
ID: 6478212
Also you could use the getpass() function:

#include <stdlib.h>

char *getpass(const char *prompt);

Note that "getpass" truncates the typed password to at most eight characters.



Author Comment

ID: 6481258
Acarna,this is exactly what I was looking for.But it does not seem that getpass() truncates to 8 chars.

Expert Comment

ID: 6481440
Oh, yes! If you are using a modern Unix maybe the function don't truncate the string but the primitive and old getpass(), you can be sure truncate the data.
Have this in mind in matters as portability.


Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question