how to hide characters on screen

T

torno

Hi,
Can someone guide me as to how I can modify the code below to hide
characters from displaying on the screen as they are entered? If there is
a better way of achieving this, please let me know.

I'm running this on unix (Solaris8).

thanks.

#include <stdio.h>

int main(void)
{
int passwdstr;

printf ( "Enter password: ");
while ( ( passwdstr=getchar() ) != '\r' ) {
putchar ('*');
}
return 0;
}
 
E

E. Robert Tisdale

torno said:
Can someone guide me as to how I can modify the code below to hide
characters from displaying on the screen as they are entered?
If there is a better way of achieving this, please let me know.

I'm running this on unix (Solaris8).

#include <stdio.h>

int main(void) {
int passwdstr;

printf ( "Enter password: ");
while ( ( passwdstr=getchar() ) != '\r' ) {
putchar ('*');
}
return 0;
}
> cat main.c
#include <unistd.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
char *getpass(const char* prompt);
fprintf(stdout, "password = %s\n",
getpass("Enter password: "));
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
Enter password:
password = password
 
R

Richard Bos

Read the FAQ. The FAQ is your friend.
<http://www.eskimo.com/~scs/C-faq/q19.1.html>. Admittedly, the way this
question is phrased in the index for section 19 is incomplete; but a
search of the text version (posted here regularly) on "password"
would've turned up this question.
#include <unistd.h>

This is not ISO C, and therefore off-topic.
char *getpass(const char* prompt);

This is not ISO C, and therefore off-topic. Moreover, function
declarations within other functions (yes, even within main()) are a Bad
Thing.
getpass("Enter password: "));

This is not ISO C, and therefore off-topic.

We don't care what your broken compiler says, it's still off-topic.

I'd tell you to read the bloody FAQ, as well, but in your case it would
probably be a waste of time, since you don't appear to give a damn.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top